ReentrantReadWriteLock(读写锁)
使用:
> ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
> readWriteLock.readLock().lock();//拿读锁
> boolean res = readWriteLock.readLock().tryLock();//拿到读锁返回true,没拿到返回false
> boolean res = readWriteLock.readLock().tryLock(10,TimeUnit.SECONDS);//10秒内拿到读锁返回true,否则没拿到返回false
> readWriteLock.readLock().unlock();//释放读锁
> readWriteLock.writeLock().lock();//拿写锁
> boolean res = readWriteLock.writeLock().tryLock();//拿到写锁返回true,没拿到返回false
> boolean res = readWriteLock.writeLock().tryLock(10,TimeUnit.SECONDS);//10秒内拿到写锁返回true,否则没拿到返回false
> readWriteLock.writeLock().unlock();//释放写锁