将传统生产者消费者模式里的notify 换成了 notifyAll
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class ProducerModular {
public static void main(String[] args) throws InterruptedException {
MyShare share = new MyShare();
Thread provider = new MyProvider(share);
Thread bailing = new MyBailing(share, "baiLing one ");
Thread bailing2 = new MyBailing(share, "baiLing two ");
provider.setDaemon(true);
bailing.setDaemon(true);
bailing2.setDaemon(true);
System.out.println("0.01秒生产消费者模式");
bailing.start();
bailing2.start();
provider.start();
Thread.sleep((long) (0.01 * 1000));
provider.interrupt();// 中断结束线程
bailing.interrupt(); // 中断结束线程
bailing2.interrupt();// 中断结束线程
provider.join();
bailing.join();
bailing2.join();
System.out.println("--------------| 结束");
}
}
/**
* 生产者
*
* @author chengkai
*
*/
class MyProvider extends Thread {
private MyShare myshare;
public MyProvider(MyShare myshare) {
super();
this.myshare = myshare;
}
// private Thread cThread;
@Override
public void run() {
// this.cThread=Thread.currentThread();
try {
while (!Thread.currentThread().isInterrupted()) {
String str = "" + (char) this.random(1, 255);
myshare.charSetting(str);
// System.out.println("生产了 : "
// + str);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
return;
}
}
public int random(int start, int end) {
double result = Math.random() * (end - start + 1) + start;
return (int) result;
}
// public void toStop(){
//
// }
}
/**
* 消费者(白领)
*
* @author chengkai
*
*/
class MyBailing extends Thread {
private MyShare myshare;
public MyBailing(MyShare myshare, String name) {
super(name);
this.myshare = myshare;
}
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
String str = myshare.getCharVar();
// System.out.println(this.getName()+" 消费了: " + str);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
return;
}
}
}
/**
* 通道 //适用于1个生产者 ,多个消费者
*
* @author chengkai
*
*/
class MyShare {
List<String> shareList = Collections.synchronizedList(new LinkedList());
public synchronized void charSetting(String product)
throws InterruptedException {
while (shareList.size() != 0) {
this.wait();
}
this.push(product);
System.out.println(Thread.currentThread().getName()
+ " 生产了 ---| " + product);
this.notifyAll();
}
public synchronized String getCharVar() throws InterruptedException {
while (shareList.size() == 0) {
this.wait();
}
String str = this.popLast();
System.out.println(Thread.currentThread().getName() + " 消费了 ---| "
+ str);
this.notifyAll();
return str;
}
/*********************
* 设置和输出队列
*
* @param obj
*/
private void push(String obj) {
synchronized (shareList) {
shareList.add(0, obj);
}
}
private String popLast() {
synchronized (shareList) {
if (shareList.size() == 0) {
return null;
}
return shareList.remove(this.shareList.size() - 1);
}
}
}
输出结果:
0.01秒生产消费者模式
Thread-0 生产了 ---| ú
baiLing two 消费了 ---| ú
Thread-0 生产了 ---| Ò
baiLing one 消费了 ---| Ò
Thread-0 生产了 ---| 9
baiLing two 消费了 ---| 9
Thread-0 生产了 ---|
baiLing one 消费了 ---|
Thread-0 生产了 ---| ,
baiLing two 消费了 ---| ,
Thread-0 生产了 ---| i
baiLing one 消费了 ---| i
Thread-0 生产了 ---|
baiLing two 消费了 ---|
Thread-0 生产了 ---| l
baiLing one 消费了 ---| l
Thread-0 生产了 ---| »
baiLing two 消费了 ---| »
Thread-0 生产了 ---| «
baiLing one 消费了 ---| «
Thread-0 生产了 ---| ß
baiLing two 消费了 ---| ß
Thread-0 生产了 ---| |
baiLing one 消费了 ---| |
......