正在加载图片...
Python多线程编程 历些毛子代拔大》 XIDIAN UNIVERSITY 口线程同步锁互斥控制 import threading,time if name=='main_: class myThread(threading.Thread): count =0 def init (self,n): #同步锁,也称互斥量 threading.Thread.init (self) lock threading.Lock() self.myThread nn threads=[] def run(self): for i in range(5): global count threads.append(myThread(1000)) for i in range(self.myThread_n): for t in threads: if lock.acquire(): t.start() Temp count time.sleep(0.0001) time.sleep(5) count Temp +1 print('count:',count) lock.release() Python多线程编程 线程同步锁互斥控制 7 import threading, time class myThread(threading.Thread): def __init__(self, n): threading.Thread.__init__(self) self.myThread_n = n def run(self): global count for i in range(self.myThread_n): if lock.acquire(): __Temp = count time.sleep(0.0001) count = __Temp + 1 lock.release() if __name__==’__main__’: count = 0 #同步锁,也称互斥量 lock = threading.Lock() threads = [] for i in range(5): threads.append(myThread(1000)) for t in threads: t.start() time.sleep(5) print(‘count:’,count)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有