Lock源码分析

Lock源码分析

/**
 * Create by ~JH~ on 2018/4/13
 */
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;

/**
 *实现Lock类可以获得在synchronized方法和声明上专用的操作。
 * 他们允许更加灵活的结构可能会有相当不同的属性,并且可能支持多个有关系的环境对象。
 *锁是一个多线程控制对资源共享的工具,通常一个锁对一个共享资源提供专用的接入。
 * 在同一时间一个资源只有一个县城可以获得锁,并且所有对共享资源的解饿如都需要先获得锁。
 * 然而,一些锁允许同时接入一个共享资源,例如,读锁ReadWriteLock。
 *使用synchronized方法或声明,提供一种对其他关联的每一个对象隐形的监测锁,
 * 但是强制所有的锁需要以块结构的方式获取和释放:当多个锁被获得,他们必须按次序被释放,
 * 并且所有的锁必须被释放在他们获得的相同的词法作用域内
 *尽管作用与机制使得synchronized方法和声明使之更加简单在程序中使用用监视锁
 * 并且帮助避免的了许多普通的涉及锁的程序错误,有一些时候你需要一种更加灵活的方式使用锁。
 * 例如,一些遍历算法并发的访问数据结构,需要使用hand-over-hand或者是锁式链,你获得A B C
 * 释放B 获得D ...实现Lock接口可以通过允许一个锁在不同的作用域获得和释放,并且允许多个锁以任何次序被获得和释放。
 *
 *增加的灵活性来自附加的责任。缺乏块结构的锁,移除了在synchronized方法的自动释放锁,
 *  <pre> {@code
 * Lock l = ...;
 * l.lock();
 * try {
 *   // access the resource protected by this lock
 * } finally {
 *   l.unlock();
 * }}</pre>
 *
 *当上锁和释放锁的时候在不同的作用域,必须保证所有的代码被执行当这个锁被try-catch包围,
 * 保证锁被释放。
 *锁的实现类提供额外的函数在使用synchronized方法之上通过提供一个不阻塞尝试获得锁,
 * tryLock ,这个过程可能中断,可能超时。
 *Lock提供了行为和语义,这是和隐式的检测锁完全不同的,比如保证次序,不可重入的用法,
 *死锁检测。如果一个实现指定了词法。然后这个实现必须记录这些语法。
 *一个Lock的实例只是一个普通的对象并且可以作为一个目标在synchronized中使用,
 * 一个锁的实例获得监测锁,该实例和任何lock方法没有指定的关系。
 * <h3>Memory Synchronization</h3>
 *内存同步
 * 所有Lock实现必须强迫使用监测锁提供的相同的内存同步语法
 *一个成功的锁操作有相同的内存同步影响就像一个成功的Lock行为
 * 一个成功的释放锁操作有相同的内存影响就像一个成功的UnLok行为
 *这三种锁的获得 可中断 不可中断 超时 可能不同于她的性能特点 顺序保证 或者其他实现质量。
 * 更进一步,这个中断不间断探测获取锁的能力可能不可用在Lock类里面,
 *因此,一个实现是不需要额外定义相同的保证或者语法为这三种形式获得的锁。
 * 不间断获取锁也不需要支持中断。一个实现需要清晰地记录语法,和担保对每个提供给的lock方法
 * 必须遵守Lock定义的中断语法,为了扩展获取锁时中断的支持者是全部或只是在方法入口上。
 *由于中断通常意味着取消,并且检查中断通常是不常见的 一个实现可以支持在普通方法返回之后响应中断。
 *这是真是的即使他可以展示中断发生在其他可能不阻塞的线程的行为。一个实现应该记录这个行为。
 * @see ReentrantLock
 * @see Condition
 * @see ReadWriteLock
 *
 * @since 1.5
 * @author Doug Lea
 */
public interface Lock {

    /**
     * Acquires the lock.
     *如果lock是不可用的然后这个当前的线程变得有缺陷因为这个线程调度目的和处于休眠状态知道这个锁被获得
     * <p><b>Implementation Considerations</b>
     *Lock的实现可能可以使用lock监测错误,比如一次唤醒可能导致死锁,并且可能跑出一个unchecked异常在这种情形下。
     * 这种情形和异常类型必修被记录通过实现Lock
     */
    void lock();

