Java Monitor Object在生产者消费者问题中的应用
2011-12-19 11:48
295 查看
直接上代码
package javaL.homework; import java.util.ArrayList; import java.util.List; /** * Producer and Consumer problem * @author yaolei * */ public class ProducerAndConsumer { public static void main(String[] args) { PCMonitor monitor = new PCMonitor();//monitor object Thread p1 = new Thread(new Producer(1, monitor)); Thread p2 = new Thread(new Producer(2, monitor)); Thread p3 = new Thread(new Producer(3, monitor)); Thread c1 = new Thread(new Consumer(1, monitor)); Thread c2 = new Thread(new Consumer(2, monitor)); Thread c3 = new Thread(new Consumer(3, monitor)); //start threads p1.start(); p2.start(); p3.start(); c1.start(); c2.start(); c3.start(); } } class Producer implements Runnable { private PCMonitor mtr = null;//the object which contains sharing resource private int producerNo; // the number of the Producer public Producer(int no, PCMonitor mtr) { this.mtr = mtr; this.producerNo = no; } @Override public void run() { // TODO Auto-generated method stub int productNo = 0; while (true) { Product p = new Product(producerNo,productNo++); try { mtr.put(p); System.out.println("Producer " + producerNo + " is producing product[" + p + "]"); Thread.sleep(1000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class Consumer implements Runnable { private PCMonitor mtr = null; private int consumerNo; public Consumer(int no, PCMonitor mtr) { this.mtr = mtr; this.consumerNo = no; } @Override public void run() { // TODO Auto-generated method stub while (true) { try { Product p = mtr.get(); System.out.println("Consumer " + consumerNo + " is consuming product[" + p + "]"); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } } class PCMonitor { private List<Product> items = new ArrayList<Product>(); final static int MAX_COUNT = 20;//The max number of products in queue public synchronized void put(Product item) throws Exception { while (items.size()>=20)//while condition wait(); items.add(item); notifyAll();// notify(); } public synchronized Product get() throws Exception { while (items.isEmpty()) //while condition wait(); Product item = items.remove(0); notifyAll();//notify() return item; } } class Product { private int id; private int producerId; public Product(int producerId,int id) { super(); this.id = id; this.producerId = producerId; } public String toString() { return producerId+":"+id; } }
相关文章推荐
- java生产者消费者问题(线程同步与线程等待的应用)
- java生产者消费者问题(线程同步与线程等待的应用)
- java生产者消费者问题(线程同步与线程等待的应用)
- java生产者消费者问题(线程同步与线程等待的应用)
- 生产者-消费者问题【Java实现】
- Java 多线程:生产者消费者问题
- 哲学家进餐问题与生产者与消费者问题(java实现)
- Java实现生产者消费者问题
- Java线程(三):线程协作-生产者/消费者问题
- Java并发之:生产者消费者问题
- java 线程间通信,多生产者多消费者问题
- 生产者-消费者问题【Java实现】
- java再复习——线程的经典问题-生产者消费者
- 生产者/消费者问题的多种Java实现方式
- 生产者/消费者问题的多种Java实现方式
- JAVA多线程实例(生产者与消费者问题)
- 生产者/消费者问题的多种Java实现方式
- Java线程通信练习之“生产者/消费者问题”
- [Java] 生产者&消费者问题
- Java中简单的生产者和消费者问题