如果要让一个任务队列按照顺序进行,则必须使用join,代码如下:

'''
Created on Dec 23, 2013 @author: long
'''
import threading
from threading import Thread
import time class Thread1(Thread):
'''
classdocs
''' def __init__(self,thread_name):
'''
Constructor
'''
Thread.__init__(self,name=thread_name) def run(self):
'''
run method
'''
count = 0
while True:
print ('thread--',self.getName(),",count:",count)
time.sleep(0.5)
count = count + 1
if count > 10:
break def main(): for y in range(1, 3):
thread1 = Thread1('longthread' + str(y))
thread1.start()
if thread1.isAlive():
thread1.join() for i in range(50):
print ('main:', i) if __name__ == "__main__":
main()

 结果是先执行名为'longthread1',再'longthread2',再是主进程,所以thread1.join()的意思是等thread1执行完,再去执行其他线程。

04-14 18:09