Producer & Consumer
2009-10-16 22:47
141 查看
/**
*
*/
package mythread;
/**
* @author daniel zhou
* 演示生产者、消费者模型
*/
public class WaitAndNotify {
/**
* @param args
*/
public static void main(String[] args) {
// 定义一个储物罐
Queue q = new Queue();
// 生产者
producer p = new producer(q);
// 消费者
consumer c = new consumer(q);
// 开始生产、消费过程
p.start();
c.start();
System.gc();
}
}
/**
*
* @author daniel zhou 储物罐,用作存储生产者的产品
*/
class Queue {
// 产品编号
int value;
// 有无产品标示
boolean flag = false;
// 生产
public synchronized void put(int i) {
// 为空则放置,并通知消费者去取产品,自己则开始等待
if (!flag ) {
value = i;
flag = true;
// 通知消费者
notify();
}
// 开始等待
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 消费
public synchronized int get() {
// 有产品则取,并标示已经置空,通知生产者放置,自己开始等待
if (!flag ) {
// 开始等待
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 通知生产者并返回产品
flag = false;
notify();
return value;
}
}
/**
*
* @author daniel zhou 消费者
*/
class consumer extends Thread {
Queue q;
public consumer(Queue q) {
this.q = q;
}
// 消费10个产品
public void run() {
//这个while必须要
while(true){
System.out.println("消费者消费了第" + q.get() + "个产品");
}
}
}
/**
*
* @author daniel zhou 生产者
*/
class producer extends Thread {
Queue q;
public producer(Queue q) {
this.q = q;
}
// 生产10个产品
public void run() {
for (int i = 0; i < 10; i++) {
q.put(i);
System.out.println("生产者放置了第" + i + "个产品");
}
}
}
*
*/
package mythread;
/**
* @author daniel zhou
* 演示生产者、消费者模型
*/
public class WaitAndNotify {
/**
* @param args
*/
public static void main(String[] args) {
// 定义一个储物罐
Queue q = new Queue();
// 生产者
producer p = new producer(q);
// 消费者
consumer c = new consumer(q);
// 开始生产、消费过程
p.start();
c.start();
System.gc();
}
}
/**
*
* @author daniel zhou 储物罐,用作存储生产者的产品
*/
class Queue {
// 产品编号
int value;
// 有无产品标示
boolean flag = false;
// 生产
public synchronized void put(int i) {
// 为空则放置,并通知消费者去取产品,自己则开始等待
if (!flag ) {
value = i;
flag = true;
// 通知消费者
notify();
}
// 开始等待
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 消费
public synchronized int get() {
// 有产品则取,并标示已经置空,通知生产者放置,自己开始等待
if (!flag ) {
// 开始等待
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 通知生产者并返回产品
flag = false;
notify();
return value;
}
}
/**
*
* @author daniel zhou 消费者
*/
class consumer extends Thread {
Queue q;
public consumer(Queue q) {
this.q = q;
}
// 消费10个产品
public void run() {
//这个while必须要
while(true){
System.out.println("消费者消费了第" + q.get() + "个产品");
}
}
}
/**
*
* @author daniel zhou 生产者
*/
class producer extends Thread {
Queue q;
public producer(Queue q) {
this.q = q;
}
// 生产10个产品
public void run() {
for (int i = 0; i < 10; i++) {
q.put(i);
System.out.println("生产者放置了第" + i + "个产品");
}
}
}
相关文章推荐
- Application Design Patterns: Producer/Consumer
- ProtoThreads - Producer&Consumer
- Producer/Consumer problem
- classic problem: producer and consumer
- 线程协作,producer and consumer.
- ProducerConsumer_8
- producer and consumer concept ( II )
- Java Thread Producer/Consumer
- 多线程程序设计学习(6)Producer-Consumer模式
- 【Java多线程】之十一:Java BlockingQueue Example implementing Producer Consumer Problem
- 安装部署(六) Kafka集群安装部署以及Producer和Consumer的JAVA代码测试
- Kafka系列3-python版本producer生产者和consumer消费者实例
- Python MultiProducer and MultiConsumer
- PECS(Producer Extends Consumer Super)原则
- linux下C语言实现多线程通信—环形缓冲区,可用于生产者(producer)/消费者(consumer)【转】
- Kafka学习总结(四)——broker、consumer、producer主要配置
- AWT使用ImageProducer/ImagConsumer模式加载和显示图像的原理(转)
- 多线程设计模式之Producer Consumer模式
- Design Pattern: Producer Consumer 模式
- java基础之线程同步实例之Producer与Consumer