Python?中?threading.Thread.join()?的使用方法示例詳解
threading.Thread.join() 方法用于阻塞當(dāng)前線程,直到調(diào)用它的線程對象執(zhí)行完成或者超時(shí)。這在需要等待子線程執(zhí)行完畢后再繼續(xù)執(zhí)行主線程時(shí)非常有用?;谒@種特性,我講用我的方法幫你選擇你合適的解決方案。

問題背景
在 Python 中,想要充分利用多線程的優(yōu)勢,就需要對 threading 模塊中的 Thread 類有一定的了解。這里有一個(gè)非常簡單的多線程程序,用于幫助我們理解 threading.Thread.join 方法。
import threading
val = 0
def increment():
global val
print("Inside increment")
for x in range(100):
val += 1
print("val is now {} ".format(val))
thread1 = threading.Thread(target=increment, args=())
thread2 = threading.Thread(target=increment, args=())
thread1.start()
# thread1.join()
thread2.start()
# thread2.join()這里有兩個(gè)問題:
如果注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果會(huì)是怎樣的?如果不注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果又會(huì)是怎樣的?
解決方法
1. 不注釋掉 join() 方法
如果我們不注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果如下:
Inside increment val is now 1 val is now 2 val is now 3 ... val is now 100 Inside increment val is now 1 val is now 2 val is now 3 ... val is now 100
2. 注釋掉 join() 方法
如果我們注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果如下:
Inside increment Inside increment val is now 1 val is now 1 val is now 2 val is now 3 ... val is now 99 val is now 2 val is now 3 ... val is now 98 val is now 99 val is now 100
比較輸出結(jié)果
通過比較這兩個(gè)輸出結(jié)果,我們可以發(fā)現(xiàn),如果注釋掉 join() 方法,那么兩個(gè)線程的輸出結(jié)果是交織在一起的,這表明這兩個(gè)線程是并發(fā)執(zhí)行的。而如果不注釋掉 join() 方法,那么兩個(gè)線程的輸出結(jié)果是按照順序輸出的,這表明這兩個(gè)線程是串行執(zhí)行的。
join() 方法的作用
join() 方法的作用是讓調(diào)用它的線程等待另一個(gè)線程終止。在我們的例子中,thread1.join() 和 thread2.join() 的作用是讓主線程等待 thread1 和 thread2 兩個(gè)線程終止。如果不注釋掉這兩個(gè)方法,那么主線程就會(huì)等待這兩個(gè)線程終止后才繼續(xù)執(zhí)行。而如果注釋掉這兩個(gè)方法,那么主線程就不會(huì)等待這兩個(gè)線程終止,而是直接繼續(xù)執(zhí)行。
代碼示例:
為了更清楚地了解 join() 方法的作用,我們修改一下上面的代碼:
import threading
val = 0
def increment(msg,sleep_time):
global val
print("Inside increment")
for x in range(10):
val += 1
print("%s : %d\n" % (msg,val))
time.sleep(sleep_time)
thread1 = threading.Thread(target=increment, args=("thread_01",0.5))
thread2 = threading.Thread(target=increment, args=("thread_02",1))
thread1.start()
thread1.join()
thread2.start()
thread2.join()如果我們運(yùn)行這段代碼,那么輸出結(jié)果如下:
Inside increment
thread_01 : 1thread_01 : 2
thread_01 : 3
thread_01 : 4
thread_01 : 5
thread_01 : 6
thread_01 : 7
thread_01 : 8
thread_01 : 9
thread_01 : 10
Inside increment
thread_02 : 1thread_02 : 2
thread_02 : 3
thread_02 : 4
thread_02 : 5
thread_02 : 6
thread_02 : 7
thread_02 : 8
thread_02 : 9
thread_02 : 10
從輸出結(jié)果中,我們可以看到,這兩個(gè)線程是按照順序輸出的,這表明這兩個(gè)線程是串行執(zhí)行的。這是因?yàn)槲覀冊诖a中使用了 thread1.join() 和 thread2.join() 這兩個(gè)方法,讓主線程等待這兩個(gè)線程終止后才繼續(xù)執(zhí)行。
在這個(gè)例子中,主線程啟動(dòng)了一個(gè)子線程,并在子線程執(zhí)行完成之前調(diào)用了 join() 方法來等待子線程執(zhí)行完成。如有任何疑問可以評論區(qū)留言討論。
到此這篇關(guān)于Python 中 threading.Thread.join() 的使用方法的文章就介紹到這了,更多相關(guān)Python threading.Thread.join()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
keras訓(xùn)練曲線,混淆矩陣,CNN層輸出可視化實(shí)例
這篇文章主要介紹了keras訓(xùn)練曲線,混淆矩陣,CNN層輸出可視化實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python numpy實(shí)現(xiàn)數(shù)組合并實(shí)例(vstack,hstack)
這篇文章主要介紹了Python numpy實(shí)現(xiàn)數(shù)組合并(官網(wǎng)實(shí)例),涉及vstack,hstack的簡單用法,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
結(jié)合Python工具使用TfidfVectorizer進(jìn)行文本特征提取方式
在自然語言處理中,TF-IDF是一種重要的特征提取方法,本文介紹了如何使用Python的sklearn庫中的TfidfVectorizer進(jìn)行文本特征提取,首先,需要安裝sklearn庫,TfidfVectorizer能將文本文檔集合轉(zhuǎn)換為TF-IDF特征矩陣2024-10-10

