java - Java中如何正确实现生产者-消费者
问题描述
我像这样实现了生产者消费者。
但它正在抛出错误。
我尝试使用这种使用锁的方法。关联
class Testclass {
Boolean isFresh = false;
int count = 0;
public synchronized void GET(String threadName){
while(!isFresh){
try {
isFresh.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("GET method was called : " + count + " " + threadName);
isFresh = false;
isFresh.notify();
}
public synchronized void PUT(String threadName){
while(isFresh){
try{
isFresh.wait();
}catch( InterruptedException e){
e.printStackTrace();
}
}
count++;
System.out.println("PUT method was called : " + count + " " + threadName);
isFresh = true;
isFresh.notify();
}
}
class Producer implements Runnable{
Testclass q;
String name;
Producer(Testclass q, String name){
this.q = q;
this.name = name;
}
public void run(){
while(true){
int time = (int)(Math.random() * 10000);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
q.PUT(this.name);
}
}
}
class Consumer implements Runnable{
Testclass q;
String name ;
Consumer(Testclass q,String name){
this.q = q;
this.name = name;
}
public void run(){
while(true){
int time = (int)(Math.random() * 10000);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
q.GET(this.name);
}
}
}
public class Main {
public static void main(String[] args){
Testclass t = new Testclass();
Thread t1 = new Thread(new Consumer(t, "consumer 1"));
Thread t2 = new Thread(new Consumer(t, "consumer 2"));
Thread t3 = new Thread(new Producer(t, "producer 1"));
Thread t4 = new Thread(new Producer(t, "producer 2"));
t1.start();
t2.start();
t3.start();
t4.start();
try{
t1.join();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
此实现引发以下错误。
请解释为什么所有线程都抛出 Illegal MonitorStateException?
PUT method was called : 1 producer 1
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.PUT(Main.java:34)
at Producer.run(Main.java:54)
at java.base/java.lang.Thread.run(Thread.java:832)
GET method was called : 1 consumer 1
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.GET(Main.java:19)
at Consumer.run(Main.java:76)
at java.base/java.lang.Thread.run(Thread.java:832)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.wait(Native Method)
at java.base/java.lang.Object.wait(Object.java:321)
at Testclass.GET(Main.java:12)
at Consumer.run(Main.java:76)
at java.base/java.lang.Thread.run(Thread.java:832)
PUT method was called : 2 producer 2
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.PUT(Main.java:34)
at Producer.run(Main.java:54)
at java.base/java.lang.Thread.run(Thread.java:832)
Process finished with exit code 0
我想知道为什么输出是这样的?什么是正确的实施方式?
解决方案
您应该使用BlockingQueue。然后你不必使用等待/通知甚至同步。
class Testclass {
BlockingQueue<Integer> queue = new ArrayBlockingQueue(1);
AtomicInteger count = new AtomicInteger(0);
public void GET(String threadName) throws InterruptedException{
Integer i = queue.take();
count.getAndIncrement();
System.out.println("GET method was called : " + count + " " + threadName);
}
public void PUT(String threadName) throws InterruptedException{
int c = count.get();
queue.put(c);
count.getAndIncrement();
System.out.println("PUT method was called : " + count + " " + threadName);
}
}
我不确定你想对 count 做什么。这很容易通过更改队列的大小来扩展。
推荐阅读
- flutter - Flutter - 参数类型'Object'不能分配给参数类型'Map
' - foreign-keys - TypeORM 官方关系示例不起作用
- javascript - 连接时如何检查错误的字符串值?
- multithreading - MPI RMA 中的多线程支持
- docker - 我经常从 docker 容器中无法访问主机
- javascript - 在 webview 中请求权限时如何使我的网址不可见?
- kotlin - 构建器模式功能无法在 SpringBoot Kotlin 应用程序中工作
- ssis - Excel 停止刷新 Olap 多维数据集
- java - Android是否可以在没有dx的情况下加载jar?
- azerothcore - 重生后玩家看不见的生物