生产者消费者之Java/python实现
来源:互联网 发布:快跑者抢单软件 编辑:程序博客网 时间:2024/06/09 14:08
java实现版
/** * 生产者线程,负责生产公共资源 */ class ProducerThread implements Runnable { private PublicResource resource; public ProducerThread(PublicResource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep((long) (Math.random() * 10)); } catch (InterruptedException e) { e.printStackTrace(); } resource.increace(); } } } /** * 消费者线程,负责消费公共资源 */ class ConsumerThread implements Runnable { private PublicResource resource; public ConsumerThread(PublicResource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep((long) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } resource.decreace(); } } } /** * 公共资源类 */ class PublicResource { private int number = 0; /** * 增加公共资源 */ public synchronized void increace() { while (number != 0) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } number++; System.out.println(number); notify(); } /** * 减少公共资源 */ public synchronized void decreace() { while (number == 0) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } number--; System.out.println(number); notify(); } } public class ProducerConsumerTest { public static void main(String[] args) { PublicResource resource = new PublicResource(); new Thread(new ProducerThread(resource)).start(); new Thread(new ConsumerThread(resource)).start(); new Thread(new ProducerThread(resource)).start(); new Thread(new ConsumerThread(resource)).start(); new Thread(new ProducerThread(resource)).start(); new Thread(new ConsumerThread(resource)).start(); } }
python实现版
Python的线程是真正的Posix Thread,而不是模拟出来的线程。 Python的标准库提供了两个模块:thread和threading,thread是低级模块,threading是高级模块,对_thread进行了封装。绝大多数情况下,我们只需要使用threading这个高级模块。
Python中的Queue模块已经提供了对线程同步的支持,所以本文并没有涉及锁、同步、死锁等多线程问题。
#-*- coding:UTF-8 -*-from Queue import Queueimport random,threading,time#生产者类class Producer(threading.Thread): def __init__(self, name,queue): threading.Thread.__init__(self, name=name) self.data=queue def run(self): for i in range(5): print("%s is producing %d to the queue!" % (self.getName(), i)) self.data.put(i) time.sleep(random.randrange(10)/5) print("%s finished!" % self.getName())#消费者类class Consumer(threading.Thread): def __init__(self,name,queue): threading.Thread.__init__(self,name=name) self.data=queue def run(self): for i in range(5): val = self.data.get() print("%s is consuming. %d in the queue is consumed!" % (self.getName(),val)) time.sleep(random.randrange(10)) print("%s finished!" % self.getName())def main(): queue = Queue() producer = Producer('Producer',queue) consumer = Consumer('Consumer',queue) producer.start() consumer.start() producer.join() consumer.join() print 'All threads finished!'if __name__ == '__main__': main()
1 0
- 生产者消费者之Java/python实现
- 生产者消费者问题之Java线程实现
- 多线程编程之生产者消费者(java实现)
- 生产者消费者java实现
- Java实现生产者消费者
- 生产者消费者JAVA实现
- 生产者消费者Java实现
- java实现生产者消费者
- java实现生产者消费者
- 生产者消费者 Java 实现
- 生产者、消费者问题,python实现
- python用于实现多线程异步交互之生产者消费者模型
- 生产者消费者JAVA队列实现
- Java 实现生产者消费者(二)
- 生产者-消费者问题【Java实现】
- java实现生产者消费者模型
- Java 实现生产者和消费者
- java生产者消费者的实现
- spring mvc 扫描注解(ioc,di)
- SetWindowPos
- src与href区别
- spring 注入 动态注入
- windows mysql 5.7.18 解压版安装配置过程
- 生产者消费者之Java/python实现
- shrio动态修改权限,解决重启
- maven工程出问题
- C++著名库的比较和使用经验
- 使用vl_sift提取图像特征
- mysql存储引擎
- System.exit(0)和System.exit(1)
- jdk,cglib代理
- 解决xml sql多层()不能解析问题