Analyze java thread wait and notify in detail

Wait () and notify () are directly subordinate to the object class, that is, all objects have this pair of methods. At first glance, this seems incredible, but in fact it is very natural, because when this pair of methods are blocked, the occupied lock will be released, and the lock is owned by any object. Calling the wait () method of any object will lead to thread blocking, and the lock on the object will be released. Calling the notify () method of any object causes a randomly selected thread blocked by calling the wait () method of the object to unblock (but it can not be executed until the lock is obtained).

Secondly, wait () and notify () can be called at any location, but this pair of methods must be invoked in synchronized method or block. The reason is very simple. Only when the current thread in synchronized method or block occupies a lock, can a lock be released. Similarly, the lock on the object calling this pair of methods must be owned by the current thread so that a lock can be released. Therefore, method calls must be placed in such synchronized methods or blocks, and the locked object of the method or block is the object that calls these methods. If this condition is not met, the program can still be compiled, but an illegalmonitorstateexception will appear at run time.

The above characteristics of wait () and notify () methods determine that they are often used with synchronized methods or blocks. If you compare them with the interprocess communication machine of the operating system, you will find their similarities: synchronized methods or blocks provide functions similar to the operating system primitives, and their execution will not be disturbed by the multithreading mechanism, This opposite law is equivalent to the block and wakeup primitives (both methods are declared as synchronized). Their combination enables us to implement a series of exquisite interprocess communication algorithms (such as semaphore algorithms) on the operating system and solve various complex interprocess communication problems.

Finally, two points will be explained about the wait () and notify () methods: first, the threads unblocked by calling the notify () method are randomly selected from the threads blocked by calling the wait () method of the object. We can't predict which thread will be selected, so we should be careful when programming to avoid problems caused by this uncertainty. Second: in addition to notify (), there is another method notifyAll (). The only difference is that calling notifyAll () will unblock all threads blocked by calling the wait () method of the object at one time. Of course, only the thread that obtains the lock can enter the executable state.

Related wait and notify use Demo:

The above is the whole content of this article. I hope you can like it.

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
分享
二维码
< <上一篇
下一篇>>