LockSupport explained in detail. It took 6 months to successfully join Alibaba

Keywords: Java Android Back-end Programmer

  1. void park(): blocks the current thread. If the unpark method is called or the current thread is interrupted, it can be returned from the park() method

  2. void park(Object blocker): the function is the same as method 1. An Object object is added to the input parameter to record the blocking objects that cause thread blocking, so as to facilitate troubleshooting;

  3. void parkNanos(long nanos): blocks the current thread for no more than nanoseconds, adding the feature of timeout return;

  4. void parkNanos(Object blocker, long nanos): the function is the same as method 3. An Object object is added to the input parameter to record the blocking objects that cause thread blocking, so as to facilitate problem troubleshooting;

  5. void parkUntil(long deadline): blocks the current thread and knows the deadline;

  6. void parkUntil(Object blocker, long deadline): the function is the same as method 5. An Object object is added to the input parameter to record the blocking objects that cause thread blocking, so as to facilitate problem troubleshooting;

Wake up thread

  1. void unpark(Thread thread): wakes up the specified thread in the blocked state

In fact, the function of LockSupport to block and wake up threads depends on sun.misc.Unsafe, which is a very low-level class. If you are interested, you can consult the data. For example, the function implementation of the park() method depends on the unsafe.park() method. In addition, another interesting phenomenon in the series of methods of blocking threads is that each method will add an overloaded method of blocking Object with Object. So what's the difference when you add an Object object? The sample code is very simple. Let's just look at the dump thread information.

Call the park() method dump thread:

"main" #1 prio=5 os_prio=0 tid=0x02cdcc00 nid=0x2b48 waiting on condition [0x00d6f000]

   java.lang.Thread.State: WAITING (parking)

        at sun.misc.Unsafe.park(Native Method)

        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:304)

        at learn.LockSupportDemo.main(LockSupportDemo.java:7) 

Call the park(Object blocker) method dump thread

"main" #1 prio=5 os_prio=0 tid=0x0069cc00 nid=0x6c0 waiting on condition [0x00dcf000]

   java.lang.Thread.State: WAITING (parking)

        at sun.misc.Unsafe.park(Native Method)

        - parking to wait for  <0x048c2d18> (a java.lang.String)

        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)

        at learn.LockSupportDemo.main(LockSupportDemo.java:7) 

By calling these two methods respectively and then dump the thread information, it can be seen that the park method with Object will increase the information of parking to wait for < 0x048c2d18 > (a Java. Lang. string) compared with the park method without parameters, which is similar to recording the "crime scene" , which helps engineers to quickly find and solve problems. An interesting thing is that we all know that if synchronized blocks threads, dump threads will have descriptions of blocking objects. This was omitted when java 5 launched LockSupport, which was supplemented in java 6. Another point to be added is that synchronized causes threads to block, and threads will enter Entering the BLOCKED state, and calling the LockSupprt method to block the thread will cause the thread to enter the WAITING state

An example

Tell me how to use these methods with a very simple example.

public class LockSupportDemo {

    public static void main(String[] args) {

        Thread thread = new Thread(() -> {

            LockSupport.park();



# summary

If you choose IT The industry is not firmly going forward. There must be no problem in this direction. This is a high salary industry, but the high salary is obtained by studying hard. This time I P8 Some learning notes used by the boss( pdf)It's all sorted out in this article

**[CodeChina Open source project: [first tier big factory] Java Analysis of interview questions+Core summary learning notes+Latest explanation Video]](https://codechina.csdn.net/m0_60958482/java-p7)**

**<Java Comprehensive analysis of intermediate and advanced core knowledge**

![](https://img-blog.csdnimg.cn/img_convert/d7ad292b656a721579600eadc17f66b9.png)

**Xiaomi shopping mall project actual combat, don't worry that there is no actual combat project in the interview:**

a-p7)**

**<Java Comprehensive analysis of intermediate and advanced core knowledge**

[External chain picture transfer...(img-HfPuNSAo-1631064280815)]

**Xiaomi shopping mall project actual combat, don't worry that there is no actual combat project in the interview:**

![](https://img-blog.csdnimg.cn/img_convert/ce5f65759882b907654d6d54eecc91da.png)

Posted by quikone on Wed, 24 Nov 2021 04:23:54 -0800