0

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.?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212
  • 2
    Your question doesn't quite make sense to me. If `DummyReadLock` is extending `ReentrantReadWriteLock.ReadLock`, then why does it need a `readLock()` method? I think what you want to do is create a `DummyReadWriteLock` class that extends `ReentrantReadWriteLock`, *as well as* a `DummyReadWriteLock.ReadLock` that extends `ReentrantReadWriteLock.ReadLock` (and similarly for the write-lock). Also, a point of terminology: those are "nested classes", not "subclasses". "Subclasses" refers to classes that extend other classes using inheritance. – ruakh Dec 05 '11 at 21:51

2 Answers2

0

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;
    }       
}
Tudor
  • 61,523
  • 12
  • 102
  • 142
0

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.

Trevor Freeman
  • 7,112
  • 2
  • 21
  • 40