读写锁ReentrantReadWriteLock的使用

package com.thread.test.Lock;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; public class ReadWriteLockDemo { private int value; public Object handleRead(Lock lock) throws InterruptedException {
try {
lock.lock();// 模拟读操作
Thread.sleep(1000);// 假设单个读操作耗时1s,读操作的耗时越多,读写锁的优势越明显
return value;
} finally {
lock.unlock();
}
} public void handleWrite(Lock lock, int index) throws InterruptedException {
try {
lock.lock();// 模拟写操作
Thread.sleep(1000);// 假设单个写操作耗时1s
value = index;
} finally {
lock.unlock();
}
} private static Lock lock = new ReentrantLock();// 普通的重入锁
private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();// 读写锁
private static Lock readLock = readWriteLock.readLock();// 读锁
private static Lock writeLock = readWriteLock.writeLock();// 写锁
// 读写锁中:允许多个线程读,所以18个读线程是完全并行的;
// 读写锁中:读-写互斥,及读阻塞写,写阻塞读;写写互斥,写写阻塞。所以两个写线程实际是串行的
// 所以用读写锁,整个操作耗时大概2s就结束了
// 如果用普通的重入锁,那么这20个线程都是串行执行的,耗时大概20s public static void main(String[] args) {
final ReadWriteLockDemo demo = new ReadWriteLockDemo();
Runnable readRunnable = new Runnable() {
public void run() {
try {
demo.handleRead(readLock);// 读锁
// demo.handleRead(lock);//普通的重入锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}; Runnable writeRunnable = new Runnable() {
public void run() {
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();// 开启18个读线程
}
for (int j = 18; j < 20; j++) {
new Thread(writeRunnable).start();// 开启2个写线程
}
}
}
上一篇:Java多线程之ReadWriteLock读写锁简介与使用教程


下一篇:JUC——线程同步锁(ReentrantReadWriteLock读写锁)