疑问就是 调用notifyAll(); 唤醒不及时
问题比较细.希望给位跑跑下面的代码.经典的生存者和消费者例子.
根据注释屏蔽掉Producer中的run方法里面{}起来的代码块 对比输出日志的发现疑问所在.
public class ProducerConsumer {
public static void main(String[] args) {
ProductStack ps = new ProductStack();
Producer p = new Producer(ps, "生产者1");
Consumer c = new Consumer(ps, "消费者1");
new Thread(p).start();
new Thread(c).start();
}
}class Product {
int id; private String producedBy = "N/A"; private String consumedBy = "N/A";
Product(int id, String producedBy) {
this.id = id;
this.producedBy = producedBy;
}
public void consume(String consumedBy) {
this.consumedBy = consumedBy;
} public String toString() {
return "Product : " + id + ", produced by " + producedBy
+ ", consumed by " + consumedBy;
} public String getProducedBy() {
return producedBy;
} public void setProducedBy(String producedBy) {
this.producedBy = producedBy;
} public String getConsumedBy() {
return consumedBy;
} public void setConsumedBy(String consumedBy) {
this.consumedBy = consumedBy;
}}// 这个class就是仓库,是生产者跟消费者共同争夺控制权的同步资源
class ProductStack {
int index = 0; Product[] arrProduct = new Product[6];
public synchronized void push(Product product) {

while (index == arrProduct.length) // 这里本来可以用if(),但是如果catch
// exception会出问题,让满的index越界
{
try {
System.out.println(product.getProducedBy() + " is waiting.");

wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notifyAll();
try {
Thread.sleep((int) (500));
System.out.println("调用生产的当前线程ID" + Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(product.getProducedBy() + " sent a notifyAll().");
arrProduct[index] = product;
index++;
System.out.println(product.getProducedBy() + " 生产了: " + product); }
public synchronized Product pop(String consumerName) {

while (index == 0) {
try {
// here will be the consumer thread instance will be waiting ,
// because empty
System.out.println(consumerName + " is waiting.");
// 等待,并且从这里退出pop()
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} System.out.println(consumerName + " sent a notifyAll().");
// 因为我们不确定有没有线程在wait(),所以我们既然消费了产品,就唤醒有可能等待的生产者,让他们醒来,准备生产
notifyAll();
index--;
Product product = arrProduct[index];
product.consume(consumerName);
System.out.println(product.getConsumedBy() + " 消费了: " + product);
return product;
}
}class Producer implements Runnable {
String name; ProductStack ps = null; Producer(ProductStack ps, String name) {
this.ps = ps;
this.name = name;
} public void run() {
for (int i = 0; i < 12; i++) {
Product product = new Product(i, name);
ps.push(product);
{//疑问产生在这个代码块中.
// System.out.println("生产的当前线程ID"+Thread.currentThread().getId());//屏蔽掉这一句
try {
Thread.sleep((int) (0));//或者屏蔽掉这一句 } catch (InterruptedException e) {
e.printStackTrace();
}

}//如果屏蔽掉上面代码块中的代码 根据输出日志 可以发现唤醒消费者非常的不及时 要等待所有商品生产完成才能唤醒消费者.反之,如果不屏蔽 商品生产完成.消费者立马被唤醒;
}
}
}class Consumer implements Runnable {
String name; ProductStack ps = null; Consumer(ProductStack ps, String name) {
this.ps = ps;
this.name = name;
} public void run() {
for (int i = 0; i < 12; i++) {
ps.pop(name);
}
}
}
日志:
屏蔽代码块前
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 0, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 1, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 2, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 2, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 1, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 0, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 3, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 3, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 4, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 4, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 5, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 6, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 6, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 5, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 7, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 7, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 8, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 8, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 9, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 9, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 10, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 10, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 11, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 11, produced by 生产者1, consumed by 消费者1屏蔽后调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 0, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 1, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 2, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 3, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 4, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 5, produced by 生产者1, consumed by N/A
生产者1 is waiting.
消费者1 sent a notifyAll().
消费者1 消费了: Product : 5, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 4, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 3, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 2, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 1, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 0, produced by 生产者1, consumed by 消费者1
消费者1 is waiting.
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 6, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 7, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 8, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 9, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 10, produced by 生产者1, consumed by N/A
调用生产的当前线程ID8
生产者1 sent a notifyAll().
生产者1 生产了: Product : 11, produced by 生产者1, consumed by N/A
消费者1 sent a notifyAll().
消费者1 消费了: Product : 11, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 10, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 9, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 8, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 7, produced by 生产者1, consumed by 消费者1
消费者1 sent a notifyAll().
消费者1 消费了: Product : 6, produced by 生产者1, consumed by 消费者1thread&nbsp;wait线程同步