如果要让一个任务队列按照顺序进行,则必须使用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执行完,再去执行其他线程。

相关文章:

  • 2021-11-16
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
猜你喜欢
  • 2021-07-01
  • 2021-05-18
  • 2021-11-19
  • 2022-12-23
  • 2021-12-09
  • 2021-09-08
相关资源
相似解决方案