在之前的模式中,为了确保线程安全,每次读写数据只允许一个线程操作数据类对象,但是多线程同时读数据也是安全的,所以为了提升性能,是不是可以有一种模式,允许同时多线程读,但是只能一条写线程在操作数据类对象呢?答案是肯定的。现在就来介绍下读写锁模式。
互斥,读读不互斥。
以下是自己实现的一个读写锁的类:
/**
* 读写锁
*/
public class MyReadWriteLock {
private int readingReaders = 0;//正在读的线程
private int waitingWriters = 0;//正在等待的写线程
private int writingWriters = 0;//正在写的线程
private boolean preferWriter = true;//写优先,当读解锁时,优先写
/**
* 读锁
* @throws InterruptedException
*/
public synchronized void readLock() throws InterruptedException {
//正在进行写或者写优先且等待写的线程数大于零
while (writingWriters>0||(preferWriter&&waitingWriters>0)){
wait();
}
readingReaders++;
}
/**
* 读解锁
*/
public synchronized void readUnlock(){
readingReaders--;
preferWriter = true;
//唤醒正准备写的线程
notifyAll();
}
/**
* 写锁
* @throws InterruptedException
*/
public synchronized void writeLock() throws InterruptedException {
waitingWriters++;
try {
while (readingReaders>0||writingWriters>0){
wait();
}
} finally {
//确保wait之后等待的写线程减1
waitingWriters--;
}
writingWriters++;
}
/**
* 写解锁
*/
public synchronized void writeUnlock(){
writingWriters--;
preferWriter = false;
//唤醒等待读或者写的线程
notifyAll();
}
}
数据类:
public class Data {
private final char[] buffer;
private final MyReadWriteLock lock = new MyReadWriteLock();
public Data(int size) {
this.buffer = new char[size];
for (int i = 0; i < size; i++) {
buffer[i] = '*';
}
}
/**
* 读数据
* @return
* @throws InterruptedException
*/
public char[] read() throws InterruptedException {
lock.readLock();
try {
return doRead();
}finally {
lock.readUnlock();
}
}
/**
* 写数据
* @param c
* @throws InterruptedException
*/
public void write(char c) throws InterruptedException {
lock.writeLock();
try {
doWrite(c);
}finally {
lock.writeUnlock();
}
}
/**
* 真正的写数据
* @param c
*/
private void doWrite(char c) {
for (int i = 0; i < buffer.length; i++) {
buffer[i] = c;
slowly();
}
}
/**
* 真正的读数据
* @return
*/
private char[] doRead() {
char[] newBuffer = new char[buffer.length];
for (int i = 0; i < buffer.length; i++) {
newBuffer[i] = buffer[i];
}
slowly();
return newBuffer;
}
/**
* 延时
*/
private void slowly() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
很简单,持有一个字符数组,读写操作先通过我们的ReadWriteLock的lock方法加锁,读是将字符数组的所有字符依次打印出来,写则是将字符数组全部改为某个字符,读写操作完后通过
通过我们的ReadWriteLock的unlock方法解锁。
读线程:
public class Data {
private final char[] buffer;
private final MyReadWriteLock lock = new MyReadWriteLock();
public Data(int size) {
this.buffer = new char[size];
for (int i = 0; i < size; i++) {
buffer[i] = '*';
}
}
/**
* 读数据
* @return
* @throws InterruptedException
*/
public char[] read() throws InterruptedException {
lock.readLock();
try {
return doRead();
}finally {
lock.readUnlock();
}
}
/**
* 写数据
* @param c
* @throws InterruptedException
*/
public void write(char c) throws InterruptedException {
lock.writeLock();
try {
doWrite(c);
}finally {
lock.writeUnlock();
}
}
/**
* 真正的写数据
* @param c
*/
private void doWrite(char c) {
for (int i = 0; i < buffer.length; i++) {
buffer[i] = c;
slowly();
}
}
/**
* 真正的读数据
* @return
*/
private char[] doRead() {
char[] newBuffer = new char[buffer.length];
for (int i = 0; i < buffer.length; i++) {
newBuffer[i] = buffer[i];
}
slowly();
return newBuffer;
}
/**
* 延时
*/
private void slowly() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
写线程:
public class WriterThread extends Thread{
private static final Random random = new Random();
private final Data data;
private final String filler;
private int index = 0;
public WriterThread(Data data, String filler) {
this.data = data;
this.filler = filler;
}
@Override
public void run() {
try{
while (true){
char c = nextChar();
data.write(c);
Thread.sleep(random.nextInt(3000));
}
}catch (InterruptedException e){
}
}
private char nextChar() {
char c = filler.charAt(index);
index++;
if (index>=filler.length()){
index = 0;
}
return c;
}
}
这样子,在保证安全性的同时,又增加了性能(因为现在可以同时多条读线程操作数据了)。注意到ReadWriteLock类中有个preferWriter字段,它的作用是使得读锁解除后优先写,即如果此时有写线程在wait时就执行写。为什么要这样呢,因为一般用读写锁的目的就是在读线程明显多于写线程的情况下使用来提高性能的,所以如果没有写优先的话,可能执行了很多读线程才执行一个写线程(notifyAll后所有线程竞争),这样子长时间读到的数据都是一样的。(当然在写解锁后要将其置为false,不然没法读了)