Below is a post I have seen in the 9CBS Forum, involving the concept of synchronization, wait (), notify (), etc., I tried to be Wait according to some of the original reply and the related concept on the Think in Java. And NOTIFY () two methods are analyzed, welcome to advise.
Questions are as follows:
Analyze this program and explain it, focus on synchronized, wait (), notify thank you! Class threada {public static void main (string [] args) {threadb b = new threadb (); b.Start (); system.out.println ("b is start ...."); synchronized (b) / What is the meaning of B in parentheses? {Try {system.out.println ("Waiting for B To Complete ...); b.wait (); // What is the meaning of this sentence, let Who Wait? System.out.println ("Completed.Now Back to Main Thread");} catch (interruptedException E) {}} system.out.println ("Total IS: B. Total);}}
Class Threadb Extends Thread {Int Total; Public Void Run () {SYNCHRONIZED (THIS) {System.out.Println ("Threadb Is Running .."); for (int i = 0; i <100; i ) {Total = i; system.out.println ("Total IS" TOTAL);} NOTIFY ();}}}
To analyze this procedure, you must first understand notify () and wait (), why didn't you record these two methods when you record threads a few days ago, because these two methods are not a Thread class, but belongs to the bottom. Object basic class, that is, not only Thread, each object has NOTINE and WAIT features, why? Because they are used to manipulate locks, and each object is locked, the lock is the basis of each object, since the lock is based, the method of manipulating lock is of course the most basic.
Before you look down, you should first be better to review the 3.3.1 of Think in Java 14.3.1: Waiting and notice, that is, Wait () and Notify.
Follow the explanation in Think in Java: "Wait () allows us to place the thread into the" sleep "state, and" actively "wait for the conditions to change. And only in a NOTIFY () or NotifyAll () when it changes, The thread will be awakened and check if the condition has changed. "
Let's explain this sentence. "WAIT () allowed us to place the thread into" Sleep "status", that is, wait is also blocked by the current thread, this is the same as Sleep or Suspend. That and SLEEP, What is SUSPEND?
Differences between "Wait" and "actively" wait for the conditions to change ", this is critical, Sleep and Suspend cannot be done. Because we sometimes need to control the conflicts between threads through syncing (SYNCHRONIZED). Once you use synchronization, you have to lock the object, which is to get the object lock. Other threads to use the object lock can only queue waiting, waiting until the synchronization method or the program in the synchronization block is completely operational. Method in synchronization method In sync block, no matter how Sleep () is still unlocked when you are called, they all account for the object lock that is being used. And Wait can, it can make synchronization methods or synchronization blocks temporarily Abandon the object lock, and temporarily let other people who need object locks (here should be blocks, or threads), which means that other synchronization methods can be called during WAIT () period! Next (Sleep, Suspend), this is impossible. But note that I have said before, just give up the object lock, temporarily give other threads, my WAIT is still to lock this object. Wait what? Is Wait others used to finish it! Ok, how can I lock the object? The first method is limited to the time to borrow. In Wait (), the parameters are set, such as Wait (1000), in milliseconds, indicate that I only borrowed 1 second After a second, I automatically recovered. The second method, let the people who borrow go not know me, he is finished, I have to give it to me. At this time, I will pick up immediately. Oh, if I set up 1 hour After receiving, others are only half an hour, what should I do? I will rely on it! Of course, I will return it, I still have to set up how long it is. So how do you notify me? I believe everyone can think of it. Notify (), this is the last sentence "and only the thread will be awakened when a notify () or notifyAll () changes. So we can put a wait () and notify () Any synchronization method or inside the synchronization block is placed, whether or not the process involving the thread is prepared in that class. And in fact, we can only call WAIT () and Notify () in synchronous methods or synchronous blocks.
At this time, we will explain the above procedures, which is easy to be confused.
Synchronized (b) {...}; what is meaning is to define a synchronization block that uses B as a resource lock. B.Wait (); meaning temporary release lock, and blocks the current thread, so that other threads using the same lock have the opportunity to execute, here to use the same lock is the B thread itself. This thread is executed After the place, use notify () to inform WAIT threads, the lock has been used, and after the synchronization block where the notify () is running, the thread where the WAIT is located will continue.