class ModelThread():
def __init__(self):
self.newrun1 = threading.Thread(target=self.target1)
self.newrun2 = threading.Thread(target=self.target2)
self.event = threading.Event()
def run(self):
self.newrun1.start()
self.newrun2.start()
self.newrun1.join()
self.newrun2.join()
def target1(self):
while True:
for i in range(1,9999999):
print(i, end=' ')
time.sleep(1)
def target2(self):
while True:
for i in ['A','B','C','D','E','F','G','H','I']:
self.event.set()
if i == 'D':
pass #阻塞(挂起)target1
print(i, end=' ')
time.sleep(1)
if i=='I':
pass #解除阻塞(挂起)
if __name__ == '__main__':
a = ModelThread()
a.run()
最近学习python中,遇到一个多线程问题,有没有学霸提供思路呢?