The main research on ReadWriteLock features in this article, as follows.
readLock and readLock are not mutually exclusive
readLock and writeLock are mutually exclusive
writeLock and readLock are mutually exclusive
writeLock and writeLock are mutually exclusive
Thread 1, get readLock first, thread 2 tries to get readLock, you can get thread 1, get readLock first, thread 2 tries to get writeLock, blocking and waiting, and you can get thread 1 until thread 1 releases the lock, and you can get thread 1, get writeLock first, thread 1, get writeLock first, thread 2 tries to get writeLock, blocking and waiting, and you can get thread 1 until thread 1 releases the lock.
package com.alio.lock;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;/** * */public class ReadWriteLockDemo {static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");public static void main(String[] args) {Data data = new Data();Worker t1 = new Worker(data, false);//Write Worker t2 = new Worker(data, true);//Read Worker t3 = new Worker(data, true);//Read t1.start();t2.start();t3.start();}static class Worker extends Thread {Data data;Boolean read;public Worker(Data data, Boolean read) {this.data = data;this.read = read;}public void run() {if (read) data.read(); else data.write();}}static class Data {ReadWriteLock lock = new ReentrantReadWriteLock();Lock read = lock.readLock();Lock write = lock.writeLock();public void write() {try {Thread.sleep(2000);//}catch (Exception e) {}write.lock();System.out.println(Thread.currentThread() + " write:begin " + sdf.format(new Date()));try {Thread.sleep(5000);//}catch (Exception e) {} finally {System.out.println(Thread.currentThread() + " write:end " + sdf.format(new Date()));write.unlock();}}public int read() {read.lock();System.out.println(Thread.currentThread()+ " read :begin " + sdf.format(new Date()));try {Thread.sleep(5000);//}catch (Exception e) {} finally {System.out.println(Thread.currentThread() + " read :end " + sdf.format(new Date()));read.unlock();}return 1;}}}Thread[Thread-2,5,main] read :begin 2018-01-22 13:54:16.794
Thread[Thread-1,5,main] read :begin 2018-01-22 13:54:16.794
Thread[Thread-2,5,main] read :end 2018-01-22 13:54:21.795
Thread[Thread-1,5,main] read :end 2018-01-22 13:54:21.795
Thread[Thread-0,5,main] write:begin 2018-01-22 13:54:21.795
Thread[Thread-0,5,main] write:end 2018-01-22 13:54:26.795
Three threads were started at the same time, among which Thread No. 1 Thread Thread[Thread-0,5,main], and it sleeps for 2 seconds when executing write. Then Thread No. 2, Thread No. 3 Thread[Thread-1,5,main],Thread[Thread-2,5,main] will prioritize the execution of code
read.lock();
Since read.lock(); is not mutually exclusive (can be reentrant), they have obtained the lock at the same time, which can be seen through the log
Thread[Thread-2,5,main] read :begin 2018-01-22 13:54:16.794Thread[Thread-1,5,main] read :begin 2018-01-22 13:54:16.794
And their execution time overhead is the same (the test code is sleeping for 5 seconds), so the execution will also end at the same time.
Thread[Thread-2,5,main] read :end 2018-01-22 13:54:21.795Thread[Thread-1,5,main] read :end 2018-01-22 13:54:21.795
Only when all readLocks are released can writeLock get the lock. At this time, the lock will be released after the execution of Thread[Thread-1,5,main] and Thread[Thread-2,5,main] will be released.
So Thread[Thread-0,5,main] got writeLock and executed its own business code at this time
Thread[Thread-0,5,main] write:begin 2018-01-22 13:54:21.795Thread[Thread-0,5,main] write:end 2018-01-22 13:54:26.795
The above is all about the example test of Java language ReadWriteLock feature, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!