22.ReadWriteLock读写锁

时间:2022-02-20 08:14:21
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; /**
* ReadWriteLock读写锁:允许多个线程同时读
* 读-读不互斥:读写之间不阻塞
* 读-写互斥:读阻塞写,写阻塞读
* 写-写互斥:写写阻塞
*/
public class ReadWriteLockDemo {
private static Lock lock = new ReentrantLock();
private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private static Lock readLock = readWriteLock.readLock();
private static Lock writeLock = readWriteLock.writeLock();
private int value;
//读
public Object handleRead(Lock lock) throws InterruptedException{
try {
lock.lock();
Thread.sleep(1000);//读操作的耗时越多读写的优势越明显
return value;
} finally {
lock.unlock();
}
}
//写
public void handleWrite(Lock lock,int index) throws InterruptedException{
try {
lock.lock();
Thread.sleep(1000);
value = index;
} finally {
lock.unlock();
}
}
public static void main(String[] args){
final ReadWriteLockDemo demo = new ReadWriteLockDemo();
Runnable readRunnable = () -> {
try {
demo.handleRead(readLock);//读写锁
// demo.handleRead(lock);//重入锁
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Runnable writeRunnable = () -> {
try {
demo.handleWrite(writeLock,new Random().nextInt());//读写锁
// demo.handleWrite(lock,new Random().nextInt());//重入锁
} catch (InterruptedException e) {
e.printStackTrace();
}
};
//读写分离
for (int i = 0; i < 18; i++) {
new Thread(readRunnable).start();
}
for (int i = 18; i < 20; i++) {
new Thread(writeRunnable).start();
}
}
//使用读写锁,由于使用了读写分离,读线程完全并行,而写会阻塞读(注意for循环次数,写线程之间实际是串行的),因此运行大约2秒多结束
//而使用重入锁,所有读写线程之间都必须相互等待,因此执行时间大约20多秒
}