初学python的多线程,有一道经典的面试题,对于很多和我一样的初学者来说可能开始都不容易理解和掌握。
分享一下自己的理解和代码。重点是***的部分,希望对大家有帮助!
问:生成5个线程按照顺序来输出1234512345... 。其中1为线程1输出,2为线程2输出以此类推。
思路:使用Condition
解答:
import threading
cond = threading.Condition() # 利用Condition
#
def domywork(threadnum):
print(str(threadnum), end='')
# 构建一个自己的线程类
class mythread(threading.Thread):
def __init__(self, threadnum, thdcnt):
threading.Thread.__init__(self) # 父类初始化
self.thdnum = threadnum
self.thdcnt = thdcnt
def run(self) -> None:
global nowthdnum
jobcnt = 10 # 这里循环了10次,也可以放到mythread类的参数里,自己做做
for i in range(jobcnt):
with cond:
# *** 这里是重点1:因为系统调度线程执行时是任意的,即处于就绪状态的线程都可能被调度执行 ***
# 所以,执行到这里时,判断一下执行中的线程如果不等于nowthdnum(即没有按指定顺序执行时,让这个线程等待)
while nowthdnum != self.thdnum:
cond.wait()
# print('-- nowthdnum