    /**
     *获取这个锁 除非当前线程是被中断的
     *获取锁如果他是可用的并且立即返回
     *如果锁式不可用的然后这个当前线程变成不可用因为这个线程调度目的和处于休眠状态知道下面的两件事情中的一件发生

     *当前线程需要锁,或者一些其他线程中断了当前线程,并且支持获得锁中断

     * interrupted status is cleared.InterruptedException被抛出然后当前线程的中断状态就会被清除
     *如果当前线程有一个他自己的中断状态的集合字进入这个方法的时候,
     * 或者当前线程被中断了当获得这个锁的时候,并且这个获得中断锁的操作是被支持的
     * <p><b>Implementation Considerations</b>
     *
     * 中断一个获得锁的能力在一些实现不可能,并且如果可能成为一个高消费的操作。
     * 这个程序员应该意识到可能会成为case。实现应该记录这个case
     *
     *可以支持在普通方法返回之后响应中断。
     * <p>A {@code Lock} implementation may be able to detect
     * erroneous use of the lock, such as an invocation that would
     * cause deadlock, and may throw an (unchecked) exception in such
     * circumstances.  The circumstances and the exception type must
     * be documented by that {@code Lock} implementation.
     *
     * @throws InterruptedException if the current thread is
     *         interrupted while acquiring the lock (and interruption
     *         of lock acquisition is supported)
     */
    void lockInterruptibly() throws InterruptedException;

    /**
     *获取这个锁只有在调用的时候他是自由的
     *获取到的这个锁如果他是可用的就会立即返回true
     * 如果这个锁式不可用然后这个放啊就会立即返回false
     * <p>A typical usage idiom for this method would be:
     *  <pre> {@code
     * Lock lock = ...;
     * if (lock.tryLock()) {
     *   try {
     *     // manipulate protected state
     *   } finally {
     *     lock.unlock();
     *   }
     * } else {
     *   // perform alternative actions
     * }}</pre>
     *
     *这个用法确认这个锁在获取到时没有上锁,并且如果没有获取到锁不会尝试释放锁
     * @return {@code true} if the lock was acquired and
     *         {@code false} otherwise
     */
    boolean tryLock();

    /**
     * Acquires the lock if it is free within the given waiting time and the
     * current thread has not been {@linkplain Thread#interrupt interrupted}.
     *在等待的获取锁的过程中当前的锁是不能被中断的
     * 如果这个锁是不可用的然后当前线程就变成了不可用的线程调度和处于休眠状态,直到当前线程获得锁,或者其他线程中断了当前线程
     * 并且中断了获取锁的支持锁。或者指定的等待时间超过
     * <ul>
     * <li>The lock is acquired by the current thread; or
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
     * current thread, and interruption of lock acquisition is supported; or
     * <li>The specified waiting time elapses
     * </ul>
     *
     * <p>If the lock is acquired then the value {@code true} is returned.
     * <p>If the current thread:
     * <ul>
     * <li>has its interrupted status set on entry to this method; or
     * <li>is {@linkplain Thread#interrupt interrupted} while acquiring
     * the lock, and interruption of lock acquisition is supported,
     * </ul>
     * then {@link InterruptedException} is thrown and the current thread's
     * interrupted status is cleared.

     * <p>If the specified waiting time elapses then the valu
     *如果时间《=0,这个方法就会停止等待。
     * <p><b>Implementation Considerations</b>
     *
     *在一些实现里面中断获取锁是不可能的,如果变成可能那肯定是高消费的操作
     *一些实现可以支持普通方法返回后响应中断,或者报告超时
     * <p>A {@code Lock} implementation may be able to detect
     * erroneous use of the lock, such as an invocation that would cause
     * deadlock, and may throw an (unchecked) exception in such circumstances.
     * The circumstances and the exception type must be documented by that
     * {@code Lock} implementation.
     *
     * @param time the maximum time to wait for the lock
     * @param unit the time unit of the {@code time} argument
     * @return {@code true} if the lock was acquired and {@code false}
     *         if the waiting time elapsed before the lock was acquired
     *
     * @throws InterruptedException if the current thread is interrupted
     *         while acquiring the lock (and interruption of lock
     *         acquisition is supported)
     */
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;

    /**
     * Releases the lock.
     *
     * <p><b>Implementation Considerations</b>
     *
     * type must be documented by that {@code Lock} implementation.
     * 实现通常强加了一些限制一个县城必须释放锁并且可能跑出一个unchecked异常如果这些限制被违反。
     * 任何限制和一场都要遭实现类里面记录。
     */
    void unlock();

    /**
     * Returns a new {@link Condition} instance that is bound to this
     * {@code Lock} instance.
     *返回一个新的环境实例,这个实例被lock实例限制
     * <p>Before waiting on the condition the lock must be held by the
     * current thread.
     * A call to {@link Condition#await()} will atomically release the lock
     * before waiting and re-acquire the lock before the wait returns.
     *在等待和重新获得锁之前调用await方法将会原子的释放锁
     * <p><b>Implementation Considerations</b>
     *
     * <p>The exact operation of the {@link Condition} instance depends on
     * the {@code Lock} implementation and must be documented by that
     * implementation.
     *
     * @return A new {@link Condition} instance for this {@code Lock} instance
     * @throws UnsupportedOperationException if this {@code Lock}
     *         implementation does not support conditions
     */
    Condition newCondition();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值