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:
File: // analyze this program and explain it. here we will 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 does B in parentheses mean and what does it do?
{
Try
{
System. Out. println ("waiting for B to complete ...");
B. Wait (); // What does this sentence mean?
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 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 () allows us to put threads into the" Sleep "state, while actively waiting for conditions 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 () allows us 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) and" actively "wait for conditions to change", this is critical, and sleep and suspend cannot do it. 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 have run. in the synchronization method and synchronization block, 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 the parameter in wait (), for example, wait (1000). in milliseconds, it indicates that I only lent out for 1 second. After one second, I automatically took it back.
The second method is to let the borrower inform 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? Depend! Of course, it will be recovered after 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 thread B must use the same lock. after this thread is executed to a certain point, it notifies the wait thread by running y (). The lock is used up. After the synchronization block in which notify () is running, the thread where wait is located can continue the execution.