我有一个代码,其中两个实现Runnable的线程类正在运行并共享Buffer类的公共对象。
虽然我已经使用synchronized块和wait()
和notify()
正确的方法,我的知识范围之内,但是当我睡觉的线程与Thread.sleep(0)
在try / catch块中,一些公认的输出后,导致它进入死锁。
当我以1000ms睡眠线程CookThread.sleep(1000)
和以3000ms睡眠线程Bheem睡眠时,未检测到问题Thread.sleep(3000)
这是线程中常见的生产者-消费者方案,我有意将消费者置于生产者之上。
PS-我给了从最后一个修剪的输出。实际产量很大。
下面的代码在Bheem类中
public void consume() {
while (ob.buffer >= 1) {
synchronized (ob) {
System.out.println(Thread.currentThread().getName()
+ " started eating Ladoos with currently "
+ (ob.buffer--) + " ladoos in plate");
try {
Thread.sleep(0); // bheem takes 1.5 sec to eat
ob.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (ob) {
try {
System.out
.println("Plate is empty, bheem will wait for ladoos to serve ");
ob.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void run() {
while (true) {
consume();
}
}
这是库克班的
public void produce() {
while (ob.buffer < 5) {
synchronized (ob) {
System.out.println(Thread.currentThread().getName()
+ " started making Ladoos with currently "
+ (ob.buffer++) + " ladoos in plate");
try {
Thread.sleep(0); // 1 sec time taken to make a ladoo
ob.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (ob) {
try {
System.out.println("Plate is full, cook will wait ");
ob.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void run() {
while (true) {
produce();
}
}
这是为了了解观众而调用的主要方法
public static void main(String[] args) {
Buffer b = new Buffer(0);
Producer p = new Producer(b);
Consumer c = new Consumer(b);
Thread producer = new Thread(p, "Cook");
Thread consumer = new Thread(c, "Bheem");
System.out.println("Main started");
// buffer size is taken as 5 max.
consumer.start(); // bheem takes 1.5 sec to finish a ladoo
/*try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
producer.start(); // cook takes 1 sec to prepare ladoo
输出
Plate is empty, bheem will wait for ladoos to serve
Cook started making Ladoos with currently 0 ladoos in plate
Bheem started eating Ladoos with currently 1 ladoos in plate
Plate is empty, bheem will wait for ladoos to serve
Cook started making Ladoos with currently 0 ladoos in plate
Bheem started eating Ladoos with currently 1 ladoos in plate
Plate is empty, bheem will wait for ladoos to serve
Cook started making Ladoos with currently 0 ladoos in plate
Bheem started eating Ladoos with currently 1 ladoos in plate
Plate is empty, bheem will wait for ladoos to serve
Cook started making Ladoos with currently 0 ladoos in plate
Bheem started eating Ladoos with currently 1 ladoos in plate
Cook started making Ladoos with currently 0 ladoos in plate
Cook started making Ladoos with currently 1 ladoos in plate
Cook started making Ladoos with currently 2 ladoos in plate
Cook started making Ladoos with currently 3 ladoos in plate
Cook started making Ladoos with currently 4 ladoos in plate
Plate is full, cook will wait
Plate is empty, bheem will wait for ladoos to serve
当消费者拿到最后一个拉杜时,是什么阻止您的生产者在消费者设法打电话notify()
之前完全填满盘子并打电话给最后一个wait()
?同样,当盘子装满时,是什么阻止了消费者notify()
在生产者进入之前拿走最后一个拉杜并打电话给最后一个wait()
呢?
如果某个其他线程不等待接收通知,则对的调用o.notify()
不执行任何操作。也就是说,对象o
不记得它已被通知。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句