classXiaoAi(threading.Thread):def __init__(self, cond):
super().__init__(name="小爱")
self.cond=conddefrun(self):
with self.cond:
self.cond.wait()print("{} : 在".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 好啊".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 君住长江尾".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 共饮长江水".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 此恨何时已".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 定不负相思意".format(self.name))
self.cond.notify()classTianMao(threading.Thread):def __init__(self, cond):
super().__init__(name="天猫精灵")
self.cond=conddefrun(self):
with self.cond:print("{} : 小爱同学".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 我们来对古诗吧".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 我住长江头".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 日日思君不见君".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 此水几时休".format(self.name))
self.cond.notify()
self.cond.wait()print("{} : 只愿君心似我心".format(self.name))
self.cond.notify()
self.cond.wait()if __name__ == "__main__":
cond=threading.Condition()
xiaoai=XiaoAi(cond)
tianmao=TianMao(cond)#启动顺序很重要
#在调用with cond之后才能调用wait或者notify方法
xiaoai.start()
tianmao.start()