from threading import *
from time import *
class MyThread(Thread):
def __init__(self,x):
self.x = x
Thread.__init__(self)
def run(self):
sleep(2)
print(self.x)
if __name__=='__main__':
threads = []
for i in range(5):
threads.append(MyThread('Hello'))
for i in range(5):
threads[i].start()
for i in range(5):
threads[i].join()
此代码打印“你好” 10倍,但如果我的评论“睡眠(2)”它打印“你好” 5倍。
sleep()函数有什么问题?或问题在哪里? 我正在使用Python3000。Python的线程奇怪的行为
程序为我打印'Hello'5次,无论'sleep()'调用是否被注释掉。由于您只创建5个线程,每个线程只打印一次“Hello”,所以我不会看到代码如何打印超过5次的“Hello”。 – 2009-11-16 06:54:53
我也没有看到它。 [:-)] – 2009-11-16 07:24:23