Java multi-thread programming 5: An Example of understanding wait () and notify ()

Source: Internet
Author: User

An example of understanding wait () and Policy ()

The following is a post I saw on the csdn forum, which involves the understanding of synchronization, wait (), notify (), and other concepts, I tried to analyze the wait () and Policy () methods based on some original replies and related concepts in think in Java. Thank you for your advice.
The problem is as follows:
// Analyze this program and explain it, focusing on synchronized, wait (), notify. Thank you!
The source code of the main program threada is as follows:
Class threada <br/>{< br/> Public static void main (string [] ARGs) <br/>{< br/> threadb B = new threadb (); <br/> B. start (); <br/> system. out. println ("B is start .... "); <br/> synchronized (B) // What does B in parentheses mean and what does it do? <Br/>{< br/> try <br/>{< br/> system. out <br/>. println ("waiting for B to complete... "); <br/> B. wait (); // What does this sentence mean? Who is going to wait? <Br/> system. out <br/>. println ("completed. now back to main thread "); <br/>} catch (interruptedexception e) <br/>{< br/>}< br/> system. out. println ("total is:" + B. total); <br/>}< br/>}
The source code of threadb is as follows:
Class threadb extends thread <br/>{< br/> int total; <br/> Public void run () <br/>{< br/> synchronized (this) <br/>{< br/> system. out. println ("threadb is running .. "); <br/> for (INT I = 0; I <100; I ++) <br/> {<br/> total + = I; <br/> system. out. println ("total is" + total); <br/>}< br/> running y (); <br/>}< br/>}
To analyze this program, we must first understand y () and wait (). Why didn't we record these two methods when recording the thread a few days ago, because these two methods are not in the Thread class, it belongs to the underlying object base class. That is to say, not only is thread, but every object has the functions of notify and wait. Why? Because they are used to manipulate the lock, and each object has a lock, the lock is the basis of each object. Since the lock is the basis, the method of manipulating the lock is of course the most basic.
Let's take a look at it. First, we 'd better review Part 1 of think in Java 14.3.1: waiting and notification, that is, wait () and policy.
According to think in Java, wait () promised to put the thread into the "Sleep" state, while actively waiting for the condition to change. In addition, the thread will be awakened only when one notify () or notifyall () Change and check whether the conditions have changed.
Let's explain this sentence.
"Wait () promised to put the thread into the 'sleep 'state". That is to say, wait also blocks the current thread, which is the same as sleep or suspend. What is the difference between it and sleep and suspend?
The difference is that "(wait) while waiting for conditions to change" is very important. Sleep and suspend cannot do this. Because we sometimes need the help of synchronized to prevent conflicts between threads. Once synchronization is used, the object is locked, that is, the object lock is obtained, other threads that want to use the object lock can only wait in the queue and wait until the synchronization method or all the programs in the synchronization block are completed. In Synchronization Methods and synchronization blocks, neither sleep () nor suspend () can be unlocked when they are called by themselves. They all occupy the used object lock.
However, wait does. It allows the synchronization method or synchronization block to temporarily discard the object lock and temporarily give it to other persons who need the object lock (Here it should be a program block or thread) for use, this means that other Synchronization Methods in the thread object can be called during wait () execution! In other cases (sleep, suspend), this is impossible.
But pay attention to what I mentioned above. I just temporarily give up the object lock and use it for other threads. The thread where my wait is located still needs to reclaim this object lock. What is wait? It's wait. I can get it back when someone else finishes using it!
Well, how can we take back the object lock?
The first method is to limit the lending time. Set parameters in wait (), for example, wait (1000). in milliseconds, it indicates that I only lend for 1 second. One second later, I will automatically reclaim it.
The second method is to ask the lender to notify me that he has used up his work and will return it to me. At this time, I will immediately withdraw it. Ah, what if I took it back one hour later and it took only half an hour for someone else to finish it? Of course, it will be recovered when it is used up. It also determines how long it will take.
So how can someone tell me? I believe everyone can think of it: Notify (), this is the last sentence, "and only when a notify () or notifyall () changes, the thread will be awakened.
Therefore, we can place a wait () and Policy () in any synchronous method or within the synchronization block, regardless of whether the class is prepared for thread-related processing. In fact, we can only call wait () and Policy () in the synchronous method or block ().
At this time, it is easy to explain the above program.
Synchronized (B) {...}; defines a synchronization block and uses B as the resource lock. B. wait (); Means to temporarily release the lock and block the current thread, so that other threads that use the same lock have the opportunity to execute it. Here, the same lock is used for thread B. After this thread is executed to a certain point, notify wait that the thread lock has been used up with notify (). After the synchronization block where notify () is running, the thread where wait is located can continue to run.

Link: http://www.cnzzad.com/tut/52560.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.