首先建了一个简单的Product类,用来表示生产和消费的产品
package com.spring.boot.entity;
public class Product {
private String name;
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
主函数里我设定了两类线程,并且这里选择用普通的ArrayDeque 来实现Queue,更简单的方式是直接用Java 中的BlockingQueue来实现。
BlockingQueue是阻塞队列,它有一系列的方法可以让线程实现自动阻塞,常用的。这里为了更好的理解并发协同的这个过程,我们先自己处理。
package com.spring.boot.entity;
import java.util.ArrayDeque;
import java.util.Queue;
public class ProducerAndConsumer {
public static void main(String[] args) {
Queue<Product> queue = new ArrayDeque<>();
for (int i = 0; i < 100; i++) {
new Thread(new Producer(queue, 100)).start();
new Thread(new Consumer(queue, 100)).start();
}
}
}
然后就是Producer和Consumer 了。
package com.spring.boot.entity;
import java.util.Queue;
import java.util.Random;
public class Producer implements Runnable{
private Queue<Product> queue;
private int maxCapacity;
public Producer(Queue queue, int maxCapacity) {
this.queue = queue;
this.maxCapacity = maxCapacity;
}
@Override
public void run() {
while(true) {
synchronized (queue) {
while (queue.size() == maxCapacity) { //一定要用 while,而不是 if,下文解释
try {
System.out.println("生产者" + Thread.currentThread().getName() + "等待中... Queue 已达到最大容量,无法生产");
wait();
System.out.println("生产者" + Thread.currentThread().getName() + "退出等待");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (queue.size() == 0) { //队列里的产品从无到有,需要通知在等待的消费者
queue.notifyAll();
}
Random random = new Random();
Integer i = random.nextInt();
queue.offer(new Product("产品" + i.toString()));
System.out.println("生产者" + Thread.currentThread().getName() + "生产了产品:" + i.toString());
}
}
}
}
package com.spring.boot.entity;
import java.util.Queue;
public class Consumer implements Runnable{
private Queue<Product> queue;
private int maxCapacity;
public Consumer(Queue queue, int maxCapacity) {
this.queue = queue;
this.maxCapacity = maxCapacity;
}
@Override
public void run() {
while(true){
synchronized (queue) {
while (queue.isEmpty()) {
try {
System.out.println("消费者" + Thread.currentThread().getName() + "等待中... Queue 已缺货,无法消费");
wait();
System.out.println("消费者" + Thread.currentThread().getName() + "退出等待");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Product product = queue.poll();
System.out.println("消费者" + Thread.currentThread().getName() + "消费了:" + product.getName());
if (queue.size() == maxCapacity) {
queue.notifyAll();
}
}
}
}
}
运行,报错:Exception in thread "Thread-12" java.lang.IllegalMonitorStateException

抛出: IllegalMonitorStateException - 如果当前线程不是此对象监视器的所有者。
原因:根据jdk源码
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
// Perform action appropriate to condition
}
此方法只应由作为此对象监视器的所有者的线程来调用。
所以代码wait()改成:queue.wait()
package com.spring.boot.entity;
import java.util.Queue;
import java.util.Random;
public class Producer implements Runnable{
private Queue<Product> queue;
private int maxCapacity;
public Producer(Queue queue, int maxCapacity) {
this.queue = queue;
this.maxCapacity = maxCapacity;
}
@Override
public void run() {
while(true) {
synchronized (queue) {
while (queue.size() == maxCapacity) { //一定要用 while,而不是 if,下文解释
try {
System.out.println("生产者" + Thread.currentThread().getName() + "等待中... Queue 已达到最大容量,无法生产");
queue.wait();
System.out.println("生产者" + Thread.currentThread().getName() + "退出等待");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (queue.size() == 0) { //队列里的产品从无到有,需要通知在等待的消费者
queue.notifyAll();
}
Random random = new Random();
Integer i = random.nextInt();
queue.offer(new Product("产品" + i.toString()));
System.out.println("生产者" + Thread.currentThread().getName() + "生产了产品:" + i.toString());
}
}
}
}
package com.spring.boot.entity;
import java.util.Queue;
public class Consumer implements Runnable{
private Queue<Product> queue;
private int maxCapacity;
public Consumer(Queue queue, int maxCapacity) {
this.queue = queue;
this.maxCapacity = maxCapacity;
}
@Override
public void run() {
while(true){
synchronized (queue) {
while (queue.isEmpty()) {
try {
System.out.println("消费者" + Thread.currentThread().getName() + "等待中... Queue 已缺货,无法消费");
queue.wait();
System.out.println("消费者" + Thread.currentThread().getName() + "退出等待");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Product product = queue.poll();
System.out.println("消费者" + Thread.currentThread().getName() + "消费了:" + product.getName());
if (queue.size() == maxCapacity) {
queue.notifyAll();
}
}
}
}
}
https://blog.youkuaiyun.com/hch814/article/details/106296929/
博客内容讲述了在实现多线程的生产者消费者模型时遇到的`java.lang.IllegalMonitorStateException`错误。错误源于线程调用`wait()`方法时未持有正确的监视器对象。通过分析JDK源码,指出只有对象监视器的所有者线程才能调用`wait()`,并提供了将代码修改为`queue.wait()`的解决方案,以解决这个问题。
6068

被折叠的 条评论
为什么被折叠?



