039条件变量同步(Condition)
2018-06-18 01:48:04来源:未知 阅读 ()
也是锁,这个锁多加了wait(),notify()唤醒一个进程,notifyall()唤醒全部进程方法,创建的时候默认是Rlock类型的锁,可以设置为lock类型的,默认就ok
1 from random import randint 2 import threading 3 import time 4 5 class Producer(threading.Thread): 6 def run(self): 7 global L 8 while True: 9 val = randint(0,100) 10 print('生产者',self.name,':append',str(val),L) 11 if lock_con.acquire(): 12 L.append(val) 13 lock_con.notify() 14 lock_con.release() 15 time.sleep(3) 16 17 class Consumer(threading.Thread): 18 def run(self): 19 global L 20 while True: 21 lock_con.acquire() 22 if len(L) == 0: 23 lock_con.wait() 24 print('消费者',self.name,'delete',str(L[0]),L) 25 del L[0] 26 lock_con.release() 27 time.sleep(0.5) 28 29 if __name__ == '__main__': 30 L = [] 31 lock_con = threading.Condition() 32 threads = [] 33 for i in range(5): 34 threads.append(Producer()) 35 threads.append(Consumer()) 36 for t in threads: 37 t.start() 38 for t in threads: 39 t.join()
###########java改编:单生产单消费
1 import time 2 import threading 3 4 class Res: 5 def __init__(self): 6 self.flag = False 7 self.count = 0 8 self.product = '' 9 10 def set(self,name): 11 lock_con.acquire() 12 if self.flag: 13 lock_con.wait() 14 time.sleep(0.00001) 15 self.count += 1 16 self.product = ''.join([name,'**',str(self.count)]) 17 self.message = ''.join([self.product,'__生产者__',str(threading.current_thread())]) 18 print(self.message) 19 self.flag = True 20 lock_con.notify() 21 lock_con.release() 22 23 def get_product(self): 24 lock_con.acquire() 25 time.sleep(0.00001) 26 if not self.flag: 27 lock_con.wait() 28 self.message = ''.join([self.product,'__消费者__',str(threading.current_thread())]) 29 print(self.message) 30 self.flag = False 31 lock_con.notify() 32 lock_con.release() 33 34 class Producer(threading.Thread): 35 def __init__(self,r): 36 threading.Thread.__init__(self) 37 self.r = r 38 39 def run(self): 40 for i in range(100): 41 self.r.set('大白兔奶糖') 42 43 class Consumer(threading.Thread): 44 def __init__(self,r): 45 threading.Thread.__init__(self) 46 self.r = r 47 48 def run(self): 49 for i in range(100): 50 self.r.get_product() 51 52 if __name__ == '__main__': 53 lock_con = threading.Condition() 54 r = Res() 55 c = Consumer(r) 56 p = Producer(r) 57 c.start() 58 p.start()
############多生产多消费
1 import time 2 import threading 3 4 class Res: 5 def __init__(self): 6 self.flag = False 7 self.count = 0 8 self.product = '' 9 10 def set(self,name): 11 lock_con.acquire() 12 while self.flag: 13 lock_con.wait() 14 time.sleep(0.00001) 15 self.count += 1 16 self.product = ''.join([name,'**',str(self.count)]) 17 self.message = ''.join([self.product,'__生产者__',str(threading.current_thread())]) 18 print(self.message) 19 self.flag = True 20 lock_con.notifyAll() 21 lock_con.release() 22 23 def get_product(self): 24 lock_con.acquire() 25 time.sleep(0.00001) 26 while not self.flag: 27 lock_con.wait() 28 self.message = ''.join([self.product,'__消费者__',str(threading.current_thread())]) 29 print(self.message) 30 self.flag = False 31 lock_con.notifyAll() 32 lock_con.release() 33 34 class Producer(threading.Thread): 35 def __init__(self,r): 36 threading.Thread.__init__(self) 37 self.r = r 38 39 def run(self): 40 for i in range(100): 41 self.r.set('大白兔奶糖') 42 43 class Consumer(threading.Thread): 44 def __init__(self,r): 45 threading.Thread.__init__(self) 46 self.r = r 47 48 def run(self): 49 for i in range(100): 50 self.r.get_product() 51 52 if __name__ == '__main__': 53 lock_con = threading.Condition() 54 r = Res() 55 l = [] 56 for i in range(5): 57 l.append(Consumer(r)) 58 for i in range(5): 59 l.append(Producer(r)) 60 for a in l: 61 a.start()
个人觉得例子理解是最好的,所以我学的东西一般使用例子
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:04-python第四天学习
- python变量和常量 2019-08-13
- 在 Python 中这样命名变量就是给自己挖坑 2019-08-13
- Python基础总结之第二天从变量开始(新手可相互督促) 2019-07-24
- 第四章 函数之高级函数 2019-07-24
- Python习题001 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash