美文网首页
LockSupport

LockSupport

作者: IAmWhoAmI | 来源:发表于2018-04-27 20:27 被阅读2次
    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

    相关文章

      网友评论

          本文标题:LockSupport

          本文链接:https://www.haomeiwen.com/subject/iysrlftx.html