Java multi-thread series -- LockSupport for "JUC lock" 07

Source: Internet
Author: User

LockSupport introduces that LockSupport is the basic thread blocking primitive used to create locks and other synchronization classes. In LockSupport, park () and unpark () are used to block the Thread and remove the blocking Thread, and park () and unpark () do not encounter "Thread. suspend and Thread. possible deadlocks caused by resume. Because park () and unpark () have a license, the competition between the thread that calls park () and the thread that tries to keep its unpark () will remain active. The LockSupport function list copies code // returns the blocker object provided to the last unblocking park method call. If the call is not blocked, null is returned. Static Object getBlocker (Thread t) // for Thread scheduling, disable the current Thread unless permitted to be available. Static void park () // for thread scheduling, disable the current thread before the license is available. Static void park (Object blocker) // disables the current thread for Thread Scheduling and waits for the specified wait time at most, unless permitted. Static void parkNanos (long nanos) // for thread scheduling, disable the current thread before the license is available and wait for the specified wait time at most. Static void parkNanos (Object blocker, long nanos) // for thread scheduling, disable the current thread before the specified time limit unless permitted. Static void parkUntil (long deadline) // for thread scheduling, disable the current thread before the specified time limit unless permitted. Static void parkUntil (Object blocker, long deadline) // make the given thread available if the permission is not available yet. Static void unpark (Thread thread) copy the code LockSupport reference code (based on JDK1.7.0 _ 40) LockSupport. the source Code of java is as follows: View Code Description: LockSupport blocks and removes blocking by calling the interface in the Unsafe function. The LockSupport example compares "Example 1" with "Example 2" to provide a clearer understanding of the usage of LockSupport. Example 1 copy code 1 public class WaitTest1 {2 3 public static void main (String [] args) {4 5 ThreadA ta = new ThreadA ("ta "); 6 7 synchronized (ta) {// obtain the "synchronization lock of object ta" Through synchronized (ta) 8 try {9 System. out. println (Thread. currentThread (). getName () + "start ta"); 10 ta. start (); 11 12 System. out. println (Thread. currentThread (). getName () + "block"); 13 // The main thread waits for 14 ta. wait (); 15 16 System. out. println (Thread. currentThread (). getName () + "continue"); 17} catch (InterruptedException e) {18 e. printStackTrace (); 19} 20} 21} 22 23 static class ThreadA extends Thread {24 25 public ThreadA (String name) {26 super (name ); 27} 28 29 public void run () {30 synchronized (this) {// obtain the "synchronization lock of the current object" Through synchronized (this) 31 System. out. println (Thread. currentThread (). getName () + "wakup others"); 32 y (); // wake up the "Waiting thread on the current object" 33} 34} 35} 36} copy sample code 2 Copy code 1 import java. util. concurrent. locks. lockSupport; 2 3 public class LockSupportTest1 {4 5 private static Thread mainThread; 6 7 public static void main (String [] args) {8 9 ThreadA ta = new ThreadA ("ta"); 10 // obtain the main Thread 11 mainThread = Thread. currentThread (); 12 13 System. out. println (Thread. currentThread (). getName () + "start ta"); 14 ta. start (); 15 16 System. out. println (Thread. currentThread (). getName () + "block"); 17 // The main thread blocks 18 LockSupport. park (mainThread); 19 20 System. out. println (Thread. currentThread (). getName () + "continue"); 21} 22 23 static class ThreadA extends Thread {24 25 public ThreadA (String name) {26 super (name ); 27} 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} copy the code running result: main start tamain blockta wakup othersmain continue

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.