Java Thread Producer/Consumer
2015-04-19 19:20
381 查看
The producer/consumer is a typical thread synchronization problem that uses the wait() and notify() methods.
There are four classes: Buffer, Producer, Consumer, and ProducerConsumerTest.
An object of the Buffer class has an integer data element that will be produced by the producer and consumed by the consumer.
We have to synchronize the access to the buffer, so the Producer produces a new data element only when the Buffer is empty and the Consumer consumes the buffer's data only when it is available.
The ProducerConsumerTest class is used to test the program.
Example
There are four classes: Buffer, Producer, Consumer, and ProducerConsumerTest.An object of the Buffer class has an integer data element that will be produced by the producer and consumed by the consumer.
We have to synchronize the access to the buffer, so the Producer produces a new data element only when the Buffer is empty and the Consumer consumes the buffer's data only when it is available.
The ProducerConsumerTest class is used to test the program.
import java.util.Random; /*from w w w .j a va 2 s . com*/ class Consumer extends Thread { private Buffer buffer; public Consumer(Buffer buffer) { this.buffer = buffer; } public void run() { int data; while (true) { data = buffer.consume(); } } } public class Main { public static void main(String[] args) { Buffer buffer = new Buffer(); Producer p = new Producer(buffer); Consumer c = new Consumer(buffer); p.start(); c.start(); } } class Producer extends Thread { private Buffer buffer; public Producer(Buffer buffer) { this.buffer = buffer; } public void run() { Random rand = new Random(); while (true) { int n = rand.nextInt(); buffer.produce(n); } } } class Buffer { private int data; private boolean empty; public Buffer() { this.empty = true; } public synchronized void produce(int newData) { while (!this.empty) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.data = newData; this.empty = false; this.notify(); System.out.println("Produced:" + newData); } public synchronized int consume() { while (this.empty) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.empty = true; this.notify(); System.out.println("Consumed:" + data); return data; } }
相关文章推荐
- Java:多线程Producer-Consumer的多种实现
- [置顶] Java多线模式-Producer-Consumer模式(生产者、消费者模式)
- Java多线程:Producer-Consumer不同的几种实现方式
- Java中实现Consumer-Producer
- java160110ProducerConsumerDemo
- Java producer-consumer(生产者/消费者模式)
- java基础之线程同步实例之Producer与Consumer
- java160110ProducerConsumerDemo2
- 【Java多线程】之十一:Java BlockingQueue Example implementing Producer Consumer Problem
- kafka java producer consumer实践
- 安装部署(六) Kafka集群安装部署以及Producer和Consumer的JAVA代码测试
- JAVA_Kafka_producer_consumer
- Java第七课 Java的多线程程序进程和线程的概念,实现多线程的两种方式,线程同步的原理,线程的死锁,运用wait和notify来实现producer - consumer关系,线程终止的两种情况。
- java--kafka编程实例--producer和consumer
- Java菜鸟入门(20) Producer Consumer经典代码
- golang for thread channel routine consumer and producer
- java 线程 ProducerAndConsumer
- producer consumer java代码
- java 线程 ProducerAndConsumer
- Java Lock and Condition Example using Producer Consumer Solution