对某一数据进行线程安全的读写操作,那么就要利用锁来进行线程同步,习惯的做法是,读的时候加锁,禁止其他的读写操作,写的时候,更是要对其他读写操作禁止。那么读写锁就是要更细粒度的进行锁的操作。读的时候,允许其他的线程的读操作,禁止其他线程的写操作等。那么具体的总结如下表:
- | 读 | 写 |
---|---|---|
读 | 非阻塞 | 阻塞 |
写 | 阻塞 | 阻塞 |
比较极端的例子,如果系统中写的次数远远小于读的次数,那么这更能突出读写锁的优势:
public class WriteReadLockDemo {
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 handdleWrite(Lock lock, int index) throws InterruptedException {
try {
lock.lock();
Thread.sleep(1000);
value = index;
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
WriteReadLockDemo writeReadLockDemo = new WriteReadLockDemo();
Runnable readRunnable = () -> {
try {
// 下面两行是普通锁和读写锁
// writeReadLockDemo.handleRead(readLock);
writeReadLockDemo.handleRead(lock);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Runnable writeRunnable = () -> {
try {
// 下面两行是普通锁和读写锁
// writeReadLockDemo.handdleWrite(writeLock, new Random().nextInt());
writeReadLockDemo.handdleWrite(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();
}
}
}
假设读写操作都很耗时,每个读或写操作都需要1秒。
main方法里面,两个for循环,分别启动18个读线程,2个写线程。程序的读方法,写方法分别有一个注释,是表示用普通锁和读写锁,用读写锁2秒执行完成,而利用普通锁则需要20秒。