LockSupport

Disables the current thread for thread scheduling purposes unless the permit is available.
     
If the permit is available then it is consumed and the call returns immediately; 
otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
     1.Some other thread invokes #unpark with the current thread as the target; 
     2.Some other thread Thread#interrupt the current thread; 
     3.The call spuriously (that is, for no reason) returns.
     This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the thread to park in the first place. Callers may also determine, for example, the interrupt status of the thread upon return.
    
    public static void park(Object blocker) {
        Thread t = Thread.currentThread();
        setBlocker(t, blocker);
        UNSAFE.park(false, 0L);
        setBlocker(t, null);
    }

这里setBlocker是为了查问题的时候可以知道在哪里阻塞的作用。

image.png

聊起了hotspot如何实现park和unpark,这里有一点可以知道park 和unpark 通过 set volatile int _counter =0 或 =1 进行通信:
https://blog.csdn.net/hengyunabc/article/details/28126139
https://blog.csdn.net/hengyunabc/article/details/27969613

locksupport Api 文档
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/LockSupport.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容