1
import threading
import time
def test1():
print "hello"
time.sleep(15)
print "hello2"
def test2():
print "hi"
th1 =threading.Thread(test1())
th2 =threading.Thread(test2())
p=[th1,th2]
for i in p:
i.start()
for i in p:
i.join()
我不確定我是否正確,如果我不是,請更正我。我期待輸出按這個順序打印你好,你好,你好。因爲我期望創建的兩個線程並行運行。但我得到了下面的輸出,你好你好2和嗨。線程2僅在完成thread1之後運行。我做錯了什麼?或者我的理解或線程錯誤?python中的多線程不按預期工作
您正在調用函數並將其結果直接傳遞給Thread()的構造函數。 –