Java Concurrent Programming: inter thread communication wait, notify

Java Concurrent Programming: two ways of cooperation between threads: wait, notify, notifyAll and condition

We've discussed a lot about synchronization, but in reality, we need collaboration between threads. For example, the most classic producer consumer model: when the queue is full, the producer needs to wait for the queue to have space to continue to put goods into it, and during the waiting period, Producers must release critical resources (i.e. queue) occupation right. If the producer does not release the occupation right of critical resources, consumers will not be able to consume the goods in the queue, and there will be no space in the queue, so the producer will wait indefinitely. Therefore, generally, when the queue is full, the producer will be asked to hand over the occupation right of critical resources and enter the suspended state. Then wait After the consumer consumes the goods, the consumer notifies the producer that there is space in the queue. Similarly, when the queue is empty, the consumer must wait for the producer to inform it that there are goods in the queue. This process of communicating with each other is the cooperation between threads.

Today, let's discuss the two most common ways of thread cooperation in Java: using object wait()、Object. Notify() and using condition

The following is the outline of this article:

I Wait(), notify(), and notifyall()

II Condition

III Implementation of producer consumer model

If there is anything wrong, please forgive me and welcome criticism and correction.

Please respect the author's labor achievements. Please indicate the original link for Reprint:

   http://www.cnblogs.com/dolphin0520/p/3920385.html

I Wait(), notify(), and notifyall()

Wait(), notify() and notifyall() are methods in the object class:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>