我如何将ReentrantReadWriteLock.readLock或ReentrantReadWriteLock.writeLock转换为我的类对象

时间:2011-11-30 12:20:51

标签: java multithreading locking reentrantreadwritelock

我想要做的是一次性获取当前线程的读取数量。我为此写了一个包装器,但我的问题是ReadLock()方法返回ReentrantReadWriteLock.WriteLock所以我没有之后超出了getThreadReadLockCount。我想要的是readLock()方法应该返回我,以便我可以超出count方法。 Anyideas。?

   private final ThreadLocal    readLocksHeldByThread;

private static class ReadCounts {
    public int  value   = 0;
}

public CustomReentrantReadWriteLock() {
    super(true);
    readLocksHeldByThread = new ThreadLocal() {
        @Override
        public Object initialValue() {
            return new ReadCounts();
        }
    };
}

@Override
public synchronized ReadLock readLock() {
    final ReadCounts myReadLocks = (ReadCounts) readLocksHeldByThread.get();
    ++myReadLocks.value;
    return super.readLock();
}

    public synchronized int getThreadReadLockCount() {
    final ReadCounts currentReadLocks = (ReadCounts) readLocksHeldByThread.get();
    return currentReadLocks.value;
}

1 个答案:

答案 0 :(得分:1)

你的代码只计算一个线程请求readlock的次数,如果它在lock和release之间被缓存,这将得到不同的结果:

Lock rl = var.readLock();
rl.lock();
try{
    //...
}finally{
    rl.unlock();
}

VS

var.readLock().lock();
try{
    //...
}finally{
    var.readLock().unlock();
}

会给出不同的结果

你可以像这样包装返回的锁

@Override public Lock readLock() {
     final ReadCounts myReadLocks = (ReadCounts) readLocksHeldByThread.get();
     return new Lock(){
        Lock l = super.readLock(); 
        public void lock(){
            l.lock();
            ++myReadLocks.value;
        }

        public void lockInterruptibly() throws InterruptedException{
            l.lockInterruptibly();
            ++myReadLocks.value;
        }

        public boolean tryLock(){
            if(l.tryLock()){
                ++myReadLocks.value;
                return true;
            }else return false;
        }

        public boolean tryLock(long time, TimeUnit unit) throws InterruptedException{
            if(l.tryLock(time,unit)){
                ++myReadLocks.value;
                return true;
            }else return false;
        }

        public void unlock(){
            --myReadLocks.value;
            l.unlock();
        }

        public Condition newCondition(){
            return l.newCondition();
        }

     }
}