package com.thread.ch6;
public class ReadWriteLock {
//正在读的用户数
private int readingReaders = 0;
//等待读的用户数量
private int waitingReaders = 0;
//正在写的用户数量
private int writingWriters = 0;
//等待写的用户数量
private int waitingWriters = 0;
private boolean preferWriter = true;
public ReadWriteLock() {
this(true);
}
public ReadWriteLock(boolean preferWriter) {
this.preferWriter = preferWriter;
}
public synchronized void readLock() {
//读的人加1
this.waitingReaders++;
try {
//当写的人数大于0 或者偏向写为true 且等待写的人数大于0 则当前读等待
while (writingWriters > 0 || (preferWriter && waitingWriters > 0)) {
this.wait();
}
this.readingReaders++;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
this.waitingReaders--;
}
}
/**
* 释放读锁
*/
public synchronized void readUnlock() {
this.readingReaders--;
this.notifyAll();
}
public synchronized void writeLock() {
this.waitingWriters++;
try {
//当正在读的数量大于0或写的数量大于0时阻塞
while (readingReaders > 0 || writingWriters > 0) {
this.wait();
}
this.writingWriters++;
} catch (Exception e) {
e.printStackTrace();
} finally {
this.waitingWriters--;
}
}
public synchronized void writeUnlock() {
this.writingWriters--;
this.notifyAll();
}
}
package com.thread.ch6;
public class SharedData {
private final char[] buffer;
private final ReadWriteLock lock = new ReadWriteLock();
public SharedData(int size) {
this.buffer = new char[size];
for (int i = 0; i < size; i++) {
this.buffer[i] = '*';
}
}
public char[] read() throws InterruptedException {
try {
lock.readLock();
return this.doRead();
} finally {
lock.readUnlock();
}
}
public void write(char c) throws InterruptedException {
try {
lock.writeLock();
this.doWrite(c);
} finally {
lock.writeUnlock();
}
}
private void doWrite(char c) {
for (int i = 0; i < buffer.length; i++) {
buffer[i] = c;
slowly(10);
}
}
private char[] doRead() {
char[] newBuf = new char[buffer.length];
for (int i = 0; i < buffer.length; i++)
newBuf[i] = buffer[i];
slowly(50);
return newBuf;
}
private void slowly(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
}
}
}
package com.thread.ch6;
public class ReaderWorker extends Thread {
private final SharedData data;
public ReaderWorker(SharedData data) {
this.data = data;
}
@Override
public void run() {
try {
while (true) {
char[] readBuf = data.read();
System.out.println(Thread.currentThread().getName() + " reads " + String.valueOf(readBuf));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.thread.ch6;
import java.util.Random;
public class WriterWorker extends Thread {
private static final Random random = new Random(System.currentTimeMillis());
private final SharedData data;
private final String filler;
private int index = 0;
public WriterWorker(SharedData 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(1000));
}
} catch (Exception e) {
e.printStackTrace();
}
}
private char nextChar() {
char c = filler.charAt(index);
index++;
if (index >= filler.length())
index = 0;
return c;
}
}
package com.thread.ch6;
public class ReadWritLockClient {
public static void main(String[] args) {
final SharedData sharedData = new SharedData(10);
new ReaderWorker(sharedData).start();
new ReaderWorker(sharedData).start();
new ReaderWorker(sharedData).start();
new ReaderWorker(sharedData).start();
new ReaderWorker(sharedData).start();
new WriterWorker(sharedData, "qwertyuiopasdfg").start();
new WriterWorker(sharedData, "QWERTYUIOPASDFG").start();
}
}
来源:oschina
链接:https://my.oschina.net/lvzi98/blog/3167545