public class ReadWriteLock {
// 读锁持有个数
private int readCount = 0;
private int writeCount = 0;
public synchronized void lockRead() throws InterruptedException {
while(writeCount > 0) {
wait();
}
readCount++;
}
public synchronized void unlockRead() {
readCount--;
notifyAll();
}
public synchronized void lockWrite() throws InterruptedException {
while(writeCount > 0) {
wait();
}
writeCount ++;
while(readCount >0 ){
wait();
}
}
public synchronized void unlockWrite() {
writeCount--;
notifyAll();
}