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()的构造函数。 –