Thread blocking sleep () Wait () yield ()

Source: Internet
Author: User
Tags time in milliseconds

From: http://blog.csdn.net/peijunlin/archive/2008/12/20/3564559.aspx

Java introduced a synchronization mechanism to solve access conflicts in shared storage areas. Now let's examine the access of multiple threads to shared resources. Obviously, the synchronization mechanism is not enough, because the resources required at any time are not necessarily ready for access, in turn, there may be more than one ready resource at the same time. To solve the access control problem in this case, Java introduces support for the blocking mechanism.

Blocking refers to suspending the execution of a thread to wait for a condition to occur (for example, a resource is ready). Students who have learned the operating system must be familiar with it. Java provides a large number of methods to support blocking. Let's analyze them one by one.

1.Sleep () method:

1)Sleep () allows you to specify a period of time in milliseconds as a parameter,ItSo that the thread enters the blocking status within the specified time, And the CPU time cannot be obtained. When the specified time expires, the thread re-enters the executable status.

2)When the sleep () function is called, the thread will not release its "lock flag ".
Typically, sleep () is used to wait for a certain resource to be ready: after the test finds that the condition is not met, the thread is blocked for a period of time and then re-tested until the condition is met.

2. suspend () and resume () Methods: these two methods are used together. Suspend () causes the thread to enter the blocking state and will not be automatically restored. The corresponding resume () must be called, in order to re-enter the executable state of the thread. Typically, suspend () and resume () are used to wait for the results produced by another thread: after the test finds that the results have not yet been produced, the thread is blocked. After the results are produced by another thread, call resume () to restore it.

3.Yield () method: yield () causes the thread to discard the current CPU time, but does not block the thread, that is, the thread is still executable and the CPU time may be allocated again at any time.The effect of calling yield () is equivalent to that the scheduler considers that the thread has executed enough time to go to another thread. Yield () only enables the current thread to return to the executable State. Therefore, the thread that executes yield () may be executed immediately after it enters the executable state. Sleep () can lead to execution of low-priority threads. Of course, it can also lead to execution of threads with the same priority and higher priority; yield () only threads with the same priority can have execution opportunities.

4. Wait () and notify () Methods: they are used together,Wait () enables the thread to enter the blocking state. It has two forms,

You can specify a period of time in milliseconds as a parameter;The other has no parameters. When the corresponding notify () is called or the thread enters the executable state after the specified time is exceeded, the latter must call the corresponding notify.When wait () is called, the thread will release the lock mark it occupies, so that other synchronized data in the thread's object can be used by other threads.
Waite () and Y () are called in the synchronized function or synchronized block because they operate on the object's "lock flag. If the call is performed in the non-synchronized function or the non-synchronized block, the illegalmonitorstateexception exception may occur at runtime although the compilation is successful.

 

The wait () and notify () methods are described as follows:

First, the thread that calls the notify () method to remove blocking is randomly selected from the thread that is blocked by calling the wait () method of the object, we cannot predict which thread will be selected, so we should be very careful when programming to avoid problems caused by such uncertainty.

Second: In addition to notify (), there is also a method notifyall () that can also play a similar role. The only difference is that calling the notifyall () method will call the wait () and all the blocked threads are all blocked at one time. Of course, only the thread that gets the lock can enter the executable state.

When talking about blocking, we can't help but talk about deadlocks. A brief analysis shows that calls of the suspend () method and the wait () method without specifying the timeout period may lead to deadlocks. Unfortunately, Java does not support avoiding deadlocks at the language level. We must be careful in programming to avoid deadlocks.


It seems that they have no difference with the suspend () and resume () methods, but they are actually completely different. The core difference is that all the methods described above will not release the occupied lock (if occupied) when blocking, and this method is the opposite.

The above core differences lead to a series of detailed differences.

First, all the methods described above belong to the Thread class, but this pair is directly affiliated to the object class, that is, all objects have this pair of methods. It seems incredible at the beginning, but it is actually quite natural, because this pair of methods will release the occupied lock when blocking, and the lock is owned by any object, the wait () method of any object is called to cause thread blocking and the lock on the object is released. However, calling the notify () method of any object causes the wait () method of the object to be called () method ).

Secondly, all the methods described above can be called anywhere, but this pair of methods must be called in the synchronized method or block for a very simple reason, the lock can be released only when the current thread occupies the lock in the synchronized method or block. In the same way, the lock on the object that calls this method must be owned by the current thread so that the lock can be released. Therefore, this pair of method calls must be placed in such a synchronized method or block. The lock object of this method or block is the object that calls this pair of methods. If this condition is not met, the program can still be compiled, but the illegalmonitorstateexception will occur at runtime.

The preceding features of the wait () and notify () methods determine that they are often used together with the synchronized method or block, by comparing them with the inter-process communication machine of the operating system, we will find their similarity: the synchronized method or block provides functions similar to the operating system primitive, their execution will not be affected by the multi-thread mechanism, and this pair of methods is equivalent to the block and wakeup primitives (both of which are declared as synchronized ). The combination of these algorithms allows us to implement a series of exquisite inter-process communication algorithms (such as semaphore algorithms) on the operating system and solve various complicated inter-thread communication problems.

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.