多线程模拟producer和consumer问题
2012-10-31 19:45
162 查看
package com.dijkstra;
public class Buffer {
private int contents;//
private boolean avaliable = false;
public synchronized int get() {
while (!avaliable) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int value = contents;
avaliable = false;
System.out.println("取出:\t" + contents);
this.notifyAll();
return value;
}
public synchronized void put(int value) {
while (avaliable) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
contents = value;
avaliable = true;
System.out.println("存入\t" + contents);
this.notifyAll();
}
}
=======================================================
package com.dijkstra;
public class Producer extends Thread {
private Buffer buf;
private String name;
public Producer(Buffer buf, String name) {
super();
this.buf = buf;
this.name = name;
}
public void run() {
for (int i = 0;;) {
buf.put(i);
System.out.println("生产者" + name + "生产" + i++);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
===================================================
package com.dijkstra;
public class Consumer extends Thread {
private Buffer buf;
private int number;
public Consumer(Buffer buf, int number) {
// super();
this.buf = buf;
this.number = number;
}
public void run() {
for (;;) {
//
int v = buf.get();
System.out.println("消费者" + number + "消费" + v);
}
}
}
===============================================================
package com.dijkstra;
public class ProducerConsumerProblem {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Buffer buf = new Buffer();
new Producer(buf, "\tprducer1\t").start();
new Consumer(buf, 1).start();
new Consumer(buf, 2).start();
new Consumer(buf, 3).start();
}
}
=================================================
运行结果:
存入 0
生产者 prducer1 生产0
取出: 0
消费者2消费0
存入 1
生产者 prducer1 生产1
取出: 1
消费者3消费1
存入 2
生产者 prducer1 生产2
取出: 2
消费者1消费2
存入 3
生产者 prducer1 生产3
取出: 3
消费者1消费3
存入 4
取出: 4
消费者2消费4
生产者 prducer1 生产4
存入 5
生产者 prducer1 生产5
取出: 5
消费者2消费5....................
public class Buffer {
private int contents;//
private boolean avaliable = false;
public synchronized int get() {
while (!avaliable) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int value = contents;
avaliable = false;
System.out.println("取出:\t" + contents);
this.notifyAll();
return value;
}
public synchronized void put(int value) {
while (avaliable) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
contents = value;
avaliable = true;
System.out.println("存入\t" + contents);
this.notifyAll();
}
}
=======================================================
package com.dijkstra;
public class Producer extends Thread {
private Buffer buf;
private String name;
public Producer(Buffer buf, String name) {
super();
this.buf = buf;
this.name = name;
}
public void run() {
for (int i = 0;;) {
buf.put(i);
System.out.println("生产者" + name + "生产" + i++);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
===================================================
package com.dijkstra;
public class Consumer extends Thread {
private Buffer buf;
private int number;
public Consumer(Buffer buf, int number) {
// super();
this.buf = buf;
this.number = number;
}
public void run() {
for (;;) {
//
int v = buf.get();
System.out.println("消费者" + number + "消费" + v);
}
}
}
===============================================================
package com.dijkstra;
public class ProducerConsumerProblem {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Buffer buf = new Buffer();
new Producer(buf, "\tprducer1\t").start();
new Consumer(buf, 1).start();
new Consumer(buf, 2).start();
new Consumer(buf, 3).start();
}
}
=================================================
运行结果:
存入 0
生产者 prducer1 生产0
取出: 0
消费者2消费0
存入 1
生产者 prducer1 生产1
取出: 1
消费者3消费1
存入 2
生产者 prducer1 生产2
取出: 2
消费者1消费2
存入 3
生产者 prducer1 生产3
取出: 3
消费者1消费3
存入 4
取出: 4
消费者2消费4
生产者 prducer1 生产4
存入 5
生产者 prducer1 生产5
取出: 5
消费者2消费5....................
相关文章推荐
- JAVA多线程(七)模式-Producer Consumer
- java多线程模拟生产者消费者问题
- 多线程设计模式之Producer Consumer模式
- 线程问题:生产者(Producer)与消费者(Consumer)
- Linux下多线程模拟生产者/消费者问题
- java多线程模拟停车位问题
- Java第七课 Java的多线程程序进程和线程的概念,实现多线程的两种方式,线程同步的原理,线程的死锁,运用wait和notify来实现producer - consumer关系,线程终止的两种情况。
- JAVA多线程-线程池-实例模拟上厕所问题
- Java多线程模拟售票程序,及线程安全问题
- 多线程程序设计学习(6)Producer-Consumer模式
- java 编写多线程应用程序,模拟多个人通过独木桥的模拟。 线程问题
- 多线程模拟哲学家就餐问题
- java多线程模拟火车站窗口卖票的问题
- 问题记录: kafka producer.send(record) topic可以创建, 确无法从kafka-console-consumer中查看到此topic上的内容
- Java学习--多线程案例--模拟火车票销售(线程安全问题)
- producer-consumer问题的分析和讨论
- 译 -- Java 并发编程(多线程)二 | Producer-Consumer | Get status of all threads | deadlocked
- Kafka的Producer以及Consumer远程调用问题
- Java: 简单模拟多线程访问同样变量导致的问题
- Producer consumer problem - 生产者消费者问题