并发编程--读写锁ReadWriteLock和ReentrantReadWriteLock(一)

Java并发编程包提供了读写锁的实现,其维护了一对相关的锁 — — “读取锁”和“写入锁”,一个用于读取操作,另一个用于写入操作。

“读取锁”用于只读操作,它是“共享锁”,能同时被多个线程获取。

“写入锁”用于写入操作,它是“独占锁”,写入锁只能被一个线程锁获取。

ReadWriteLock是一个接口。ReentrantReadWriteLock是它的实现类,ReentrantReadWriteLock包括子类ReadLock和WriteLock。

读写锁接口ReadWriteLock:

(1)readLock获取一个读锁

(2)writeLock获取一个写锁

public interface ReadWriteLock {

    Lock readLock();

    Lock writeLock();
}

在实现类ReentrantReadWriteLock中并发编程包分别实现了一个读锁ReadLock和写锁WriteLock,其实ReadLock和WriteLock差别不大,比较明显的差别是ReadLock获取锁时是调用acquireShared方法

public void lock() {
          sync.acquireShared(1);
        }
WriteLock获取锁的方法是sync.acquire(1)

 public void lock() {
            sync.acquire(1);
        }
我们知道读锁是一个共享锁,写锁是一个独占锁,具体的实现区别应该是在acquireShared和acquire中体现的。

读锁ReadLock源码:

    public static class ReadLock implements Lock, java.io.Serializable {
        private static final long serialVersionUID = -5992448646407690164L;
        private final Sync sync;
        protected ReadLock(ReentrantReadWriteLock lock) {
            sync = lock.sync;
        }
        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();
        }
        public String toString() {
            int r = sync.getReadLockCount();
            return super.toString() +
                "[Read locks = " + r + "]";
        }
    }
写锁WriteLock源码:

    public static class WriteLock implements Lock, java.io.Serializable {
        private static final long serialVersionUID = -4992448646407690164L;
        private final Sync sync;

        protected WriteLock(ReentrantReadWriteLock lock) {
            sync = lock.sync;
        }

       
        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 String toString() {
            Thread o = sync.getOwner();
            return super.toString() + ((o == null) ?
                                       "[Unlocked]" :
                                       "[Locked by thread " + o.getName() + "]");
        }

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

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





你可能感兴趣的:(Java-并发编程,Java并发编程)