玩蛇网提供最新Python编程技术信息以及Python资源下载!

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

不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 )~

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19341.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月19日 - 18时17分30秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules