我有一个同步练习,我需要同步一个读取方法,以便任何数量的线程都可以执行它,只要不执行任何写入方法即可。这必须从头开始,所以我不能使用java.util.concurrent.locks等。
为此,我需要某种机制来保护,但不能阻止read方法,因此,读线程被写操作阻止,但未被其他读操作阻止。我不能为此使用普通锁,因为在read方法中调用lock方法将导致其他读取线程等待。
规则应这样:
当线程在write()内部时,其他任何线程都不得输入read()或write()
当线程位于read()内时,没有其他线程必须输入write(),但是它们可以输入read()
我试图建立几个自制的锁来解决这个问题。
WriteLock是一个相当标准的可重入锁,但是如果正在执行读取,则它会阻塞(使用readcounter)
如果遍历了write(),则ReadLock仅应使线程等待。否则,它应仅允许线程处理其事务并增加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解锁时它不会释放读取器。
这仅仅是从概念上讲是不正确的,还是显示器我不了解的东西?或者是其他东西?
最佳答案
您的ReadLock和WriteLock正在不同的对象上同步,并在不同的对象上调用wait和notify。
这允许ReadLock在WriteLock验证计数时修改WriteLock中的计数。这也将导致不同的锁不会从调用中唤醒。
如果您修改ReadLock以将WriteLock用作监视器,则会得到更好的结果(我没有检查这是否是唯一的问题)
protected class ReadLock{
WriteLock lock;
public ReadLock(WriteLock lock) {
super();
this.lock = lock;
}
public void lock() throws InterruptedException { //If write() then wait
synchronized (lock) {
System.out.println("Waiting to read: "+Thread.currentThread());
Thread callingThread = Thread.currentThread();
while (lock.isLocked && lock.lockedBy != callingThread) {
lock.wait();
}
lock.readers++; //Increment writelocks readers
System.out.println("Reading: "+Thread.currentThread());
}
}
public void unlock() {
synchronized (lock) {
lock.readers--; //Subtract from writelocks readers
lock.notify();
}
}
}