学生们正在一起看老师在黑板上写的板书。这时,老师想插掉板书,再写新的内容。而学生们说道:“”老师,我们还没看完了,请先不要摖掉“”。于是,老师会等待大家都看完。
当线程“”读取“”实例的状态时,实例的状态不会发生变化。实例的状态仅在线程执行“”写入“”操作时才会发生变化。从实例的状态变化这个观点来看,“”读取“”和“”写入“”有着本质的区别。
在Read-Write Lock模式中,读取操作和写入操作是分开考虑的。在执行读取操作之前,线程必须获取用于读取的锁。而在执行写入操作之前,线程必须获取用于写入的锁。
由于当前线程执行读取操作时,实例的状态不会发生变化,所以一个线程正在写入时,其他线程不可以读取或写入。
一般来说,执行互斥处理会降低程序性能。但如果把针对写入的互斥处理和针对读取的互斥处理分开来考虑,则可以提高程序性能。
package com.readwritelock;
public class Data {
private final char[] buffer;
private final ReadWriteLock lock = new ReadWriteLock();
public Data(int size) {
this.buffer = new char[size];
for (int i = 0; i < buffer.length; i++) {
buffer[i] = '*';
}
}
public char[] read() throws InterruptedException {
lock.readLock();
try {
return doRead();
} finally {
lock.readUnlock();
}
}
public void write(char c) throws InterruptedException {
lock.writeLock();
try {
doWrite(c);
} finally {
lock.writeUnlock();
}
}
private char[] doRead() {
char[] newbuf = new char[buffer.length];
for (int i = 0; i < buffer.length; i++) {
newbuf[i] = buffer[i];
}
slowly();
return newbuf;
}
private void doWrite(char c) {
for (int i = 0; i < buffer.length; i++) {
buffer[i] = c;
slowly();
}
}
private void slowly() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
package com.readwritelock;
public class ReaderThread extends Thread {
private final Data data;
public ReaderThread(Data data) {
this.data = data;
}
public void run() {
try {
while (true) {
char[] readbuf = data.read();
System.out.println(Thread.currentThread().getName() + " reads " + String.valueOf(readbuf));
}
} catch (InterruptedException e) {
}
}
}
package com.readwritelock;
public final class ReadWriteLock {
private int readingReaders = 0; // (A)…实际正在读取中的线程个数
private int waitingWriters = 0; // (B)…正在等待写入的线程个数
private int writingWriters = 0; // (C)…实际正在写入中的线程个数
private boolean preferWriter = true; // 若写入优先,则为true
public synchronized void readLock() throws InterruptedException {
while (writingWriters > 0 || (preferWriter && waitingWriters > 0)) {
wait();
}
readingReaders++; // (A) 实际正在读取的线程个数加1
}
public synchronized void readUnlock() {
readingReaders--; // (A) 实际正在读取的线程个数减1
preferWriter = true;
notifyAll();
}
public synchronized void writeLock() throws InterruptedException {
waitingWriters++; // (B) 正在等待写入的线程个数加1
try {
while (readingReaders > 0 || writingWriters > 0) {
wait();
}
} finally {
waitingWriters--; // (B) 正在等待写入的线程个数减1
}
writingWriters++; // (C) 实际正在写入的线程个数加1
}
public synchronized void writeUnlock() {
writingWriters--; // (C) 实际正在写入的线程个数减1
preferWriter = false;
notifyAll();
}
}
package com.readwritelock;
import java.util.Random;
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;
}
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;
}
}
package com.readwritelock;
public class Main {
public static void main(String[] args) {
Data data = new Data(10);
new ReaderThread(data).start();
new ReaderThread(data).start();
new ReaderThread(data).start();
new ReaderThread(data).start();
new ReaderThread(data).start();
new ReaderThread(data).start();
new WriterThread(data, "ABCDEFGHIJKLMNOPQTSTUVWXYZ").start();
new WriterThread(data, "abcdefghijklmnopqrstuvwxyz").start();
}
}