【重点】生产者消费者(设置flag标记:最佳实践)多生产者多消费者(把if换成while)
2013-10-20 18:49
316 查看
生产者消费者问题(为什么要设置flag标记:类似一种编程模式)
package com.xiaozhi.threadinformation; /* * 生产者消费者 */ public class Test3 { public static void main(String[] args) { Resource resource=new Resource(); Producer producer=new Producer(resource); Consumer consumer=new Consumer(resource); Thread thread1=new Thread(producer); Thread thread2=new Thread(consumer); thread1.start(); thread2.start(); } } class Resource{ private int num; private boolean flag=false; public synchronized void input() { if(flag) try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} num++; System.out.println("商品生产-----------------"+num); flag=true; this.notify(); } public synchronized void output() { if(!flag) try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} System.out.println("商品消费--------------------------"+num); flag=false; this.notify(); } } class Producer implements Runnable{ private Resource resource; public Producer(Resource resource) { super(); this.resource = resource; } @Override public void run() { while(true) resource.input(); } } class Consumer implements Runnable{ private Resource resource; public Consumer(Resource resource) { super(); this.resource = resource; } @Override public void run() { while(true) resource.output(); } }多生产者多消费者问题(为什么要把if改为while:当唤醒同方线程时,再次判断)
package com.xiaozhi.procon; /* * 多生产者多消费者 */ public class Test { public static void main(String[] args) { Resource resource=new Resource(); Producer producer1=new Producer(resource); Producer producer2=new Producer(resource); Consumer consumer1=new Consumer(resource); Consumer consumer2=new Consumer(resource); Thread thread1=new Thread(producer1); Thread thread2=new Thread(producer2); Thread thread3=new Thread(consumer1); Thread thread4=new Thread(consumer2); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } } class Resource{ private int num; private boolean flag=false; public synchronized void input() { while(flag) try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} num++; System.out.println("商品生产-----------------"+num); flag=true; this.notifyAll(); } public synchronized void output() { while(!flag) try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} System.out.println("商品消费--------------------------"+num); flag=false; this.notifyAll(); } } class Producer implements Runnable{ private Resource resource; public Producer(Resource resource) { super(); this.resource = resource; } @Override public void run() { while(true) resource.input(); } } class Consumer implements Runnable{ private Resource resource; public Consumer(Resource resource) { super(); this.resource = resource; } @Override public void run() { while(true) resource.output(); } }
相关文章推荐
- java生产者消费者PV操作为中,wait判断条件为何是while不是if疑惑解答
- Linux多线程实践(5) --Posix信号量与互斥量解决生产者消费者问题
- 神州数码最佳实践之三:以项目为中心的结构设置
- 【重点】JDK5新特性lock和condition(多生产者多消费者:可以用来区分本方线程和对方线程)
- Linux多线程实践(8) --Posix条件变量解决生产者消费者问题
- Java实践之路——多线程之生产者消费者模型
- JSP 最佳实践:为您的JSP标记创建自定义属性
- 多线程实践-生产者消费者
- Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据
- Windows 2000域控制器设置的最佳实践方法
- CentOS 6.X设置环境时间最佳实践 推荐
- Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据
- (for+if) 循环最佳实践推导
- 生产者--消费者实践
- Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt)
- Linux多线程实践(五 )Posix信号量和互斥锁解决生产者消费者问题
- java个人学习笔记19(多生产者多消费者+循环判断标记+notifyAll()+Lock+Condition)
- Linux多线程实践(8) --Posix条件变量解决生产者消费者问题
- Django 1.6 最佳实践: 最佳Django开发环境设置
- 神州数码最佳实践之三:以项目为中心的结构设置