Java Multithreading Series--"Juc lock" 07 of the Locksupport

Source: Internet
Author: User

Overview

This chapter describes the Locksupport in the Juc (java.util.concurrent) package. The content includes:
Locksupport Introduction
List of Locksupport functions
Locksupport Reference code (based on JDK1.7.0_40)
Locksupport Example

Reprint Please specify source: http://www.cnblogs.com/skywang12345/p/3505784.html

Locksupport Introduction

Locksupport is the basic thread blocking primitive used to create locks and other synchronization classes.
The role of Park () and Unpark () in Locksupport is to block and unblock threads, and park () and Unpark () will not encounter deadlocks that may be caused by thread.suspend and Thread.Resume.
Because Park () and Unpark () have a licensed presence, the contention between calling Park () and another thread trying to unpark () will remain active.

List of Locksupport functions
//Returns the Blocker object provided to the last park method call that has not been unblocked, or null if the call is not blocked.StaticObject Getblocker (Thread t)//For thread scheduling, the current thread is disabled unless the license is available.StaticvoidPark ()//For thread scheduling, the current thread is disabled before the license is available.StaticvoidPark (Object blocker)//To disable the current thread for thread scheduling, wait up to the specified wait time, unless the license is available.Staticvoid Parknanos (LongNanos//For thread scheduling, disable the current thread before the license is available and wait up to the specified wait time.static void Parknanos (Object blocker, long Nanos) // for thread scheduling, Disables the current thread until the specified time limit, unless the license is available. static void parkuntil ( Long deadline) //static void Parkuntil (Object blocker, long deadline) //static void unpark (thread thread)     

Locksupport Reference code (based on jdk1.7.0_40)

The source code of Locksupport.java is as follows:

View Code

Description : Locksupport is blocking and unblocking by calling an interface in the unsafe function.

Locksupport Example

The use of Locksupport can be more clearly understood by comparing the following example 1 and Example 2.

Example 1

1PublicClassWaitTest1 {23PublicStaticvoidMain (string[] args) {45 Threada ta =New Threada ("Ta");67Synchronized (TA) {//Acquisition of "Object TA synchronization Lock" via synchronized (TA)8Try{9 System.out.println (Thread.CurrentThread (). GetName () + "Start Ta");10Ta.start ();11System.out.println (Thread.CurrentThread (). GetName () + "block");13//Main thread Wait14Ta.wait ();15System.out.println (Thread.CurrentThread (). GetName () + "continue");17}Catch(Interruptedexception e) {18E.printstacktrace ();19}20}21st}2223StaticClass ThreadaExtendsthread{2425PublicThreada (String name) {26Super(name);27}2829 public void run () {30 synchronized ( this) {//31 System.out.println (Thread.CurrentThread (). GetName () + "wakup others" ); 32 notify (); // Wake "Waiting thread on current object" 33 Span style= "color: #000000;" >}34 }35 }36}            

Example 2

1ImportJava.util.concurrent.locks.LockSupport;23PublicClassLockSupportTest1 {45PrivateStaticThread Mainthread;67PublicStaticvoidMain (string[] args) {89 Threada ta =New Threada ("Ta");10//Get the main threadOne mainthread =Thread.CurrentThread ();12System.out.println (Thread.CurrentThread (). GetName () + "Start Ta");14Ta.start ();15System.out.println (Thread.CurrentThread (). GetName () + "block");17//Main thread Blocking18Locksupport.park (Mainthread);19System.out.println (Thread.CurrentThread (). GetName () + "continue");21st}2223StaticClass ThreadaExtendsthread{2425PublicThreada (String name) {26 super27 }28 29 public void run () {30 System.out.println (Thread.CurrentThread (). GetName () + "wakup others" ); 31 // wake up "main thread" 32  Locksupport.unpark (mainthread); 33 }34 }35}            

Operation result :

Continue

Description : The difference between Park and wait. Wait for a thread to block before it must get a sync lock through synchronized.

Java Multithreading Series--"Juc lock" 07 of the Locksupport

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.