current

current包下ReentrantReadWriteLock的使用

与世无争的帅哥 提交于 2020-04-12 14:02:47
ReentrantReadWriteLock中可以生产读锁和写锁。 读锁和读锁不互斥,写锁和任何读锁或者写锁都互斥。 demo:读锁和读锁不互斥 public static void main(String[] args) { ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(); final ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock(); final ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock(); for (int i = 0; i < 5; i++) { new Thread(new Runnable() { public void run() { try { readLock.lock(); System.out.println(Thread.currentThread().getName() + " 开始读取数据"); TimeUnit.SECONDS.sleep(5); System.out.println(Thread.currentThread()