-1

I have noticed something very unusual with Java class.

If I instantiate a Condition (from a lock) in the class-body, it doesnt instatiate the object. Here is an example:

private class ConcurrentQ{
        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        Lock readLock = readWriteLock.readLock();
        Lock writeLock = readWriteLock.writeLock();
        Condition emptyList = readLock.newCondition(); //PROBLEM!
...

}

If I did ConcurrentQ q = new ConcurreqntQ(), it would not instantiate, but if removed the //PROBLEM! line it works fine.

Can I know why?

Brijendar Bakchodia
  • 1,567
  • 1
  • 9
  • 15
  • 3
    Did you check the [documentation](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock.html#newCondition--)? – shmosel Sep 14 '18 at 20:53
  • @shmosel, I'm sorry, I didn't and I should have. How can I use conditions then with `ReadWriteLocks`? – Brijendar Bakchodia Sep 14 '18 at 20:54
  • 2
    Re, "... it would not instantiate" It might be helpful if you would include the text of any error messages in your question. – Ohm's Lawman Sep 14 '18 at 20:56
  • 1
    You can create a condition on the write lock. – shmosel Sep 14 '18 at 20:58
  • @besmirched, literally nothing happens! It just ends the process without doing anything – Brijendar Bakchodia Sep 14 '18 at 20:58
  • 2
    `new ConcurrentQ()` can't do _nothing_. It's either going to return a reference to a newly created `ConcurrentQ` instance, or else it's going to throw an exception. If it throws an exception, then there's going to be a message unless,... Did you write code that would catch the exception and ignore it? – Ohm's Lawman Sep 14 '18 at 21:01
  • That code throws an UnsupportedOperationException. If you aren't seeing the stack trace then something is either catching the exception or suppressing the output. – neildo Sep 14 '18 at 21:15
  • Here is the javadoc for ReentrantReadWriteLock#newCondition: `/** * Throws {@code UnsupportedOperationException} because * {@code ReadLocks} do not support conditions. * * @throws UnsupportedOperationException always */` – neildo Sep 14 '18 at 21:21
  • @neildo, why dont readlocks support conditions? – Brijendar Bakchodia Sep 14 '18 at 21:28
  • Conditions are generally used to signal a change to the underlying resource which should only happen under the thread with write lock. Why do you want to use the read lock to create the condition? – neildo Sep 14 '18 at 22:00
  • @neildo, I explained it here: https://stackoverflow.com/questions/52339476/conditions-in-read-write-locks?noredirect=1#comment91626204_52339476, essentially because I want to have a `peek()` method in concurrent queue – Brijendar Bakchodia Sep 14 '18 at 22:01

1 Answers1

0

The Condition class provides the ability for a thread to wait for some condition to occur while executing the critical section.

This can occur when a thread acquires the access to the critical section but doesn’t have the necessary condition to perform its operation. For example, a reader thread can get access to the lock of a shared queue, which still doesn’t have any data to consume.

For a Writer thread: The write lock provides a Condition implementation that behaves in the same way, with respect to the write lock, as the Condition implementation provided by ReentrantLock newCondition does for ReentrantLock. The Condition can, of course, only be used with the write lock. The read lock does not support a Condition and readLock().newCondition() throws Unsupported OperationException.

Hope this hepls....