Basic Java Tutorial: Multi-Threaded Foundation (2)--thread-between-threads communication
After the communication between the threads, the interaction between the systems is more powerful, while greatly improving the CPU utilization, it also enables the programmer to effectively control and supervise the process of each thread task.
Communication mind Mapping between threads
Waiting in
wait/notification mechanism does not use the wait/notification mechanism
We can use a dead loop with sleep () and Whle (true) to implement communication between multiple threads.
Although two threads implement communication, thread B must constantly detect a condition through the while statement rotation mechanism, which wastes CPU resources.
If the polling interval is small, the time interval is more wasted. If the rotation time interval is large, you may not get the data you want, so you need a mechanism to reduce CPU waste.
wait/notification mechanism
Waiting/notification mechanisms abound in life, such as when eating
About wait
The role of wait is to wait for the thread that is currently executing the code, which is a method of the object class that is used to place the current thread in the pre-execution queue and stop execution at the line where wait () is located until it is notified or interrupted.
before calling Wait (), the thread must obtain an object-level lock on the object , that is, the wait method can only be called in a synchronous method or in a synchronous block .
After the wait () method is executed, the current thread releases the lock. The thread competes with other threads to regain the lock before returning from wait.
If you do not hold the appropriate lock when you call Wait (), Illegalmonitorstateexception is thrown.
About Notify
Method Notify () is also called in a synchronous method or synchronous block , that is, before the call, the thread must also obtain an object-level lock on the object. If no object lock is held, Illegalmonitorstateexception is thrown.
This method is used to notify other threads that may be waiting for an object lock on the object, and if there are multiple threads waiting, the thread planner randomly picks one of the wait-state threads, notifies notify, and causes it to wait for the object lock on the object . It is necessary to note that after the Notify method is executed, the current thread does not immediately release the object lock, the wait-state thread cannot get the object lock immediately, and waits for the thread that executes the Notify () method to execute the program, that is, after exiting the synchronized code block, To release the lock .
When the first wait thread that obtains the object lock finishes running, it frees the object lock, and if the object does not use the Notify statement again, the other wait-waiting threads continue to block in the wait state, even if the object is idle, because the object is not notified. Until the object emits a notify or notifyall.
A sentence summary:
Wait causes the thread to stop running, and notify keeps the stopped thread running.
Method wait lock release and notify Lock not released
When the method wait () is executed, the lock is automatically released;
After executing the Notify () method, the lock is not automatically released and the lock must be released after the synchronization synchronized code block where the Notify () method is executed.
Summarize
- The wait () method causes the thread that invokes the method to release the lock on the shared resource, and then exits from the running state and into the wait queue until it is awakened again.
- The Notify () method randomly wakes up a thread waiting for the same shared resource in the queue, and causes the thread to exit the wait queue and enter the operational state, which means that the Notify () method notifies only one thread.
- The Nofityall () method allows all threads waiting in the queue to wait for the same shared resource to exit from the waiting state and into a running state. At this point, the highest-priority thread executes first, but it is also possible to execute randomly, depending on the implementation of the JVM virtual machine.
Two. Cond
Basic Java Tutorial: Multi-Threaded Foundation (2)--thread-between-threads communication