摘要:本文主要介绍ReadWriteLock,可重入读写锁的基本使用,该锁只能在单服务实例中使用,不适合分布式多服务实例集群。
A ReadWriteLock维护一对关联的locks,一个用于只读操作,一个用于写操作。read lock可以由多个阅读器同时进行,只要没有作者 write lock 是独家的。
基本意思可分为如下几种场景
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
如果该锁被写锁占有,则该方法会一直等待锁
ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readWriteLock.readLock().lock();
如果该锁被写锁占有,第一个方法如果获取不到锁则返回false,第二个方法会等待你设置的时间,在你设置的时间范围内未获取到锁则返回false
ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readWriteLock.readLock().tryLock(); readWriteLock.readLock().tryLock(6L, TimeUnit.SECONDS);
如果该锁被写锁占有,则该方法会一直等待锁
ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readWriteLock.writeLock().lock();
如果该锁被写锁占有,第一个方法如果获取不到锁则返回false,第二个方法会等待你设置的时间,在你设置的时间范围内未获取到锁则返回false
ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readWriteLock.writeLock().tryLock(); readWriteLock.writeLock().tryLock(6L, TimeUnit.SECONDS);
该实例的读方法用了等待6S
public class LockManager { private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public void write(String message,Long sleep) { try{ if(readWriteLock.writeLock().tryLock()){ try{ Thread.sleep(sleep); System.out.println(Thread.currentThread().getName()+",写写写写写写写写写写,message="+message); }catch (Exception ex){ System.out.println(Thread.currentThread().getName()+",写入异常"+ex); }finally { readWriteLock.writeLock().unlock(); } }else{ System.out.println(Thread.currentThread().getName()+",获取写锁失败"); } }catch (Exception ex){ ex.printStackTrace(); } } public void read(String message,Long sleep){ try{ if(readWriteLock.readLock().tryLock(6L, TimeUnit.SECONDS)){ try{ Thread.sleep(sleep); System.out.println(Thread.currentThread().getName()+",读读读读读读读读读读,message="+message); }catch (Exception ex){ System.out.println(Thread.currentThread().getName()+",读取异常"+ex); }finally { readWriteLock.readLock().unlock(); } }else{ System.out.println(Thread.currentThread().getName()+",获取读锁失败"); } }catch (Exception ex){ ex.printStackTrace(); } } }
第一个线程等待5S输出,第二个线程等待4S输出
@Test public void testOnlyRead() throws Exception { LockManager lockManager = new LockManager(); new Thread(()->lockManager.read("001",5000L)).start(); new Thread(()->lockManager.read("002",4000L)).start(); Thread.sleep(20000L); }
Thread-2,读读读读读读读读读读,message=002
Thread-1,读读读读读读读读读读,message=001
第一个线程等待5S输出,第二个线程等待4S输出
@Test public void testOnlyWrite() throws Exception { LockManager lockManager = new LockManager(); new Thread(()->lockManager.write("001",5000L)).start(); new Thread(()->lockManager.write("002",4000L)).start(); Thread.sleep(20000L); }
Thread-2,获取写锁失败
Thread-1,写写写写写写写写写写,message=001
第一个线程等待5S输出,第二个线程等待4S输出
@Test public void testReadWrite() throws Exception { LockManager lockManager = new LockManager(); new Thread(()->lockManager.read("001",5000L)).start(); new Thread(()->lockManager.write("002",4000L)).start(); Thread.sleep(20000L); }
Thread-2,获取写锁失败
Thread-1,读读读读读读读读读读,message=001
第一个线程等待5S输出,第二个线程等待4S输出
@Test public void testWriteRead() throws Exception { LockManager lockManager = new LockManager(); new Thread(()->lockManager.write("001",5000L)).start(); new Thread(()->lockManager.read("002",4000L)).start(); Thread.sleep(20000L); }
Thread-1,写写写写写写写写写写,message=001
Thread-2,读读读读读读读读读读,message=002
第一个线程等待7S输出,第二个线程等待4S输出
@Test public void testWriteRead2() throws Exception { LockManager lockManager = new LockManager(); new Thread(()->lockManager.write("001",7000L)).start(); new Thread(()->lockManager.read("002",4000L)).start(); Thread.sleep(20000L); }
Thread-2,获取读锁失败
Thread-1,写写写写写写写写写写,message=001
以上就是Java读写锁ReadWriteLock的创建使用及测试分析示例详解的详细内容,更多关于Java读写锁ReadWriteLock的资料请关注脚本之家其它相关文章!
编程 | 2023-02-24 21:36
编程 | 2023-02-21 12:51
编程 | 2023-02-21 12:47
编程 | 2023-02-21 00:15
编程 | 2023-02-21 00:08
编程 | 2023-02-20 21:46
编程 | 2023-02-20 21:42
编程 | 2023-02-20 21:36
编程 | 2023-02-20 21:32
编程 | 2023-02-20 18:12
网友评论