How i can create a wrapper over ReentrantReadWriteLock ReadLock and WriteLock

244 Views Asked by At

I have a ReentrantReadWriteLock. The ReentrantReadWriteLock contains ReadLock and WriteLock as subclasses.

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

DummyReadLock and DummyWriteLock.

Then I must be able to do something like below

 final Lock lock = new DummyReadLock.readLock();

or

final Lock lock = new DummyWriteLock.writeLock();

Is it possible to achieve this.?

2

There are 2 best solutions below

2
On BEST ANSWER

You you can:

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;
    }       
}
0
On

Yes, that should sort of be possible. I am not sure exactly why you would want to do this, but the below code would extend the ReadLock, for instance.

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

Note that the constructor for the ReadLock requires a ReentrantReadWriteLock as a parameter, so you cannot invoke it exactly as you have shown in your example.