Java thread Blocking

Source: Internet
Author: User
Tags time in milliseconds
Blocking refers to suspending the execution of a thread to wait for a condition (for example, a resource is ready ).
You are already familiar with this feature. Java provides a large number of methods to support blocking. Let's analyze them one by one.

1. Sleep () method: Sleep () allows you to specify a period of time in milliseconds as a parameter, which makes the thread at the specified time
The thread enters the blocking status. The CPU time cannot be obtained. When the specified time expires, the thread re-enters the executable status.
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 will be blocked for a period of time.
Retest until the conditions are met.
2. Suspend () and resume () methods: the two methods are used together. Suspend () causes the thread to enter the blocking state and will not
Automatic recovery. The corresponding resume () must be called to re-enter the executable state of the thread. Typically, suspend () and
When resume () is 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 and the other
After the thread generates the result, 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 in
The execution status. The CPU time may be allocated again at any time. Calling yield () is equivalent to scheduling.ProgramThis thread has been fully executed
Enough time to go to another thread.
4. Wait () and notify () methods: the two methods are used together. Wait () causes the thread to enter the blocking state, which has two forms:
, One allows you to specify a period of time in milliseconds as a parameter, and the other does not have a parameter. The former is called when the corresponding notify () or
When the time exceeds the specified time, the thread re-enters the executable state, and the latter must be called accordingly.
It seems that they have no difference with the suspend () and resume () methods, but they are actually completely different. Zone
The other core is that all the methods described above will not release the occupied lock (if occupied) when blocking, and this method will
On the contrary.

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 first, but it is actually quite natural, because this method is blocked.
To release the occupied lock, and the lock is available to any object. Calling the wait () method of any object causes thread blocking, and the object
The lock on is released. Calling the notify () method of any object causes the thread to be blocked by calling the wait () method of this object
The unblocking option selected by the machine (but it can only be executed after the lock is obtained ).

Secondly, all the methods described above can be called anywhere, but this pair of methods must be in the synchronized method or block
The reason is also very simple. The lock can be released only when the current thread in the synchronized method or block occupies the lock. The same
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
It 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
If this condition is not met, the program can still be compiled, but the illegalmonitorstateexception will occur during runtime.

The preceding features of the wait () and notify () methods determine that they are often used together with the synchronized method or block.
When comparing the inter-process communication machines of the operating system, we will find their similarity: the synchronized method or block provides similar operations
The functions of system primitives. Their execution will not be affected by the multi-thread mechanism, and this pair of methods is equivalent to block and wakeup primitives.
(Both methods are declared as synchronized ). Their combination enables us to implement a series of exquisite processes on the Operating System
CommunicationAlgorithm(Such as semaphore algorithm), and used to solve various complex inter-thread communication problems.

The wait () and notify () methods are described as follows:
First, the thread that calls the notify () method causes blocking to be removed from the thread that is blocked by calling the wait () method of the object
Machine selection, we cannot predict which thread will be selected, so we should be very careful when programming, avoid the problem caused by such uncertainty
Question.

Second: In addition to notify (), there is also a method notifyall () that can also play a similar role. The only difference is that
The yyall () method removes all threads blocked by calling the wait () method of the object 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. We can find that the suspend () method and the wait () method without specifying the timeout period ()
Method calls may lead to deadlocks. Unfortunately, Java does not support deadlock avoidance at the language level, so we must be small in programming.
Avoid deadlocks.

We have analyzed various methods for implementing thread blocking in Java, and focused on wait () and notify ()
Methods, because they are the most powerful and flexible to use, but this also causes them to be less efficient and error-prone. Actual use
We should use various methods flexibly in order to better achieve our goal.

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.