在java中同步消费者-生产者与线程
我编写这段代码是为了尝试理解Java中线程之间的同步,但是当我运行它时,我得到了一个Java.lang.IllegalMonitorStateException异常。我的代码的条件是: 1) 如果小隔间中包含其他生产商生产的物品,则生产商无法生产该物品。 2) 如果储物箱是空的,消费者不能消费物品。 代码如下:在java中同步消费者-生产者与线程,java,multithreading,get,synchronization,producer-consumer,Java,Multithreading,Get,Synchronization,Producer Consumer,我编写这段代码是为了尝试理解Java中线程之间的同步,但是当我运行它时,我得到了一个Java.lang.IllegalMonitorStateException异常。我的代码的条件是: 1) 如果小隔间中包含其他生产商生产的物品,则生产商无法生产该物品。 2) 如果储物箱是空的,消费者不能消费物品。 代码如下: import java.util.*; public class Esercizio1 { public static void main(String[] args) {
import java.util.*;
public class Esercizio1 {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Consumer c1 = new Consumer(c, 1);
Consumer c2 = new Consumer(c, 2);
Producer p1 = new Producer(c, 10);
Producer p2 = new Producer(c, 20);
p1.start(); p2.start();
c1.start(); c2.start();
}
}
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
while (cubbyhole.getAvailable()) {
try {
wait();
} catch (InterruptedException ex) {}
}
for (int i = 1; i < 5; i++) {
int num = number*i;
cubbyhole.put(num);
System.out.println("Producer #" + number + " put: " + num);
}
notifyAll();
cubbyhole.setAvailable(true);
System.out.println("Producer #" + number + " ha finito"); }
}
class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
while (!cubbyhole.getAvailable()) {
try {
wait();
} catch (InterruptedException ex) {}
}
for (int i = 1; i < 5; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + number + " got: " + value);
}
notifyAll();
cubbyhole.setAvailable(false);
System.out.println("Consumer #" + number + " ha finito");
}
}
class CubbyHole {
private int content = -1;
private boolean available = false;
public int get() { return content; }
public void put(int value) { content = value; }
public boolean getAvailable () { return available; }
public void setAvailable (boolean condition) { available = condition; }
}
import java.util.*;
公共类Esercizio1{
公共静态void main(字符串[]args){
长方体孔c=新长方体孔();
消费者c1=新消费者(c,1);
消费者c2=新消费者(c,2);
生产者p1=新生产者(c,10);
生产者p2=新生产者(c,20);
p1.start();p2.start();
c1.开始();c2.开始();
}
}
类生成器扩展线程{
私人小房间;
私有整数;
公共制作人(小隔间c,整数){
立方孔=c;
这个数字=数字;
}
公开募捐{
while(cubbyhole.getAvailable()){
试一试{
等待();
}catch(InterruptedException ex){}
}
对于(int i=1;i<5;i++){
int num=数字*i;
小隔间。放置(num);
System.out.println(“生产者#“+number+”输出:+num);
}
notifyAll();
小隔间。设置可用(真);
System.out.println(“Producer#“+number+“ha finito”)}
}
类使用者扩展线程{
私人小房间;
私有整数;
公共消费者(储物间c,整数){
立方孔=c;
这个数字=数字;
}
公开募捐{
int值=0;
而(!cubbyhole.getAvailable()){
试一试{
等待();
}catch(InterruptedException ex){}
}
对于(int i=1;i<5;i++){
value=cubbyhole.get();
System.out.println(“消费者#“+number+”获得:“+value”);
}
notifyAll();
小隔间。设置可用(错误);
System.out.println(“消费者#”+数字+“ha finito”);
}
}
类隔间{
私有int内容=-1;
private boolean available=false;
public int get(){return content;}
公共void put(int值){content=value;}
公共布尔getAvailable(){return available;}
public void setAvailable(布尔条件){available=condition;}
}
该异常在哪一行引发?请在源代码中注明,因为我们无法访问与您相同的行号。旁白:如果您试图理解线程,那么直接扩展线程
是非常笨拙的。扩展Runnable
或Callable
。另外,现在我不喜欢你的CubbyHole
类。它做的很少,而且您的消费者和生产者必须实现所有的同步。相当地将同步封装在CubbyHole
中,这样生产者和消费者除了调用get
和put
之外就不需要做任何事情了。我最后的评论是:当然,在现实世界中,你根本不会实现CubbyHole
,而是使用java.util.concurrent.ArrayBlockQueue
。