美文网首页
Lock和LockSupport

Lock和LockSupport

作者: 摆渡时光 | 来源:发表于2019-04-22 11:31 被阅读0次

一、Lock接口

java.util.concurrent.locks.Lock只是一个接口,看下有哪些方法

public interface Lock {
    void lock();
    void lockInterruptibly() throws InterruptedException;
    boolean tryLock();
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
    void unlock();
    Condition newCondition();
}

上面几个方法接口定义已经很明确了,ReentrantLock和ReadWriteLock是此接口的实现。

二、LockSupport类

LockSupport包含了以下方法,基本围绕着park和unpark,park的作用是给线程上锁,也就是阻塞,unpark方法是释放锁。

public class LockSupport {
    public static void unpark(Thread thread)
    public static void park(Object blocker)
    public static void parkNanos(Object blocker, long nanos)
    public static void parkUntil(Object blocker, long deadline)
    public static Object getBlocker(Thread t) 
    public static void park()
    public static void parkNanos(long nanos)
    public static void parkUntil(long deadline)
}

看下park方法的实现

/**
     * Disables the current thread for thread scheduling purposes unless the
     * permit is available.
     *
     * <p>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:
     *
     * <ul>
     * <li>Some other thread invokes {@link #unpark unpark} with the
     * current thread as the target; or
     *
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
     * the current thread; or
     *
     * <li>The call spuriously (that is, for no reason) returns.
     * </ul>
     *
     * <p>This method does <em>not</em> 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.
     *
     * @param blocker the synchronization object responsible for this
     *        thread parking
     * @since 1.6
     */
    public static void park(Object blocker) {
        Thread t = Thread.currentThread();
        setBlocker(t, blocker);
        UNSAFE.park(false, 0L);
        setBlocker(t, null);
    }

blocker是用来表示当前线程,当发生死锁时,通过blocker对象上下文,可以追踪是具体的哪一个线程以及对应的代码。

  1. 值得注意的是当执行上述park方法时,执行到UNSAFE.park(false, 0L);这一行时,会卡住,因为当前线程被阻塞了。所以下一行代码 setBlocker(t, null);不会立即执行。只有调用unpark方法之后,setBlocker(t, null);这行代码才会执行。
  2. 另外unpark方法允许出现在park方法调用前。也就是先调用unpark后,再调用park方法,会直接通过,不会发生线程阻塞。想理解这个,首先要理解LockSupport中许可permit的概念。也就是park方法注释“Disables the current thread for thread scheduling purposes unless the permit is available.”。许可允许提前获得,一旦获得许可后,执行park方法就不会发生阻塞。

相关文章

  • Lock和LockSupport

    一、Lock接口 java.util.concurrent.locks.Lock只是一个接口,看下有哪些方法 上面...

  • JUC面试问题(第三季)

    LockSupport AQS AQS源码解读 lock() acquire() tryAcuqire() add...

  • 第五章_Java 中的锁

    目录 Lock 接口 队列同步器 重入锁 读写锁 LockSupport 工具 Condition 接口 Lock...

  • J.U.C之LockSupport:线程等待和唤醒

    1 LockSupport 概述 LockSupprot是线程的阻塞原语,用来阻塞线程和唤醒线程。每个使用Lock...

  • Java并发编程-Java中的锁

    主要内容有:Lock接口队列同步器重入锁读写锁LockSupport工具Condition接口 1.Lock接口 ...

  • Java之ReentrantLock的实现

    摘要 Lock在硬件层面依赖CPU指令,完全由Java代码完成,底层利用LockSupport类和Unsafe类进...

  • LockSupport工具

    1. LockSupport简介 在之前介绍AQS的底层实现,已经在介绍java中的Lock时,比如Reentra...

  • 6. LockSupport

    回顾前面部分的源码分析,当需要阻塞或唤醒一个线程的时候,都会使用LockSupport工具来完成相应工作。Lock...

  • Java CAS

    CAS和LockSupport可以说贯穿了java并发包(自旋锁 + CAS + LockSupport + 内存...

  • LockSupport实现原理

    前文中了解到AQS借助LockSupport.park和LockSupport.unpark完成线程的阻塞和唤醒,...

网友评论

      本文标题:Lock和LockSupport

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