生产者与消费者的例子,是学习Java多线程不得不看的了经典例子。在这个小小的例子中,几乎包括了所有多线程编程需要注意的方方面面。我们可以不断的修改这个代码,观察程序输出,提高对多线程调度的认识。
此时采用的是notify(),此时只会激活第一个进入wait堆栈的线程,输出结果如下:
当notify()修改为notifyAll()后,将激活wait堆栈中的所有线程,输出也会有所不同:
class SyncStack {
private int index = 0;
private char[] buffer = new char[6];
public synchronized void push(char c) {
//栈满,该方法将处于wait状态
while (index == buffer.length) {
try {
System.out.println(Thread.currentThread().getName()
+ " wait...");
wait();
System.out.println(Thread.currentThread().getName()
+ " isAlive!");
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()
+ " InterruptedException");
}
}
buffer[index] = c;
index++;
System.out.println(Thread.currentThread().getName() + " 生成了:" + c);
notify();
}
public synchronized char pop() {
//栈空,该方法将处于wait状态
while (index == 0) {
try {
System.out.println(Thread.currentThread().getName()
+ " wait...");
wait();
System.out.println(Thread.currentThread().getName()
+ " isAlive!");
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()
+ " InterruptedException");
}
}
index--;
char c = buffer[index];
System.out.println(Thread.currentThread().getName() + " 消费了: " + c);
notify();
return c;
}
}
/**
* 生产者,生成字符
*/
class Producer implements Runnable {
SyncStack theStack;
public Producer(SyncStack s) {
theStack = s;
}
public void run() {
char c;
for (int i = 0; i < 20; i++) {
c = (char) (Math.random() * 26 + 'A');// 生成A-Z
theStack.push(c);
try {
Thread.sleep((int) (Math.random() * 1000));// 随机休眠0-1000毫秒
} catch (InterruptedException e) {
}
}
}
}
/**
* 消费者,获取字符并打印
*/
class Consumer implements Runnable {
SyncStack theStack;
public Consumer(SyncStack s) {
theStack = s;
}
public void run() {
char c;
for (;;) {
c = theStack.pop();
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
}
}
public class SyncTest {
public static void main(String arguments[]) throws Exception {
SyncStack stack = new SyncStack();
Thread producerThread1 = new Thread(new Producer(stack), "producerThread1");
Thread producerThread2 = new Thread(new Producer(stack), "producerThread2");
Thread consumerThread1 = new Thread(new Consumer(stack), "consumerThread1");
Thread consumerThread2 = new Thread(new Consumer(stack), "consumerThread2");
consumerThread1.start();
consumerThread2.start();
// 暂停3秒,造成饥饿状态
Thread.sleep(3000);
producerThread1.start();
producerThread2.start();
}
}
此时采用的是notify(),此时只会激活第一个进入wait堆栈的线程,输出结果如下:
consumerThread1 wait...
consumerThread2 wait... //此时两个消费者都在处于wait堆栈
producerThread1 生成了:W //此时producerThread1生成字符W,并调用notify,总是将第一个进入wait堆栈的线程激活,即consumerThread1
consumerThread1 isAlive! //consumerThread1 被激活,离开wait堆栈,此时wait堆栈仅剩consumerThread2
consumerThread1 消费了: W //此时调用的notify,将激活wait堆栈中仅剩的consumerThread2
consumerThread2 isAlive!
consumerThread2 wait...//consumerThread2 被激活,但是由于没有读到字符,又进入wait堆栈中
producerThread2 生成了:E
consumerThread2 isAlive!
consumerThread2 消费了: E
consumerThread1 wait...
producerThread2 生成了:A
consumerThread1 isAlive!
consumerThread1 消费了: A
producerThread2 生成了:D
consumerThread2 消费了: D
consumerThread2 wait...
producerThread1 生成了:I
consumerThread2 isAlive!
consumerThread2 消费了: I
...
当notify()修改为notifyAll()后,将激活wait堆栈中的所有线程,输出也会有所不同:
consumerThread1 wait...
consumerThread2 wait... //两个消费者都进入wait堆栈
producerThread1 生成了:Z//生成了Z,并调用notifyAll(),将把consumerThread1 ,consumerThread2 同时激活
consumerThread2 isAlive!
consumerThread2 消费了: Z//注意,此时生成字符Z后,也调用了notifyAll(),其实这里如果不调用notifyAll(),处于wait堆栈的consumerThread1 也会被上一句中的notifyAll()激活
consumerThread1 isAlive!
consumerThread1 wait...
producerThread2 生成了:A
consumerThread1 isAlive!
consumerThread1 消费了: A
producerThread1 生成了:M
producerThread2 生成了:C
producerThread1 生成了:P
consumerThread1 消费了: P
consumerThread2 消费了: C
producerThread2 生成了:B
consumerThread2 消费了: B
producerThread1 生成了:K
consumerThread1 消费了: K
...