What are the common methods for waking and blocking Java threads?

Source: Internet
Author: User

If the thread is blocked by calling the wait (), sleep (), or join () method, it can be disconnected and wake it up by throwing interruptedexception, if the thread encounters io blocking, because IO is implemented by the operating system, Java code has no way of directly contacting the operating system. The following is a detailed wake-up method:

1. Sleep () method

Sleep (milliseconds), which specifies the time in milliseconds for a thread to enter a thread-blocking state during that time, without a time slice of the CPU, until the time passes and the thread re-enters the executable state. (Pauses the thread, does not release the lock)

2.suspend () and Resume () method:.

Suspending and waking the thread, suspend E () causes the thread to enter a blocking state, and the thread enters the executable state only when the corresponding resume E () is called. (not recommended, easy to happen deadlock)

3. Yield () method:

Causes the thread to discard the currently divided CPU time slice, but at this time the line Cheng Jinghan is in the executable state, at any time the CPU time slice can be divided again. The yield () method only causes threads with the same priority to have an opportunity to execute. The effect of calling yield () is equivalent to the scheduler thinking that the thread has performed enough time to go to another thread. (pauses the currently executing thread and executes other threads, and the time of the surrender is unknown)

4.wait () and Notify () methods

Two methods are used together, and wait () causes the thread to enter a blocking state, and when called Notify (), the thread enters an executable state. Wait () can be added or not added to the parameter, in milliseconds, when the specified time is reached or when the Notify () method is called, the executable state is entered. (Belonging to the object class, not the thread class, wait () releases the locked object before performing the wait action. Because wait () waits for an object that must be locked first, it can only be used in a Synchronizer segment or in a synchronous method, otherwise, an exception illegalmonitorstateexception is thrown.)

5.join () method

Also called thread join. is the join () method that the current thread A calls another thread B, and the current thread goes into a blocking state until thread B runs to the end, and thread A is moved from the blocking state to the executable state.

These are the five common methods of wake-up and blocking of Java threads, with different methods having different features, where wait () and notify () are the most powerful and flexible methods, but this also leads to their inefficient and error-prone features, therefore, in practical applications should be flexible use of various methods, To achieve the desired goal and effect!

What are the common methods for waking and blocking Java threads?

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.