Java thread: thread interaction SCJP5 learning note thread interaction is a complex problem. SCJP requires no foundation: to give a scenario, write code to properly use the waiting, notification, and notification to all threads. 1. The thread interaction knowledge point required by SCJP is from java. lang. three methods of Object class to learn: void y () Wake up a single thread waiting on this Object monitor. Void policyall () Wake up all threads waiting on this object monitor. Void wait () causes the current thread to wait until other threads call the notify () or yyall () method of this object. Of course, wait () has two other overload Methods: void wait (long timeout) causes the current thread to wait until other threads call the notify () method or yyall () method of this object, or exceeds the specified time. Void wait (long timeout, int nanos) causes the current thread to wait until other threads call the notify () method or notifyAll () method of this object, or other threads interrupt the current thread, or the actual time has exceeded. The preceding methods help the thread to pass the time state of concern to the thread. The key points to remember about waiting/notification are that the wait (), Y (), and notifyAll () methods must be called from within the synchronization ring. A thread cannot call a wait or notification method on an object unless it owns the Lock of that object. Wait (), Policy (), and policyall () are all Object instance methods. Like every object with a lock, each object can have a thread list that waits for the signal (notification ). The thread obtains the waiting list through the wait () method on the execution object. Since then, it does not execute any other commands until it calls the notify () method of the object. If multiple threads wait on the same object, only one thread is selected (the sequence in which the thread is not guaranteed) for execution. If no thread is waiting, no special operation is taken. Let's take a look at the example below to understand: /*** calculate and output data calculated by other thread locks ** @ author leizhimin 13:20:38 */public class ThreadA {public static void main (String [] args) {ThreadB B = new ThreadB (); // start the computing thread B. start (); // thread A has the lock on the object B. The thread must be the owner of the object lock synchronized (B) {try {System. out. println ("waiting for object B to complete computation... "); // The current thread A waits for B. wait ();} catch (InterruptedException e) {e. printStackTrace ();} System. out. println ("the sum of B object calculation is:" + B. total) ;}}/ *** calculate 1 + 2 + 3... + 100 and ** @ author leizhimin 13:20:49 */public class ThreadB extends Thread {int total; public void run () {synchronized (this) {for (int I = 0; I <101; I ++) {total + = I;} // (completed) Wake up a single thread waiting on this object monitor, in this example, thread A is awakened by notify () ;}}, waiting for object B to finish.... The sum calculated by object B is: 5050 Process finished with exit code 0 million. Note: When the wait () method is called on the object, the thread executing the code immediately abandons its lock on the object. However, calling notify () does not mean that the thread will give up its lock. If the thread is proud to complete the synchronization code, the thread will not give up the lock before it is removed. Therefore, calling notify () does not mean that the lock is available. 2. Multiple Threads use policyall () when waiting for an object lock. In most cases, it is best to notify all threads waiting for an object. If you do this, you can use policyall () on the object to make all the threads waiting on this object rush out of the waiting zone and return to the runable state. The following is an example:/*** computing Thread ** @ author leizhimin 11:15:46 */public class Calculator extends Thread {int total; public void run () {synchronized (this) {for (int I = 0; I <101; I ++) {total + = I ;}// notify all threads waiting for this object notifyAll ();}} /*** get the calculation result and output ** @ author leizhimin 11:15:22 */public class ReaderResult extends Thread {Calculator c; public ReaderResult (Calculator c) {this. c = C;} public void run () {synchronized (c) {try {System. out. println (Thread. currentThread () + "waiting for calculation results... "); C. wait ();} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (Thread. currentThread () + "Calculation Result:" + c. total) ;}} public static void main (String [] args) {Calculator calculator = new Calculator (); // start three threads, obtain the calculation result new ReaderResult (calculator) respectively ). start (); new ReaderResult (calculator ). start (); new ReaderResult (calculator ). start (); // start the computing thread calculator. start () ;}} running result: Thread [Thread-1, 5, main] is waiting for the calculation result... Thread [Thread-2, 5, main] waits for the calculation result... Thread [Thread-3, 5, main] waits for the calculation result... Exception in thread "Thread-0" java. lang. illegalMonitorStateException: current thread not owner at java. lang. object. policyall (Native Method) at threadtest. calculator. run (Calculator. java: 18) The calculation result of Thread [Thread-1, 5, main] is: 5050 Thread [Thread-2, 5, main]. The calculation result is: 5050 Thread [Thread-3, 5, main] The calculation result is: 5050 Process finished with exit code 0. The running result indicates that there are exceptions in the program and multiple output results may exist. This shows that this multi-threaded interactive program still has problems. What is the problem that requires in-depth analysis and consideration? The following is a detailed analysis. In fact, in the above Code, we expect the thread that reads the result to wait before the computing thread calls policyall. However, what happens if the computing thread executes the statement first and calls the notify () method before the reading result thread waits? This situation may occur. It cannot guarantee the order in which different parts of the thread will be executed. Fortunately, when the read thread is running, it can only enter the waiting state immediately-it does not do anything to check whether the waiting event has occurred. ---- Therefore, if the computing thread has already called the yyall () method, it will not call notifyAll () Again, ---- and the waiting read thread will always wait. This is, of course, a problem that developers do not want to see. Therefore, when a waiting event occurs, you must be able to check whether the notifyAll () Notification event has occurred.