Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use LockSupport class in Java concurrent programming

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use the LockSupport class in Java concurrent programming". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the LockSupport class in Java concurrent programming" can help you solve the problem.

1. The property private static final sun.misc.Unsafe UNSAFE; / / of the Locksupport class indicates the memory offset address private static final long parkBlockerOffset; / / indicates the memory offset address private static final long SEED; / / indicates the memory offset address private static final long PROBE; / / indicates the memory offset address private static final long SECONDARY Static {try {/ / get Unsafe instance UNSAFE = sun.misc.Unsafe.getUnsafe (); / / Thread class type Class tk = Thread.class; / / get the memory offset address of Thread's parkBlocker field parkBlockerOffset = UNSAFE.objectFieldOffset (tk.getDeclaredField ("parkBlocker")) / / get the memory offset address of the threadLocalRandomSeed field of Thread SEED = UNSAFE.objectFieldOffset (tk.getDeclaredField ("threadLocalRandomSeed")); / / get the memory offset address of the threadLocalRandomProbe field of Thread PROBE = UNSAFE.objectFieldOffset (tk.getDeclaredField ("threadLocalRandomProbe")) / / get the memory offset address of the threadLocalRandomSecondarySeed field of Thread SECONDARY = UNSAFE.objectFieldOffset (tk.getDeclaredField ("threadLocalRandomSecondarySeed"));} catch (Exception ex) {throw new Error (ex) Second, the constructor / / private constructor of the Locksupport class cannot be instantiated private LockSupport () {} III. When the park (Object blocker) method and the park () method analyze / / call the park function, the current thread first sets the parkBlocker field, and then calls the Unsafe's park function / / since then, the current thread has blocked, waiting for the thread's unpark function to be called. So the latter setBlocker function cannot run / / unpark function is called, once the thread is licensed, it can continue to run, and then run the second setBlocker / / to set the thread's parkBlocker field to null, thus completing the logic of the entire park function. / / in short, it is important to ensure that after the entire park (Object blocker) function is executed, the thread's parkBlocker field returns to null. / / Block the current thread and set the parkBlocker field of the current thread to blocker public static void park (Object blocker) {/ / get the current thread Thread t = Thread.currentThread (); / / set the parkBlocker field of the current thread to blocker setBlocker (t, blocker) / / blocking the current thread, the first parameter indicates isAbsolute, whether it is an absolute time, and the second parameter is UNSAFE.park (false, 0L); / / set Blocker setBlocker (t, null) after it can be run again;} / / infinitely block the thread until another thread calls the unpark method public static void park () {UNSAFE.park (false, 0L) 4. ParkNanos (Object blocker,long nanos) method and parkNanos (long nanos) method analyze / / block the current thread nanos millisecond public static void parkNanos (Object blocker,long nanos) {/ / first determine whether nanos is greater than 0, less than or equal to 0 means infinite waiting for if (nanos > 0) {/ / get the current thread Thread t = Thread.currentThread () / / set the parkBlocker field of the current thread to blocker setBlocker (t, blocker); / / block the nanos seconds UNSAFE.park of the current thread (false, nanos); / / set the parkBlocker field of the current thread to null setBlocker (t, null) }} / / blocking the current thread nanos millisecond public static void parkNanos (long nanos) {if (nanos > 0) UNSAFE.park (false, nanos) } 5. ParkUntil (Object blocker,long deadline) method and parkUntil (long deadline) method analyze / / set the current thread's parkBlockerOffset to blocker public static void parkUntil (Object blocker,long deadline) {/ / get the current thread Thread t = Thread.currentThread (); / / set the current thread parkBlocker field to blocker setBlocker (t, blocker) / / deadline seconds UNSAFE.park (true, deadline) blocking the current thread absolute time; / / current thread parkBlocker field is set to null setBlocker (t, null);} / / deadline seconds public static void parkUntil (long deadline) {UNSAFE.park (true, deadline) of the current thread blocking absolute time } VI. SetBlocker (Thread t, Object arg) and getBlocker (Thread t) methods analyze / / set the value of the parkBlocker field of thread t to arg private static void setBlocker (Thread t, Object arg) {UNSAFE.putObject (t, parkBlockerOffset, arg) } / / get the current thread's Blocker value public static Object getBlocker (Thread t) {/ / throw an exception if (t = = null) throw new NullPointerException () if the current thread is empty; / / use the unsafe object to get the current thread's Blocker value return UNSAFE.getObjectVolatile (t, parkBlockerOffset) 7. The unpark (Thread thread) method analyzes / / releases the blocking state of the thread, that is, it is similar to releasing the lock, except that the license is set to 1 public static void unpark (Thread thread) {/ / the thread is not empty if (thread! = null) / / release the thread license UNSAFE.unpark (thread);} VIII. LockSupport advantages

LockSupport has two advantages over Object's wait/notify.

1.LockSupport does not need to be in the synchronization code block. Therefore, there is no need to maintain a shared synchronization object between threads, and the decoupling between threads is realized.

The 2.unpark function can be called before park, so you don't have to worry about the order of execution between threads.

This is the end of the introduction to "how to use the LockSupport class in Java concurrent programming". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report