设为首页 加入收藏

TOP

queue队列(二)
2017-12-07 14:22:58 】 浏览:375
Tags:queue 队列
e() lock = threading.Lock() count = 1 class ProducerThread(threading.Thread): def __init__(self, p_name): super(ProducerThread, self).__init__() self.p_name = p_name def run(self): global count # 骨头的数量累加 # count = 1 如果count 放在这的话,所有的生产者生产的数量从1开始 while True: lock.acquire() # 加锁,确保count数据安全 print('%s生成了骨头%s' % (self.p_name, count)) q.put("%s生成的骨头%s" % (self.p_name, count)) count += 1 time.sleep(1) lock.release() class ConsumerThread(threading.Thread): def __init__(self, c_name): super(ConsumerThread, self).__init__() self.c_name = c_name def run(self): while True: print("[%s] 取到了[%s],并且吃了它..." % (self.c_name, q.get())) # q.task_done() if __name__ == '__main__': for i in range(4): p = ProducerThread('Producer%s' % i) p.start() for i in range(5): c = ConsumerThread("dog%s" % i) c.start()

  

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python3的基础 下一篇djang-分页

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目