Java多线程: 生产者消费者问题(源码)
2009-05-23 18:19
369 查看
有人反映看不到源码,发现是CSDN的代码控件有问题。希望CSDN好好改进一下。
暂时先以文本方式发布出来,格式会变样。
import java.util.LinkedList; public class ProducerConsumer { /** * @param args */ public static void main(String[] args) { Queue queue = new Queue(); Producer producer1 = new Producer(queue, "producer1"); Producer producer2 = new Producer(queue, "producer2"); Consumer consumer1 = new Consumer(queue, "consumer1"); Consumer consumer2 = new Consumer(queue, "consumer2"); Consumer consumer3 = new Consumer(queue, "consumer3"); producer1.start(); producer2.start(); consumer1.start(); consumer2.start(); consumer3.start(); } } class Message { public static int sno = 0; public int id = 0; public String content = null; public Message(String content) { synchronized(Message.class) { this.id = ++sno; } this.content = content; } public String toString() { return "id = " + id + ", content = " + content; } } class Queue { private LinkedList queue = null; private int capacity = 0; public Queue(int capacity) { this.capacity = capacity; queue = new LinkedList(); } public Queue() { this(10); } public synchronized void put(Message product) { //wait until the consumer has consume a product while(queue.size() >= capacity) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } queue.addLast(product); // notify the Consumer that a message has been put in the queue notifyAll(); } public synchronized Message get() { while(queue.isEmpty()) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Message product = (Message)queue.removeFirst(); // notify the Producer that a message has been removed notifyAll(); return product; } public synchronized boolean isEmpty() { return queue.isEmpty(); } } class Producer extends Thread { private Queue queue = null; public Producer(Queue queue, String name) { this.queue = queue; setName(name); } public void run() { for(int i = 0; i < 10; i++) { try { sleep(100); } catch (Exception e) { e.printStackTrace(); } queue.put(produce()); } } public Message produce() { Message product = new Message("Made by " + getName()); System.out.println(this.getName() + " -- Produce: " + product); return product; } } class Consumer extends Thread { private Queue queue = null; public Consumer(Queue queue, String name) { this.queue = queue; this.setName(name); } public void run() { while(true) { try { sleep(100); } catch (Exception e) { e.printStackTrace(); } //when get a terminated message, stop the thread. if(!consume(queue.get())) { break; } } } public boolean consume(Message product) { System.out.println(this.getName() + " -- Consume: " + product); //terminated message. if(product == null || product.id == 20) { queue.put(null); return false; } return true; } }
暂时先以文本方式发布出来,格式会变样。
import java.util.LinkedList; public class ProducerConsumer { /** * @param args */ public static void main(String[] args) { Queue queue = new Queue(); Producer producer1 = new Producer(queue, "producer1"); Producer producer2 = new Producer(queue, "producer2"); Consumer consumer1 = new Consumer(queue, "consumer1"); Consumer consumer2 = new Consumer(queue, "consumer2"); Consumer consumer3 = new Consumer(queue, "consumer3"); producer1.start(); producer2.start(); consumer1.start(); consumer2.start(); consumer3.start(); } } class Message { public static int sno = 0; public int id = 0; public String content = null; public Message(String content) { synchronized(Message.class) { this.id = ++sno; } this.content = content; } public String toString() { return "id = " + id + ", content = " + content; } } class Queue { private LinkedList queue = null; private int capacity = 0; public Queue(int capacity) { this.capacity = capacity; queue = new LinkedList(); } public Queue() { this(10); } public synchronized void put(Message product) { //wait until the consumer has consume a product while(queue.size() >= capacity) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } queue.addLast(product); // notify the Consumer that a message has been put in the queue notifyAll(); } public synchronized Message get() { while(queue.isEmpty()) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Message product = (Message)queue.removeFirst(); // notify the Producer that a message has been removed notifyAll(); return product; } public synchronized boolean isEmpty() { return queue.isEmpty(); } } class Producer extends Thread { private Queue queue = null; public Producer(Queue queue, String name) { this.queue = queue; setName(name); } public void run() { for(int i = 0; i < 10; i++) { try { sleep(100); } catch (Exception e) { e.printStackTrace(); } queue.put(produce()); } } public Message produce() { Message product = new Message("Made by " + getName()); System.out.println(this.getName() + " -- Produce: " + product); return product; } } class Consumer extends Thread { private Queue queue = null; public Consumer(Queue queue, String name) { this.queue = queue; this.setName(name); } public void run() { while(true) { try { sleep(100); } catch (Exception e) { e.printStackTrace(); } //when get a terminated message, stop the thread. if(!consume(queue.get())) { break; } } } public boolean consume(Message product) { System.out.println(this.getName() + " -- Consume: " + product); //terminated message. if(product == null || product.id == 20) { queue.put(null); return false; } return true; } }
import java.util.LinkedList; public class ProducerConsumer { /** * @param args */ public static void main(String[] args) { Queue queue = new Queue(); Producer producer1 = new Producer(queue, "producer1"); Producer producer2 = new Producer(queue, "producer2"); Consumer consumer1 = new Consumer(queue, "consumer1"); Consumer consumer2 = new Consumer(queue, "consumer2"); Consumer consumer3 = new Consumer(queue, "consumer3"); producer1.start(); producer2.start(); consumer1.start(); consumer2.start(); consumer3.start(); } } class Message { public static int sno = 0; public int id = 0; public String content = null; public Message(String content) { synchronized(Message.class) { this.id = ++sno; } this.content = content; } public String toString() { return "id = " + id + ", content = " + content; } } class Queue { private LinkedList queue = null; private int capacity = 0; public Queue(int capacity) { this.capacity = capacity; queue = new LinkedList(); } public Queue() { this(10); } public synchronized void put(Message product) { //wait until the consumer has consume a product while(queue.size() >= capacity) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } queue.addLast(product); // notify the Consumer that a message has been put in the queue notifyAll(); } public synchronized Message get() { while(queue.isEmpty()) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Message product = (Message)queue.removeFirst(); // notify the Producer that a message has been removed notifyAll(); return product; } public synchronized boolean isEmpty() { return queue.isEmpty(); } } class Producer extends Thread { private Queue queue = null; public Producer(Queue queue, String name) { this.queue = queue; setName(name); } public void run() { for(int i = 0; i < 10; i++) { try { sleep(100); } catch (Exception e) { e.printStackTrace(); } queue.put(produce()); } } public Message produce() { Message product = new Message("Made by " + getName()); System.out.println(this.getName() + " -- Produce: " + product); return product; } } class Consumer extends Thread { private Queue queue = null; public Consumer(Queue queue, String name) { this.queue = queue; this.setName(name); } public void run() { while(true) { try { sleep(100); } catch (Exception e) { e.printStackTrace(); } //when get a terminated message, stop the thread. if(!consume(queue.get())) { break; } } } public boolean consume(Message product) { System.out.println(this.getName() + " -- Consume: " + product); //terminated message. if(product == null || product.id == 20) { queue.put(null); return false; } return true; } }
相关文章推荐
- java多线程总结六:经典生产者消费者问题实现
- Java多线程模拟生产者消费者问题
- java多线程——生产者消费者问题
- java多线程之生产者消费者经典问题
- java多线程之生产者与消费者问题的简单模拟
- Java多线程 生产者消费者问题 (2)
- Java多线程生产者消费者说明等待唤醒机制问题和虚假唤醒问题
- Java多线程(2)生产者消费者问题(一)
- 操作系统课程设计消费者和生产者问题源码解析
- Java多线程模拟实现消费者生产者问题
- Java多线程—生产者-消费者问题
- java多线程之生产者消费者经典问题
- java多线程之生产者消费者问题
- Java多线程--生产者与消费者问题
- java多线程模拟生产者消费者问题,公司面试经常问的题。。。
- 关于Java多线程实现生产者和消费者的问题
- java多线程之生产者消费者经典问题 - 很不错的范例
- java多线程(同步与死锁问题,生产者与消费者问题)
- java多线程总结六:经典生产者消费者问题实现
- Java多线程解决生产者消费者问题