简体   繁体   English

Java中的notifyAll

[英]notifyAll in java

Is it wrong to use notifyAll() inside the loop in a multiple producer-consumer problem in Java? 在Java中的多个生产者-消费者问题中,在循环内使用notifyAll()是错误的吗?

Here is the code snippet which I am talking about. 这是我正在谈论的代码片段。

public void run() {

        while(producer_counter <= 64) {

            synchronized(list) {

                threadId1 = "Producer " + Thread.currentThread().getName();

                //Buffer size is 8
                while(list.size() >= 8) {
                    System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                    System.out.println(threadId1+ " found the buffer is full & waiting for a Consumer to consume.");
                    System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                    try {

                        list.wait();
                        list.notifyAll();
                    }

                    catch (InterruptedException ie) {

                        ie.printStackTrace();
                    }
                }

                System.out.println(threadId1+ " produced item " + producer_counter);
                //Adding the items produced to the list.
                list.add(producer_counter);
                producer_counter++;
                //Invoking notify
                list.notifyAll();

            }
        }
    }
}

notifyAll() is to notify all parties about changes in the state of the synchronized object. notifyAll()用于通知所有有关同步对象状态的更改。 The first call, which is after wait() , is redundant because no state change happened. 因为没有状态更改,所以在wait()之后的第一个调用是多余的。 This call does not make the program wrong, but only causes waste of CPU cycles: when the buffer is full, notifyAll() is called and all other threads, waiting for the buffer's availability, go to processor only to call notifyAll() again and then call wait() . 该调用不会使程序出错,只会浪费CPU周期:当缓冲区已满时,将调用notifyAll() ,而所有其他线程在等待缓冲区可用时,仅进入处理器以再次调用notifyAll() ,然后调用wait() As a result, one processor in your machine will be always busy making unnecessary work. 结果,机器中的一个处理器将总是忙于进行不必要的工作。

BTW, you should not catch InterruptedException (or any other exception) if you don't know why it happened and what to do about it. 顺便说一句,如果您不知道它发生的原因以及如何处理,则不应捕获InterruptedException (或任何其他异常)。 If, like here, you cannot write public void run() throws InterruptedException , then write 如果像这里一样无法编写public void run() throws InterruptedException ,则编写

} catch (InterruptedException ie) {
    throw new RuntimeException(ie);
}

Instead of RuntimeException , better declare your own unchecked exception. 最好声明自己的未经检查的异常,而不是RuntimeException

You should not use list.notifyAll() in while loop.After adding the items produced to the list list.add(producer_counter) you can use notify all. 您不应该在while循环中使用list.notifyAll()。将产生的项目添加到列表list.add(producer_counter)后,您可以使用notify all。

            // producer thread waits while list is full
            while (list.size() >= 8){
                list.wait();
            }

            // to insert the jobs in the list and plus 1 the counter
            list.add(producer_counter);
            producer_counter++;

            // notifies the consumer threads that now it can start 
           //consuming
            list.notifyAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM