/* 消费者生产者线程演示
* Coding by ZhaoHongliang(赵洪亮).查看原文
**/
public class ProducerConsumer {
public static void main(String[] args) {
Container container = new Container();
Producer Alice = new Producer("Alice",container);
Thread produceThread1 = new Thread(Alice);
produceThread1.start();
Producer Bob = new Producer("Bob",container);
Thread produceThread2 = new Thread(Bob);
produceThread2.start();
Producer Lucy = new Producer("Lucy",container);
Thread produceThread3 = new Thread(Lucy);
produceThread3.start();
Consumer James = new Consumer("James",container);
Thread consumeThread1 = new Thread(James);
consumeThread1.start();
Consumer Charlie = new Consumer("Charlie",container);
Thread consumeThread2 = new Thread(Charlie);
consumeThread2.start();
}
}
/* 生产者 */
class Producer implements Runnable {
private String name;
private Container container = null;
//Constructor
public Producer(String producerName,Container container) {
this.name = producerName;
this.container = container;
}
//Override abstract methoed of Interface Runnable.
public void run() {
while(true) {
synchronized(this.container) {
while(!this.container.isEmpty()) {
try {
this.container.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
Product pushedProduct = new Product(this.name);
container.push(pushedProduct);
try {
Thread.sleep((int)(Math.random()*2000));
}
catch (InterruptedException e) {
e.printStackTrace();
}
this.container.notifyAll();
}
}
}
}
/* 消费者 */
class Consumer implements Runnable {
private String name;
private Container container = null;
//Constructor
public Consumer(String consumerName,Container container) {
this.name = consumerName;
this.container = container;
}
//Override abstract methoed of Interface Runnable.
public void run() {
while(true) {
synchronized (this.container) {
while(this.container.isEmpty()) {
try {
this.container.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
Product popedProduct = container.pop(name);
try {
Thread.sleep((int)(Math.random()*1000));
}
catch (InterruptedException e) {
e.printStackTrace();
}
this.container.notifyAll();
}
}
}
}
/* 产品 */
class Product {
private static int id = 0;
//Constructor
private String producerName;
public Product(String producerName) {
this.producerName = producerName;
this.id ++;
}
//Override method of Class Object.
public String toString() {
return "Product No."+id+"; Made by "+producerName;
}
}
/* 容器 只允许放一个产品 */
class Container {
private Product container = null;
//Test if the container is empty.
public boolean isEmpty() {
if (this.container==null) {
return true;
}
else {
return false;
}
}
public synchronized void push(Product product) {
if (this.isEmpty()) {
this.container = product;
System.out.println("Pushed: "+product);
}
else {
throw new RuntimeException("Can't push! The container is full.");
}
}
public synchronized Product pop(String consumerName) {
if (!this.isEmpty()) {
Product temp = this.container;
this.container = null;
System.out.println(consumerName+" poped: "+temp);
return temp;
}
else {
throw new RuntimeException("Can't pop! The container is empty.");
}
}
}来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21468125/viewspace-609322/ ,如需转载,请注明出处,否则将追究法律责任。