Basic use of Locksupport Park and Unpark, and responsiveness to thread interrupts

Source: Internet
Author: User

/**
* Disables the current thread for thread scheduling purposes unless the
* Permit is available.
*
*<p>If the permit is available then it's consumed and the call returns
* IMMEDIATELY; otherwise
* The current thread becomes disabled for thread scheduling
* Purposes and lies dormant until one of three things happens:
*
*<ul>
*<li>Some other thread invokes {@link#unpark Unpark} with the
* Current thread as the target; Or
*
*<li>Some Other Thread {@linkplainThread#interrupt interrupts}
* the current thread; Or
*
*<li>The call spuriously (that's, for no reason) returns.
*</ul>
*
*<p>This method does<em>Not</em>Report which of these caused the
* Method to return. Callers should re-check the conditions which caused
* The thread to park in the first place. Callers may also determine,
* For example, the interrupt status of the thread upon return.
*
* @param blocker the synchronization object responsible for this
* Thread Parking
* @since 1.6
*/
Public static void Park (Object blocker)

/**
* Makes available the permit for the given thread, if it
* was not already available. If the thread is blocked on
* {@codePark} then it'll unblock. Otherwise, its next call
* to {@codePark} is guaranteed not to block. This operation
is not guaranteed to has any effect at all if the given
* thread have not been started.
*
* @param thread the thread to Unpark, or {@ Code null}, which case
* This operation have no effect
*/
public static void Unpark (thread thread)

Locksupport is the underlying class in the JDK that is used to create a basic thread blocking primitive for locks and other synchronization tool classes. The core aqs:abstractqueuedsynchronizer of the Java Lock and Synchronizer framework is to implement thread blocking and wakeup by calling Locksupport. Park () and Locksupport. Unpark (). The locksupport is very similar to a two-dollar semaphore (only 1 licenses are available), and if the license is not already in use, the current thread acquires the license and continues to execute; If the license is occupied, the current thread is blocked and waiting for a license.

Main (string[] args) {Locksupport.park (); System. Out.println ("block.");}  

Running the code, you can see that the main thread has been blocked. Because the license is occupied by default , the call to park () does not get a license, so it goes into a blocking state.

The following code: Release the license first, then obtain the license, the main thread can terminate normally. Locksupport License acquisition and release, in general, is the corresponding, if multiple Unpark, only once park will not have any problems, the result is that the license is in the available state.

Main (string[] args) {Thread thread = Thread.CurrentThread (); Locksupport.unpark (thread); //Release license Locksupport.park (); //Get license System. out.println ("B");}    

Locksupport is not reentrant , if a thread calls Locksupport. Park () 2 times in a row, the thread is bound to block.

Throws Exception{thread Thread = Thread.CurrentThread (); Locksupport.unpark (thread); System.out. println ("a"); Locksupport.park (); System.out. println ("B"); Locksupport.park (); System.out. println ("C");}     

This code prints a and B, and does not print C because the thread cannot get a deadlock when the second call to park occurs.

Let's take a look at the responsiveness of the locksupport corresponding interrupts

PublicStaticvoid T2 ()Throws Exception{thread T =New Thread (New Runnable () {PrivateIntCount =0; @Overridepublic void run () {long start = System.currenttimemillis (); long end = 0; while ((End-start) <= 1000) {count++;end = System.currenttimemillis ();} System.out. println ( "after 1 second.count=" +  count); //wait for perhaps license Locksupport.park (); System.out. println ( "thread over." + Thread.CurrentThread (). Isinterrupted ());}); T.start (); Thread.Sleep (2000); println ( "main over");}       

The final thread prints out the thread over.true. This means that the thread can respond to an interrupt request (the interrupt state is set to true) if it is blocked by calling park, but does not throw interruptedexception.

Basic use of Locksupport Park and Unpark, and responsiveness to thread interrupts

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.