关于python多线程join()

import threading
import time

class 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 = 0
print 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()在这里意义何在?

阅读 7.4k
2 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题