0

我有一个ReentrantReadWriteLock. 包含ReadLockReentrantReadWriteLock和 WriteLock 作为子类。

I want to extend this ReadLock and WriteLock by my custom classes as 

DummyReadLock and DummyWriteLock.

然后我必须能够做类似下面的事情

 final Lock lock = new DummyReadLock.readLock();

或者

final Lock lock = new DummyWriteLock.writeLock();

是否有可能实现这一目标。

4

2 回答 2

0

你可以:

class DummyReadLock extends ReentrantReadWriteLock.ReadLock {

    private ReentrantReadWriteLock.ReadLock readLock;

    // inherited constructor
    protected DummyRLock(ReentrantReadWriteLock rwlock) {
        super(rwlock);
        this.readLock = rwlock.readLock(); 
    }

    public ReentrantReadWriteLock.ReadLock readLock() {
        return readLock;
    }       
}
于 2011-12-05T21:48:10.380 回答
0

是的,这应该是可能的。我不确定你为什么要这样做,但是下面的代码会扩展 ReadLock,例如。

public class DummyReadLock extends ReentrantReadWriteLock.ReadLock
{
  public DummyReadLock(ReentrantReadWriteLock arg0)
  {
    super(arg0);
  }
}

请注意,ReadLock 的构造函数需要 ReentrantReadWriteLock 作为参数,因此您不能完全按照示例中所示调用它。

于 2011-12-05T21:49:11.717 回答