Python join多线程意义何在,pythonjoin,import threa


import threadingimport timeclass Producer(threading.Thread):    def __init__(self,t_name):        threading.Thread.__init__(self,name=t_name)    def run(self):        global x        con.acquire()        if x>0:            con.wait()        else:            for i in xrange(0,5,1):                x+=1                print u'producing...'+str(x)            con.notify()        print x        con.release()class Consumer(threading.Thread):    def __init__(self,t_name):        threading.Thread.__init__(self,name=t_name)    def run(self):        global x        con.acquire()        if x<0:            con.wait()        else:            for i in xrange(0,5,1):                x-=1                print u'consume...'+str(x)            con.notify()        print x        con.release()con = threading.Condition()x = 0print u'start consumer'c = Consumer('consumer')print u'start producer'p = Producer('producer')p.start()c.start()p.join()c.join()print x

最后的join()函数的有无对程序结果并无显示作用?请问join()在这里意义何在?

不join 主线程结束了你的子线程怎么办

Other threads can call a thread’s join() method. This blocks the calling thread until the thread whose join() method is called is terminated.
Thread Objects
threading.Thread.join
阻塞当前调用 thread.join()的进程或者线程,直到join的线程( the thread whose join() method is called )结束,才继续执行当前进程或者线程(the calling thread )~

编橙之家文章,

评论关闭