Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.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

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_Algorithm_Concurrency - Fatal编程技术网

Java 此生产者-消费者的非法监视器状态异常?

Java 此生产者-消费者的非法监视器状态异常?,java,multithreading,algorithm,concurrency,Java,Multithreading,Algorithm,Concurrency,试图在java中使用一个简单的计数器来练习生产者和消费者。 不确定为什么我在这段代码上遇到非法的监视器状态异常 我有计数器rest和计数器consumer方法,它们在自己的线程中运行。 计数器本身是一个静态int可变字段。 counter类还为您提供了一个 如果我将wait naotify更改为以下内容: Counter.lock.notify(); Counter.lock.wait(); 代码是有效的。Dosen't wait()和notify()会自动获取同步锁开启时的引用吗 生产者阶级

试图在java中使用一个简单的计数器来练习生产者和消费者。 不确定为什么我在这段代码上遇到非法的监视器状态异常

我有计数器rest和计数器consumer方法,它们在自己的线程中运行。 计数器本身是一个静态int可变字段。 counter类还为您提供了一个

如果我将wait naotify更改为以下内容:

Counter.lock.notify();
Counter.lock.wait();
代码是有效的。Dosen't wait()和notify()会自动获取同步锁开启时的引用吗

生产者阶级

package multithreading;


public class CounterProducer implements Runnable {


public void run() {

    try {   incrCounter();   } catch (InterruptedException e) {            e.printStackTrace();        }
}

public void incrCounter() throws InterruptedException {


    while (true) {
        synchronized (Counter.lock) {
            if (Counter.counter < 1) {
                System.out.println("Counter Reset");
                Counter.counter = 10;
                notify();
                wait();
            }


        }
    }


}
柜台

public class CounterRunner {

    public static void main(String[] args) {

        Thread con = new Thread(new CounterConsumer());
        Thread prod = new Thread(new CounterProducer());

        con.start();
        prod.start();

    }


}
跑步者

public class CounterRunner {

    public static void main(String[] args) {

        Thread con = new Thread(new CounterConsumer());
        Thread prod = new Thread(new CounterProducer());

        con.start();
        prod.start();

    }


}
如果我将wait naotify更改为以下内容,则代码将正常工作:

Counter.lock.notify();
Counter.lock.wait();
每个Java方法要么是某个类的静态方法,要么是某个对象的实例方法。如果您看到的方法调用不包含显式类名或对象引用,则它是对属于
this
对象的方法的隐式调用

也就是说,
notify()
的意思与
this.notify()
的意思相同,
wait()
的意思是
this.wait()

this
,当它出现在
CounterProducer.incrCounter()
方法中时,指的是
CounterProducer
实例,当它出现在
counterpconsumer.consumercounter()方法中时,指的是
counterpconsumer
实例

Counter.lock.notify();
Counter.lock.wait();