一个生产者和一个消费者
2006-12-21 18:09
295 查看
我想开发一个可以允许最多有3个物品的 生产者 消费者 例子
说明的是生产者,消费者都只有一个
1。代表商店店员
public class Clerk {
private int product = 0;
////////////////////////
///////////////////////
// 这个方法是由Producer调用
///////////////////////
//////////////////////
public synchronized void setProduct() {
if(this.product>=3) {
try {
// 店里货价已经慢了,請稍候!
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
this.product++;
System.out.printf("现在共有商品 (%d)%n个", this.product);
// 通知等待區中的一個消費者可以繼續买商品了
notify();
}
// 這個方法由Consumer调用
public synchronized int getProduct() {
if(this.product<=0) {
try {
// 货价空了,請稍候!
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
this.product--;
System.out.printf(
"现在还剩下商品 (%d)%n个", this.product);
// 通知等待區中的一個生產者可以繼續生产商品
notify();
return this.product;
}
}
2。生产者
public class Producer implements Runnable {
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
public void run() {
System.out.println(
"生產者開始生產整數......");
// 生產1到10的整數
for(int product = 1; product <= 10; product++) {
try {
// 暫停隨機時間
Thread.sleep((int) Math.random() * 3000);
}
catch(InterruptedException e) {
e.printStackTrace();
}
// 將產品交給店員
clerk.setProduct();
}
}
}
3。消费者
public class Consumer implements Runnable {
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
public void run() {
System.out.println(
"消費者開始消耗整數......");
// 消耗10個整數
for(int i = 1; i <= 10; i++) {
try {
// 等待隨機時間
Thread.sleep((int) (Math.random() * 3000));
}
catch(InterruptedException e) {
e.printStackTrace();
}
// 從店員處取走整數
clerk.getProduct();
}
}
}
4。main调用程序
public class ProduceConsumerDemo {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Thread producerThread = new Thread(
new Producer(clerk));
Thread consumerThread = new Thread(
new Consumer(clerk));
producerThread.start();
consumerThread.start();
}
}
说明的是生产者,消费者都只有一个
1。代表商店店员
public class Clerk {
private int product = 0;
////////////////////////
///////////////////////
// 这个方法是由Producer调用
///////////////////////
//////////////////////
public synchronized void setProduct() {
if(this.product>=3) {
try {
// 店里货价已经慢了,請稍候!
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
this.product++;
System.out.printf("现在共有商品 (%d)%n个", this.product);
// 通知等待區中的一個消費者可以繼續买商品了
notify();
}
// 這個方法由Consumer调用
public synchronized int getProduct() {
if(this.product<=0) {
try {
// 货价空了,請稍候!
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
this.product--;
System.out.printf(
"现在还剩下商品 (%d)%n个", this.product);
// 通知等待區中的一個生產者可以繼續生产商品
notify();
return this.product;
}
}
2。生产者
public class Producer implements Runnable {
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
public void run() {
System.out.println(
"生產者開始生產整數......");
// 生產1到10的整數
for(int product = 1; product <= 10; product++) {
try {
// 暫停隨機時間
Thread.sleep((int) Math.random() * 3000);
}
catch(InterruptedException e) {
e.printStackTrace();
}
// 將產品交給店員
clerk.setProduct();
}
}
}
3。消费者
public class Consumer implements Runnable {
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
public void run() {
System.out.println(
"消費者開始消耗整數......");
// 消耗10個整數
for(int i = 1; i <= 10; i++) {
try {
// 等待隨機時間
Thread.sleep((int) (Math.random() * 3000));
}
catch(InterruptedException e) {
e.printStackTrace();
}
// 從店員處取走整數
clerk.getProduct();
}
}
}
4。main调用程序
public class ProduceConsumerDemo {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Thread producerThread = new Thread(
new Producer(clerk));
Thread consumerThread = new Thread(
new Consumer(clerk));
producerThread.start();
consumerThread.start();
}
}
相关文章推荐
- 笔试题11:编写一个生产者与消费者的多线程例子(源码)
- 实现一个通用的生产者消费者队列(c语言版本)
- 生产者和消费者。举一个寄信的例子
- Java 多线程学习之生产者消费者模型:一个较完善的实现
- 一个基于生产者消费者原理的日志系统
- java中用一个MyNumber类充当“生产者—消费者”模式的缓冲池实例
- 生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,它不断从枪膛中射出子弹
- 一个线程作为生产者,一个线程作为消费者。生产者每生产一次,消费者就消费一次。生产者每次生产一定数量的商品,加上上次消费剩余的数量,而总数量不超过1000;总感觉代码不是很完善,求指教??
- 一个简答的生产者-消费者多线程模型
- 一个生产者一个消费者一个缓冲器
- Java多线程的一个案例-生产者消费者问题
- 珍藏好料开源放送: windows平台一个高性能、通用型的C++生产者/消费者架构模板
- 生产者/消费者模式(阻塞队列) 一个类似于监听者模式的并发模型
- 笔试题11:编写一个生产者与消费者的多线程例子(源码)
- 生产者消费者的一个面试问题
- 一个生产者一个消费者的无锁队列,多个生产者多个消费者的无锁队列
- Java线程(学习整理)--4---一个简单的生产者、消费者模型
- Java 写一个生产者和消费者的多线程Demo
- Dubbo+Zookeeper 一个消费者对多个生产者的配置流程
- 一个典型的生产者和消费者的题目和实现