读写锁-ReentrantReadWriteLock源码解读

本节一起学习ReentrantReadWriteLock类的源码:


1、首先,可以看到ReentrantReadWriteLock类实现了ReadWriteLock接口

public class ReentrantReadWriteLock
        implements ReadWriteLock, java.io.Serializable

读写锁ReadWriteLock接口定义了两个方法:

public interface ReadWriteLock {
    /**
     * Returns the lock used for reading.
     *
     * @return the lock used for reading
     */
    Lock readLock();

    /**
     * Returns the lock used for writing.
     *
     * @return the lock used for writing
     */
    Lock writeLock();
}


2、接着看类中定义的变量:

    /** Inner class providing readlock */
    private final ReentrantReadWriteLock.ReadLock readerLock;
    /** Inner class providing writelock */
    private final ReentrantReadWriteLock.WriteLock writerLock;
    /** Performs all synchronization mechanics */
    final Sync sync;

定义了一个读锁对象,一个写锁对象,一个实现同步,我们依次看一下内部类 : ReadLock、WriteLock、Sync


3.内部类:ReadLock


3.1 首先看类的定义,实现了Lock接口

public static class ReadLock implements Lock

Lock接口定义了几个方法:

    void lock();获取锁

    void lockInterruptibly() throws InterruptedException; 可中断的获取锁

    boolean tryLock();尝试获取锁,如果锁可用,则及时返回true,否则及时返回false

    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;如果在等待时间范围内且线程未中断,则尝试获取锁,获取到则返回true,否则返回false。

    void unlock();释放锁

    Condition newCondition(); 创建一个Condition --后续再跟这个接口。


3.2 类中的变量:

private final Sync sync;

3.3 构造函数:

        /**
         * Constructor for use by subclasses
         *
         * @param lock the outer lock object
         * @throws NullPointerException if the lock is null
         */
        protected ReadLock(ReentrantReadWriteLock lock) {
            sync = lock.sync;
        }

3.4 Lock接口方法实现:

        public void lock() {
            sync.acquireShared(1);
        }
        public void lockInterruptibly() throws InterruptedException {
            sync.acquireSharedInterruptibly(1);
        }
        public boolean tryLock() {
            return sync.tryReadLock();
        }
        public boolean tryLock(long timeout, TimeUnit unit)
                throws InterruptedException {
            return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
        }
        public void unlock() {
            sync.releaseShared(1);
        }

        public Condition newCondition() {
            throw new UnsupportedOperationException();
        }

可见,底层实现都是用的Sync类。


4.内部类:WriteLock

同读锁,具体Lock接口方法实现略不同:

        public void lock() {
            sync.acquire(1);
        }
        public void lockInterruptibly() throws InterruptedException {
            sync.acquireInterruptibly(1);
        }
        public boolean tryLock( ) {
            return sync.tryWriteLock();
        }
        public boolean tryLock(long timeout, TimeUnit unit)
                throws InterruptedException {
            return sync.tryAcquireNanos(1, unit.toNanos(timeout));
        }
        public void unlock() {
            sync.release(1);
        }
        public Condition newCondition() {
            return sync.newCondition();
        }

且读锁还有两个方法:

        public boolean isHeldByCurrentThread() {
            return sync.isHeldExclusively();
        }

官方翻译为:询问此写入锁是否由当前线程保持

        public int getHoldCount() {
            return sync.getWriteHoldCount();
        }

官方翻译为:询问当前线程持有的写入锁的个数


5.内部类:Sync

    执行所有同步机制,具体见另一篇博客

6. 构造方法

    默认使用的非公平版本

    /**
     * Creates a new {@code ReentrantReadWriteLock} with
     * default (nonfair) ordering properties.
     */
    public ReentrantReadWriteLock() {
        this(false);
    }

    /**
     * Creates a new {@code ReentrantReadWriteLock} with
     * the given fairness policy.
     *
     * @param fair {@code true} if this lock should use a fair ordering policy
     */
    public ReentrantReadWriteLock(boolean fair) {
        sync = fair ? new FairSync() : new NonfairSync();
        readerLock = new ReadLock(this);
        writerLock = new WriteLock(this);
    }

7.常用的方法

    public ReentrantReadWriteLock.WriteLock writeLock() { return writerLock; }
    public ReentrantReadWriteLock.ReadLock  readLock()  { return readerLock; }

猜你喜欢

转载自blog.csdn.net/dxh0823/article/details/80588596