前言
今天小編YOYO請xiaoming和xiaowang吃火鍋,吃完火鍋的時候會有以下三種場景:
-
場景一:小編(主)先吃完了,xiaoming(客)和xiaowang(客)還沒吃完,這種場景會導致結賬的人先走了,剩下兩個小伙伴傻眼了。。。
-
場景二:小編(主)先吃完了,xiaoming和xiaowang還沒吃飽,一起結賬走人。
-
場景三:小編(主)先等xiaoming和xiaowang吃飽了,小編最后結賬一起走人。
主線程與子線程
場景一:主線程已經結束了,子線程還在跑
1.我們把thread1.start()和thread2.start()稱為兩個子線程,寫在外面的代碼就是主線程了。
# coding=utf-8
import threading
import time
def chiHuoGuo(people):
print("%s 吃火鍋的小伙伴-羊肉:%s" % (time.ctime(),people))
time.sleep(1)
print("%s 吃火鍋的小伙伴-魚丸:%s" % (time.ctime(),people))
class myThread (threading.Thread): # 繼承父類threading.Thread
def __init__(self, people, name):
'''重寫threading.Thread初始化內容'''
threading.Thread.__init__(self)
self.threadName = name
self.people = people
def run(self): # 把要執行的代碼寫到run函數里面 線程在創建后會直接運行run函數
'''重寫run方法'''
print("開始線程: " + self.threadName)
chiHuoGuo(self.people) # 執行任務
print("qq交流群:226296743")
print("結束線程: " + self.name)
print("yoyo請小伙伴開始吃火鍋:!!!")
# 創建新線程
thread1 = myThread("xiaoming", "Thread-1")
thread2 = myThread("xiaowang", "Thread-2")
# 開啟線程
thread1.start()
thread2.start()
time.sleep(0.1)
print("退出主線程:吃火鍋結束,結賬走人")
2.運行結果:
守護線程setDaemon()
場景二:主線程結束了,子線程必須也跟着結束
1.主線程中,創建了子線程thread1和thread2,並且在主線程中調用了thread.setDaemon(),這個的意思是,把主線程設置為守護線程,這時候,要是主線程執行結束了,就不管子線程是否完成,一並和主線程退出.
(敲黑板:必須在start()方法調用之前設置,如果不設置為守護線程,程序會被無限掛起。)
2.線程有一個布爾屬性叫做daemon。表示線程是否是守護線程,默認取否。當程序中的線程全部是守護線程時,程序才會退出。只要還存在一個非守護線程,程序就不會退出。
主線程是非守護線程。
3.setDaemon(True)此方法里面參數設置為True才會生效
# coding=utf-8
import threading
import time
def chiHuoGuo(people):
print("%s 吃火鍋的小伙伴-羊肉:%s" % (time.ctime(),people))
time.sleep(1)
print("%s 吃火鍋的小伙伴-魚丸:%s" % (time.ctime(),people))
class myThread (threading.Thread): # 繼承父類threading.Thread
def __init__(self, people, name):
'''重寫threading.Thread初始化內容'''
threading.Thread.__init__(self)
self.threadName = name
self.people = people
def run(self): # 把要執行的代碼寫到run函數里面 線程在創建后會直接運行run函數
'''重寫run方法'''
print("開始線程: " + self.threadName)
chiHuoGuo(self.people) # 執行任務
print("qq交流群:226296743")
print("結束線程: " + self.name)
print("yoyo請小伙伴開始吃火鍋:!!!")
# 創建新線程
thread1 = myThread("xiaoming", "Thread-1")
thread2 = myThread("xiaowang", "Thread-2")
# 守護線程setDaemon(True)
thread1.setDaemon(True) # 必須在start之前
thread2.setDaemon(True)
# 開啟線程
thread1.start()
thread2.start()
time.sleep(0.1)
print("退出主線程:吃火鍋結束,結賬走人")
4.運行結果:
阻塞主線程join(timeout)
1.如果想讓主線程等待子線程結束后再運行的話,就需要用到join(),此方法是在start之后(與setDaemon相反)
2.join(timeout)此方法有個timeout參數,是線程超時時間設置。
# coding=utf-8
import threading
import time
def chiHuoGuo(people):
print("%s 吃火鍋的小伙伴-羊肉:%s" % (time.ctime(),people))
time.sleep(1)
print("%s 吃火鍋的小伙伴-魚丸:%s" % (time.ctime(),people))
class myThread (threading.Thread): # 繼承父類threading.Thread
def __init__(self, people, name):
'''重寫threading.Thread初始化內容'''
threading.Thread.__init__(self)
self.threadName = name
self.people = people
def run(self): # 把要執行的代碼寫到run函數里面 線程在創建后會直接運行run函數
'''重寫run方法'''
print("開始線程: " + self.threadName)
chiHuoGuo(self.people) # 執行任務
print("qq交流群:226296743")
print("結束線程: " + self.name)
print("yoyo請小伙伴開始吃火鍋:!!!")
# 創建新線程
thread1 = myThread("xiaoming", "Thread-1")
thread2 = myThread("xiaowang", "Thread-2")
# 開啟線程
thread1.start()
thread2.start()
# 阻塞主線程,等子線程結束
thread1.join()
thread2.join()
time.sleep(0.1)
print("退出主線程:吃火鍋結束,結賬走人")
運行結果:
參考代碼:
# coding=utf-8
import threading
import time
def chiHuoGuo(people):
print("%s 吃火鍋的小伙伴-羊肉:%s" % (time.ctime(),people))
time.sleep(1)
print("%s 吃火鍋的小伙伴-魚丸:%s" % (time.ctime(),people))
class myThread (threading.Thread): # 繼承父類threading.Thread
def __init__(self, people, name):
'''重寫threading.Thread初始化內容'''
threading.Thread.__init__(self)
self.threadName = name
self.people = people
def run(self): # 把要執行的代碼寫到run函數里面 線程在創建后會直接運行run函數
'''重寫run方法'''
print("開始線程: " + self.threadName)
chiHuoGuo(self.people) # 執行任務
print("qq交流群:226296743")
print("結束線程: " + self.name)
print("yoyo請小伙伴開始吃火鍋:!!!")
# 設置線程組
threads = []
# 創建新線程
thread1 = myThread("xiaoming", "Thread-1")
thread2 = myThread("xiaowang", "Thread-2")
# 添加到線程組
threads.append(thread1)
threads.append(thread2)
# 開啟線程
for thread in threads:
thread.start()
# 阻塞主線程,等子線程結束
for thread in threads:
thread.join()
time.sleep(0.1)
print("退出主線程:吃火鍋結束,結賬走人")
python自動化交流 QQ群:779429633