我有一个同步练习,我需要同步一个读取方法,以便只要没有执行写入方法,任意数量的线程都可以执行它。这必须从头开始,所以我不能使用 java.util.concurrent.locks 等。
为此,我需要某种机制来保护,但不阻止读取方法,因此读取线程被写入阻塞,但不会被其他读取阻塞。我不能为此使用普通锁,因为在读取方法中调用锁方法会导致其他读取线程等待。
规则应该是这样的: 当一个线程在 write() 中时,其他线程不得进入 read() 或 write() 当线程在 read() 中时,其他线程不得进入 write(),但它们可以进入读()
我已经尝试建造几个自制的锁来解决这个问题。WriteLock 是一个相当标准的可重入锁,除了如果正在执行读取它会阻塞(使用 readcounter)ReadLock 应该只导致线程等待,如果正在遍历 write()。否则,它应该简单地允许线程继续其业务并增加 WriteLocks 计数器。
代码:
package sync;
public class SyncTest {
Long testlong = new Long(0L);
int reads = 0;
int writes = 0;
WriteLock w = new WriteLock();
ReadLock r = new ReadLock(w);
public SyncTest() {
// TODO Auto-generated constructor stub
}
public static void main(String args[]){
final SyncTest s = new SyncTest();
for(int i = 0 ; i<3 ; i++){ //Start a number of threads to attack SyncTest
final int ifinal = i;
new Thread(){
int inc = ifinal;
@Override
public void run() {
System.out.println("Starting "+inc);
long starttime = System.currentTimeMillis();
try {
while(System.currentTimeMillis()-starttime < 10){
if (inc < 2){
s.readLong();
}else{
s.writeLong(inc+1);
}
}
System.out.println(inc + " done");
if(inc == 0){
Thread.sleep(1000);
System.out.println(s.reads+" "+s.writes);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Thread "+inc+" "+super.toString();
}
}.start();
}
}
public Long readLong() throws InterruptedException{
Long l;
r.lock(); //Lock for reading
//System.out.println("Read "+reads);
l = testlong;
reads++;
r.unlock(); //Unlock after reading
return l;
}
public void writeLong(int i) throws InterruptedException{
w.lock(); //Lock for writing
//System.out.println("Write "+writes);
int curreads = reads;
int curwrites = writes;
testlong = testlong + i;
writes++;
Thread.sleep(100); //Simulate long write
if(curreads != reads){
System.out.println("Reads did not lock");
}
if(curwrites+1 != writes){
System.out.println("Writes did not lock");
}
w.unlock(); //Unlock writing
}
protected class WriteLock{
boolean isLocked = false;
Thread lockedBy = null;
int lockedCount = 0;
int readers = 0; //The number of readers currently through the reading lock.
public synchronized void lock() throws InterruptedException {
System.out.println("Locking: "+Thread.currentThread());
Thread callingThread = Thread.currentThread();
while ((isLocked && lockedBy != callingThread) || readers > 0) { //Wait if locked or readers are in read()
wait();
}
isLocked = true;
lockedCount++;
lockedBy = callingThread;
System.out.println("Is locked: "+Thread.currentThread());
}
public synchronized void unlock() {
//System.out.println("Unlocking: "+Thread.currentThread());
if (Thread.currentThread() == this.lockedBy) {
lockedCount--;
if (lockedCount == 0) {
System.out.println("Is unlocked: "+Thread.currentThread());
isLocked = false;
notify();
}
}
}
}
protected class ReadLock{
WriteLock lock;
public ReadLock(WriteLock lock) {
super();
this.lock = lock;
}
public synchronized void lock() throws InterruptedException { //If write() then wait
System.out.println("Waiting to read: "+Thread.currentThread());
Thread callingThread = Thread.currentThread();
while (lock.isLocked && lock.lockedBy != callingThread) {
wait();
}
lock.readers++; //Increment writelocks readers
System.out.println("Reading: "+Thread.currentThread());
}
public synchronized void unlock() {
lock.readers--; //Subtract from writelocks readers
notify();
}
}
}
但是,这不起作用,阅读锁的工作方式到目前为止它会在线程写入时锁定读者,但据我所知,当 WriteLock 解锁时它不会释放它们。
这只是在概念上不合理,还是我对显示器有什么不明白的地方?或者是其他东西?