Detailed inter-thread cooperation

E.g:

The upper part of the process is responsible for displaying the time, and the lower part of the thread is responsible for obtaining the time

The synchronized keyword only plays the role of multiple threads "serially" execute the code in the critical section, but which thread executes first, which thread executes later cannot be determined, wait(), notify() and notifyAll() in the Object class three methods to solve the problem of coordination between threads, multiple threads can be determined by a thread "reasonable" use of these three methods have execution order:
1, wait () : call the object lock wait () method will make the current support The thread with the object lock is in the thread waiting state and the thread releases control of the object lock. The thread waiting for the object lock will not be awakened until the object lock calls the notify() method or notifyAll() method in other threads. .
2. notify() : calling the notify() method of an object lock will wake up a single thread waiting on the object lock .
3. notifyAll() : calling the notifyAll() method of the object lock will wake up all threads waiting on the object lock ; calling the notifyAll() method will not immediately activate a waiting thread, it can only cancel the interrupt state of the waiting thread , So that they can compete with other threads after the current thread exits the synchronization method or the synchronization code block method, in order to obtain resource objects for execution.
In a word: Whoever calls the wait method must call the notify or notifyAll method, and "who" is the object lock

Use the wait(), notify() and notifyAll() methods in the Object class to pay attention to the following points:
1. The wait() method needs to be paired with one of the notify() or notifyAll() methods , and the wait method When paired with notify() or notifyAll(), they cannot be in the same thread (see code 1).
2. The wait() method, notify() method and notifyAll() method must be used in a synchronized method or synchronized code block , otherwise an IllegalMonitorStateException will occur .
3. The object that calls the wait() method, notify() method, and notifyAll() method must be the same object as the synchronization lock object . (See code 2)

 

The difference between sleep() method and wait() method

After the sleep() method is called, the current thread enters the blocking state, but the current thread still holds the object lock. During the sleep of the current thread, other threads cannot execute the code in the critical section where the sleep method is located.

The object lock calls the wait() method to make the thread currently holding the object lock in a thread waiting state and at the same time the thread releases control of the object lock. While the current thread is in the thread waiting period, other threads can execute the critical area where the wait method is located. In the code.

wait() method

Although it is an infinite loop here, because the object lock lockObj calls the wait() method, the "counter 1" thread and the "counter 2" thread holding the lockObj object lock are in the thread waiting state, so the loop does not continue

Why doesn't the time thread enter the blocking state? ——The object represented by the timeThread variable serves as the object lock, because the code is in the main method, which means that the main thread will execute the code in the critical section in the future, that is to say, the main thread is the thread holding the object lock, and the main thread executes After the "timeThread.wait()" code, it enters the blocking state, which means that the main thread enters the blocking state instead of the time thread. This is also the reason why the "System.out.println("main method");" code in the main method does not execute.

notify() method

notifyAll() method

Although there is an endless loop in the blue box on the left, after the "counter 1" thread and the "counter 2" thread output 1, the object lock lockObj calls the wait() method to make the two threads enter the thread waiting state; but the main thread is sleeping After 5000 milliseconds, the thread object created by the NotifyAllThread thread class is started. When the thread object executes the run method, the object lock calls the notifyAll method, causing both threads to be awakened, so the two threads loop again

 

 

 

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/113732094