作为使用“条件”对象在python中练习线程间通信的一部分,我编写了以下代码
from threading import *
import time
c=Condition()
def sum():
time.sleep(2)#if i comment this line then pgm waits,only sum() fun print stmt is executed
c.acquire()
print('here after this stms notify is called')
c.notify()
c.release()
def display():
c.acquire()
c.wait()
print('display print is executing after notify')
c.notify()
c.release()
def show():
c.acquire()
c.wait()
print('show print is executing after notify')
c.release()
t2=Thread(target=display)
t3=Thread(target=show)
t1=Thread(target=sum)
t1.start()
t2.start()
t3.start()
pgm工作正常,但是如果我在time.sleep(2)
函数中注释sum()
,则仅执行sum()函数的print stmt,然后等待。请告诉我为什么会发生这种情况,为什么这行time.sleep(2)
会真正改变这种情况