[Java source code learning-multithreading] LockSupport (1), javalocksupport

Source: Internet
Author: User

[Java source code learning-multithreading] LockSupport (1), javalocksupport

Today, I started to learn the Java multi-threaded source code and recorded what I learned. Due to my limited personal ability, there are inevitable errors in this article. I hope my friends on the blog can point out my mistakes, thank you! At the same time, I hope I can stick to it and cheer up!

To learn java multithreading, you must first understand some principles, such as memory visibility, atomicity, re-sorting, and Java memory model. Because you are learning Java multithreading source code, so I won't write these things. I will have the opportunity to write a blog later. This series will mainly learn java. util. the source code under the concurrent package first learns the locks in java, that is, java. util. concurrent. the class under locks is the class structure under the locks package:

The core class in this package is the AbstractQueuedSynchronizer class. This class is the implementation of synchronization. The implementation of locks in Java is implemented through an internal aggregation of a synchronization. Therefore, we learn Java locks, first, you need to learn the role and principle of the synchronization machine. Before learning the synchronization machine, you must first learn the LockSupport class. The methods in this class are mainly used to block threads and wake up threads.

I. member variables in the class
1 // Unsafe member variable 2 private static final sun. misc. unsafe UNSAFE; 3 // The offset address of parkBlocker of Thread class 4 private static final long parkBlockerOffset; 5 // The offset address of threadLocalRandomSeed of Thread class 6 private static final long SEED; 7 // threadLocalRandomProbe offset address of Thread class 8 private static final long PROBE; 9 // threadLocalRandomSecondarySeed offset address of Thread class 10 private static final long SECONDARY; 11 // The above long types are all memory offset addresses, With this offset address, you can obtain or set this value to 12 static {13 try {14 UNSAFE = sun. misc. Unsafe. getUnsafe (); 15 Class <?> Tk = Thread. class; 16 // UNSAFE. objectFieldOffset is used to obtain the offset address of the field 17 parkBlockerOffset = UNSAFE. objectFieldOffset18 (tk. getDeclaredField ("parkBlocker"); 19 SEED = UNSAFE. objectFieldOffset20 (tk. getDeclaredField ("threadLocalRandomSeed"); 21 PROBE = UNSAFE. objectFieldOffset22 (tk. getDeclaredField ("threadLocalRandomProbe"); 23 SECONDARY = UNSAFE. objectFieldOffset24 (tk. getDeclaredField ("threadLocalRandomSecondarySeed"); 25} catch (Exception ex) {throw new Error (ex);} 26}

Each Thread has a member variable, such as parkBlocker. parkBlocker indicates the object on which the Thread is blocked, when analyzing the problem, you can print the member variable to check the object on which the thread is blocked. The meaning of the other variables is not clear. Here we use UNSAFE. objectFieldOffset can be used to obtain the memory offset between a field and a thread object. This memory offset can be used to quickly obtain or set the value of this member variable.

Ii. Thread blocking methods
1 // obtain parkBlocker 2 public static Object getBlocker (Thread t) of Thread t {3 if (t = null) 4 throw new NullPointerException (); 5 return UNSAFE. getObjectVolatile (t, parkBlockerOffset); 6} 7 8 // set the value of the parkBlocker field of Thread t to arg 9 private static void setBlocker (Thread t, Object arg) {10 // parkBlockerOffset is the memory offset of parkBlocker 11 UNSAFE. putObject (t, parkBlockerOffset, arg); 12} 13 14 public static void park () {15 // UNISAFE. the part method is used to block a thread. The first parameter indicates whether it is absolute time, and the second parameter is 0, indicating unlimited waiting for 16 UNSAFE. park (false, 0L); 17} 18 19 public static void park (Object blocker) {20 Thread t = Thread. currentThread (); 21 // set the thread t's blocker field 22 setBlocker (t, blocker); 23 // block the thread, so the following setBlocker will not run 24 UNSAFE. park (false, 0L); 25 // The second setBlocker Method 26 setBlocker (t, null) will be executed only when the unpark method is called or the thread is interrupted ); 27} 28 29 // the nanos time-out period is 30 public static void parkNanos (long nanos) {31 if (nanos> 0) 32 UNSAFE. park (false, nanos); 33} 34 35 public static void parkNanos (Object blocker, long nanos) {36 if (nanos> 0) {37 Thread t = Thread. currentThread (); 38 setBlocker (t, blocker); 39 UNSAFE. park (false, nanos); 40 setBlocker (t, null); 41} 42} 43 44 public static void parkUntil (long deadline) {45 // the absolute time used here is 46 UNSAFE. park (true, deadline); 47} 48 49 public static void parkUntil (Object blocker, long deadline) {50 Thread t = Thread. currentThread (); 51 setBlocker (t, blocker); 52 UNSAFE. park (true, deadline); 53 setBlocker (t, null); 54}

Thread blocking methods can be divided into two categories: parkBlocker and parkBlocker. There are three methods for each type: one is to permanently block the thread until the thread is awakened or interrupted; the other is to increase the timeout time and use relative time; one is to increase the timeout time, but the absolute time is used.

Iii. Method of awakening a thread
1 public static void unpark (Thread thread) {2 if (thread! = Null) 3 // UNSAFE. unpark is used to wake up a thread, but this method must ensure that the incoming thread must exist 4 UNSAFE. unpark (thread); 5}

There is also a method nextSecondarySeed in the LockSupport class. This method is used to generate random numbers, but it is not clear about its usefulness. If you know it, you may want to tell me.

Related Article

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.