我只知道多线程的基本概念,目前遇到需要帮助的情况。
我有两个任务要完成,都应该连续执行。问题是第二个任务应该在第一个线程首先完成一些工作之后才开始。现在这两个线程类大致如下所示:
finished = False # shared flag
class first(threading.Thread):
def __init__(self, cond, finished):
threading.Thread.__init__(self)
self.cond = cond
self.finished = finished
def run(self):
self.cond.aquire()
do_something()
self.finished = True #change the flag
self.cond.notify()
self.cond.release()
do_something_else()
class second(threading.Thread):
def __init__(self, cond, finished):
threading.Thread.__init__(self)
self.cond = cond
self.finished = finished
def run(self):
self.cond.aquire()
while self.finished == False:
self.cond.wait()
self.cond.release()
do_something()
然而,事实是程序仍然随机执行而不管wait()和notify()。有人可以帮我解决这个问题吗?谢谢。