Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中同步消费者-生产者与线程_Java_Multithreading_Get_Synchronization_Producer Consumer - Fatal编程技术网

在java中同步消费者-生产者与线程

在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) {

我编写这段代码是为了尝试理解Java中线程之间的同步,但是当我运行它时,我得到了一个Java.lang.IllegalMonitorStateException异常。我的代码的条件是: 1) 如果小隔间中包含其他生产商生产的物品,则生产商无法生产该物品。 2) 如果储物箱是空的,消费者不能消费物品。 代码如下:

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