Detailed explanation of the differences between wait and notify in Java multithreaded communication
The following is a code to introduce the difference between wait notify in Java multithreading communication. The specific contents are as follows:
The above results appear?? Consumers always consume or producers always produce
Solution: consumers can only consume after producers complete production. Otherwise, consumers can not consume. Producers can not produce if consumers do not consume or do not consume. One production and one consumption at a time. In fact, it is to ensure that only one thread operates on res shared resources at the same time,
Wait, notify, notifyAll methods
Wait, notify and notifyAll are three methods defined in the object class, which can be used to control the status of threads.
These three methods eventually call JVM level native methods. There may be some differences depending on the JVM running platform.
If the object calls the wait method, the thread holding the object will hand over the control of the object and be in the waiting state. The current thread changes from running to blocking, freeing the required resources
If an object calls the notify method, it will notify a thread waiting for control of the object to continue running. Change the thread holding the lock from blocked to ready.
If an object calls the notifyAll method, all threads waiting for control of the object will be notified to continue running.
Note: it must be used in thread synchronization and is a resource of the same lock
The requirements can be completed in the following ways.
After the producer obtains the res.getclass lock, if the flag is true, the producer calls res.getclass Wait. At this time, other threads can obtain the lock. If the flag is false, start production, then set the flag to true to ensure resource consumption before reproduction, and then notify other threads to wake up other threads through notify.
The output is as follows:
What happens if notify is removed? Remove one? Remove two?
One producer can print more than one (but not many). Consumers can only print one. Two producers may not print, or one producer may print. Therefore, wait and notify must be used in pairs
What is the difference between wait and sleep?
It's all sleep. Wait needs to notify
For the sleep method, we first need to know that the method belongs to the thread class. The wait method belongs to the object class.
The sleep method causes the program to suspend execution for the specified time and give up the CPU to other threads, but its monitoring status remains the holder. When the specified time expires, it will automatically resume running status.
During the call to the sleep method, the thread does not release the object lock.
When the wait method is called, the thread will give up the object lock and enter the wait lock pool waiting for this object. Only after the notify method is called for this object, the thread enters the object lock pool to get the object lock and enter the running state.
summary
The above is a detailed explanation of the differences between wait and notify in Java multithreaded communication introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!