In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "Java's LockSupport tool class how to use", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "Java's LockSupport tool class how to use" can help you solve your doubts.
LockSupport class
A basic thread blocking primitive used to create locks and other synchronization classes, which are associated with a license for each thread that uses it. If granted, the call to park will be returned immediately and consumed in the process; otherwise may will be blocked. Call unpark to make the license available if it is not already available. (however, unlike semaphores, licenses do not accumulate. There can only be one at most. )
Methods park and unpark provide effective ways to block and unblock threads that do not encounter problems that prevent the deprecated methods Thread.suspend and Thread.resume from being used: due to permission, competition between one thread calling park and another thread trying to unpark will remain alive. In addition, if the caller's thread is interrupted and the timeout version is supported, park returns. The park method can also return at any other time for "no reason", so you usually have to call the loop in the loop, which rechecks the condition when it returns. In this sense, park is an optimization of "busy waiting". It doesn't waste much time and must be paired with unpark to take effect.
Now that we know what it does, let's take a look at the relevant API.
LockSupport.park ()
Park (): unless licensed, disable the current thread for thread scheduling purposes.
If the license is available, it is consumed and the call is returned immediately; otherwise, the call is returned immediately. For thread scheduling purposes, the current thread is disabled and dormant until one of the following three situations occurs:
1. Other threads call the unpark () method targeting the current thread
two。 Other threads Threadinterrupt interrupts current thread
3. Falsely call the return
Source code:
Public static void park () {UNSAFE.park (false, 0L);}
LockSupport is the method of calling UNSAFE. Let's first look at the park method.
Public class LockSupportMain {public static void main (String [] args) {System.out.println ("begin park!"); LockSupport.park (); System.out.println ("end park!");}}
We called the park method, which caused the Main thread to be blocked and never finished, because by default, the calling thread does not hold a license, what can we do about it? There are three ways mentioned above, we verify them one by one.
1. Call the unpack method to get permission
Unpack (): make a given thread available if it is not yet available. If a thread is blocked on park, it will unblock it. Otherwise, it ensures that its next call to park does not block. If a given thread has not been started, there is no guarantee that this operation is completely invalid.
Source code:
Public static void unpark (Thread thread) {if (thread! = null) UNSAFE.unpark (thread);} public class LockSupportMain2 {public static void main (String [] args) {System.out.println ("begin park!"); LockSupport.unpark (Thread.currentThread ()); LockSupport.park (); System.out.println ("end park!");}}
As you can see, the current thread main has been released without blocking, and the run is complete.
We create a thread that has the Main thread to unpark the thread to run in the case of blocking.
Public class LockSupportMain3 {public static void main (String [] args) throws InterruptedException {Runnable runnable = new Runnable () {@ Override public void run () {System.out.println ("begin start thread name:" + Thread.currentThread () .getName () + "park"); LockSupport.park () System.out.println ("end start thread name:" + Thread.currentThread (). GetName () + "park");}}; Thread thread = new Thread (runnable); thread.start (); Thread.sleep (2000); System.out.println ("main thread call unpark"); LockSupport.unpark (thread);}}
As you can see from the running results, some other threads calling the unpark () method targeting the current thread can cause the thread's park to continue to run blocked.
two。 Call the interrupt interrupts method to get permission
Since the park method will not tell you the reason for the return, the caller needs to check again whether the condition is met according to the reason why the park method was called before, and if not, the park method needs to be called.
Public class LockSupportMain4 {public static void main (String [] args) throws InterruptedException {Runnable runnable = new Runnable () {@ Override public void run () {System.out.println ("begin start thread name:" + Thread.currentThread (). GetName () + "park"); while (! Thread.currentThread (). IsInterrupted ()) {LockSupport.park () } System.out.println ("end start thread name:" + Thread.currentThread (). GetName () + "park");}}; Thread thread = new Thread (runnable); thread.start (); Thread.sleep (2000); / / keep park blocking running thread.interrupt () through the interrupt method;}}
As can be seen from the run, some other threads Threadinterrupt interrupts the current thread that can block the park to continue to run.
ParkNanos (long nanos)
ParkNanos (long nanos): disables the current thread for thread scheduling for the specified wait time, unless permission is allowed.
Source code:
Public static void parkNanos (long nanos) {if (nanos > 0) UNSAFE.park (false, nanos);} public class LockSupportMain5 {public static void main (String [] args) throws InterruptedException {Runnable runnable = new Runnable () {@ Override public void run () {System.out.println ("begin start thread name:" + Thread.currentThread (). GetName () + "park") LockSupport.parkNanos (3000000000L); System.out.println ("end start thread name:" + Thread.currentThread (). GetName () + "park");}}; Thread thread = new Thread (runnable); thread.start ();}}
After three seconds, block for three seconds and continue to run.
Park (Object blocker)
Park (Object blocker): unless licensed, disable the current thread for thread scheduling purposes
Source code:
Public static void park (Object blocker) {Thread t = Thread.currentThread (); setBlocker (t, blocker); UNSAFE.park (false, 0L); setBlocker (t, null);}
The blocker object here is the blocker object in the Thread class. The code is as follows:
/ / parameters provided to java.util.concurrent.locks.LockSupport.park for the current call. / / set by (private) java.util.concurrent.locks.LockSupport.setBlocker to access volatile Object parkBlocker;parkNanos using / / java.util.concurrent.locks.LockSupport.getBlocker (Object blocker, long nanos)
Source code:
Public static void parkNanos (Object blocker, long nanos) {if (nanos > 0) {Thread t = Thread.currentThread (); setBlocker (t, blocker); UNSAFE.park (false, nanos); setBlocker (t, null);} parkUntil (Object blocker, long deadline)
ParkUntil (Object blocker, long deadline): unless a license is specified, the current thread is disabled for thread scheduling until the specified deadline.
Source code:
Public static void parkUntil (Object blocker, long deadline) {Thread t = Thread.currentThread (); setBlocker (t, blocker); UNSAFE.park (true, deadline); setBlocker (t, null);}
Let's take a look at the example in java API:
Public class LockSupportMain6 {private final AtomicBoolean locked = new AtomicBoolean (false); private final Queue waiters = new ConcurrentLinkedQueue (); public void lock () {boolean wasInterrupted = false; Thread current = Thread.currentThread (); waiters.add (current) / / block first or cannot get lock while when not in queue (waiters.peek ()! = current | |! locked.compareAndSet (false, true)) {LockSupport.park (this) / / ignore interrupts while waiting if the park method returns because it was interrupted, ignore interrupts and reset the interrupt flag to mark if (Thread.interrupted ()) {wasInterrupted = true;}} waiters.remove () / / redeclare the interrupt status if (wasInterrupted) {current.interrupt ();}} public void unlock () {locked.set (false); LockSupport.unpark (waiters.peek ());}} when exiting
With blocker, you can pass more on-site information to developers, you can view the blocking objects of the current thread, and it is convenient to locate the problem.
After reading this, the article "how to use Java's LockSupport tools" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about the article, please follow the industry information channel.
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: 280
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.