ReentrantReadWriteLock-读写锁
特点 1、读读并发
2、读写互斥
3、写写互斥
4、读锁 不支持条件 ---> throw new UnsupportedOperationException();
用法
tatic ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); static Lock r = rwl.readLock(); static Lock w = rwl.writeLock();public static void main(String[] args) throws InterruptedException {new Thread(()->{log.debug("read 获取 锁");r.lock();try {for (int i = 0; i < 10; i++) {m1(i);}}finally {r.unlock();}},"t1").start();new Thread(()->{log.debug("write 获取 锁");w.lock();try {for (int i = 0; i < 20; i++) {m1(i);}}finally {w.unlock();}},"t2");new Thread(()->{log.debug("write 获取 锁");r.lock();try {for (int i = 0; i < 20; i++) {m1(i);}}finally {r.unlock();}},"t3").start();}public static void m1(int i){log.debug("exe"+i);try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}
执行结果
18:36:02.217 [t1] DEBUG com.fala.base.lock.LockTest - read 获取 锁
18:36:02.217 [t3] DEBUG com.fala.base.lock.LockTest - write 获取 锁
18:36:02.220 [t1] DEBUG com.fala.base.lock.LockTest - exe0
18:36:02.220 [t3] DEBUG com.fala.base.lock.LockTest - exe0
18:36:03.221 [t1] DEBUG com.fala.base.lock.LockTest - exe1
18:36:03.222 [t3] DEBUG com.fala.base.lock.LockTest - exe1
18:36:04.221 [t1] DEBUG com.fala.base.lock.LockTest - exe2
18:36:04.223 [t3] DEBUG com.fala.base.lock.LockTest - exe2
18:36:05.221 [t1] DEBUG com.fala.base.lock.LockTest - exe3
18:36:05.223 [t3] DEBUG com.fala.base.lock.LockTest - exe3
18:36:06.221 [t1] DEBUG com.fala.base.lock.LockTest - exe4
18:36:06.223 [t3] DEBUG com.fala.base.lock.LockTest - exe4
18:36:07.221 [t1] DEBUG com.fala.base.lock.LockTest - exe5
18:36:07.223 [t3] DEBUG com.fala.base.lock.LockTest - exe5
18:36:08.221 [t1] DEBUG com.fala.base.lock.LockTest - exe6
18:36:08.223 [t3] DEBUG com.fala.base.lock.LockTest - exe6
18:36:09.221 [t1] DEBUG com.fala.base.lock.LockTest - exe7
18:36:09.223 [t3] DEBUG com.fala.base.lock.LockTest - exe7
18:36:10.221 [t1] DEBUG com.fala.base.lock.LockTest - exe8
18:36:10.223 [t3] DEBUG com.fala.base.lock.LockTest - exe8
18:36:11.221 [t1] DEBUG com.fala.base.lock.LockTest - exe9
18:36:11.223 [t3] DEBUG com.fala.base.lock.LockTest - exe9
18:36:12.223 [t3] DEBUG com.fala.base.lock.LockTest - exe10
18:36:13.225 [t3] DEBUG com.fala.base.lock.LockTest - exe11
18:36:14.225 [t3] DEBUG com.fala.base.lock.LockTest - exe12
18:36:15.225 [t3] DEBUG com.fala.base.lock.LockTest - exe13
18:36:16.225 [t3] DEBUG com.fala.base.lock.LockTest - exe14
18:36:17.225 [t3] DEBUG com.fala.base.lock.LockTest - exe15
18:36:18.225 [t3] DEBUG com.fala.base.lock.LockTest - exe16
18:36:19.225 [t3] DEBUG com.fala.base.lock.LockTest - exe17
18:36:20.225 [t3] DEBUG com.fala.base.lock.LockTest - exe18
18:36:21.225 [t3] DEBUG com.fala.base.lock.LockTest - exe19
可见 t1 和t3 并发执行,t2单独执行
2、读写锁支持重入,但是只支持降级不支持升级
降级代码
static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); static Lock r = rwl.readLock(); static Lock w = rwl.writeLock();public static void main(String[] args) throws InterruptedException {new Thread(() -> {log.debug("read");w.lock();try {log.debug("read 已经获取");r.lock();log.debug("write 已经获取");} finally {r.unlock();w.unlock();}}, "t1").start();}
输出结果
18:40:02.638 [t1] DEBUG com.fala.base.lock.LockTest - read
18:40:02.643 [t1] DEBUG com.fala.base.lock.LockTest - read 已经获取
18:40:02.643 [t1] DEBUG com.fala.base.lock.LockTest - write 已经获取