python 中的队列
来源:互联网 发布:网络博客犯法吗 编辑:程序博客网 时间:2024/06/08 04:47
queue 可以保证每个线程取和存数据的安全性,因为它里面实现了锁机制
#-*-coding:utf-8-*- #__author:martin#date:2017/10/23import threadingimport randomimport timeclass Producer(threading.Thread): def run(self): global L while True: val = random.randint(0,100) print('生产者',self.name,'append '+str(val),L) if lock_con.acquire(): L.append(val) lock_con.notify() lock_con.release() time.sleep(3)class Consumer(threading.Thread): def run(self): global L while True: if lock_con.acquire(): if len(L) == 0 : lock_con.wait() print('消费者', self.name, 'delete ' + str(L[0]), L) del L[0] lock_con.release() time.sleep(1)if __name__ == '__main__': L = [] lock_con = threading.Condition() threads = [] for i in range(5): threads.append(Producer()) threads.append(Consumer()) for t in threads: t.start() for j in threads: j.join() print('======================')
阅读全文
0 0
- python中的队列、栈
- python 中的队列
- python中的队列和栈
- Python中的堆栈、队列、链表
- Python入门:队列在线程中的使用
- 栈和队列在python中的数据结构
- python collections模块中的双端队列
- python 队列
- Python- 队列
- python 队列
- Python-队列
- Python 队列
- 用Python中的列表模拟栈和队列
- Python通过amqp消息队列协议中的Qpid实现数据通信
- Queue同步队列类在python中的应用
- Python队列及在微信机器人中的应用
- Python队列及在微信机器人中的应用
- Python 线程同步队列
- 1-twoSum
- Android学习笔记
- 欢迎使用CSDN-markdown编辑器
- Markdown编辑器简明教程
- java 之 泛型接口
- python 中的队列
- #NOIP模拟赛bzoj3449#大佬(期望好题)
- noip模拟10.24
- 【1013】数字河
- Leetcode 1. Two Sum
- mybatis的一对多和多对多查询
- openstack RPC通信
- 新人
- 1701 H2王建瑜 连续第一天