-
Enhancement
-
Resolution: Fixed
-
P4
-
5.0
-
b51
-
x86
-
windows_xp
A DESCRIPTION OF THE REQUEST :
The class java.util.ReentrantReadWriteLock provides no way to tell if a given thread currently holds a read lock. I am looking for the equivalent of the Thread.holdsLock() method that would tell me this, so I can use it in 'assert' statements to verify that a particular method is only called in circumstances where the object is locked for reading. For instance:
private final ReentrantReadWriteLock _lock = new ReentrantReadWriteLock();
private final ReadLock _read = _lock.readLock();
private final WriteLock _write = _lock.writeLock();
void readSomething() {
_read.lock();
try {
doSomething();
}
finally {
_read.unlock();
}
}
void doSomething() {
assert _readLock.isHeldByCurrentThread(); // There is no way to do this!
//... do something read-only here ...
}
JUSTIFICATION :
The reasons to add this feature are essentially the same as those which motivated the Thread.holdsLock() feature. This feature is extremely useful in enforcing threading constraints via 'assert' and hence reducing debugging time by detecting when a method is accidentally called without establishing a lock on any necessary objects.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was surprised that the lock objects returned by the ReentrantReadWriteLock.readLock() and reentrantReadWriteLock.writeLock() methods do not support the ReentrantLock interface, which would have provided the isHeldByCurrentThread() method. Also, for some mysterious reason, the ReentrantReadWriteLock class provides a "isWriteLockedByCurrentThread()" method, but not a "isReadLockedByCurrentThread()".
Ideally, I would like to see ReentrantReadWriteLock return a ReentrantLock instance from its readLock() and writeLock() methods, instead of a Lock object as it does now. I would also like to see an "isReadLockedByCurrentThread()" method added to ReentrantReadWriteLock (which would parallel the existing isWriteLockedByCurrentThread() method.)
ACTUAL -
There appears to be no way to determine if the current thread holds a read lock on a ReentrantReadWriteLock object.
---------- BEGIN SOURCE ----------
Source code examples given above.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
I have not discovered any workarounds.
###@###.### 2004-12-13 23:37:57 GMT
The class java.util.ReentrantReadWriteLock provides no way to tell if a given thread currently holds a read lock. I am looking for the equivalent of the Thread.holdsLock() method that would tell me this, so I can use it in 'assert' statements to verify that a particular method is only called in circumstances where the object is locked for reading. For instance:
private final ReentrantReadWriteLock _lock = new ReentrantReadWriteLock();
private final ReadLock _read = _lock.readLock();
private final WriteLock _write = _lock.writeLock();
void readSomething() {
_read.lock();
try {
doSomething();
}
finally {
_read.unlock();
}
}
void doSomething() {
assert _readLock.isHeldByCurrentThread(); // There is no way to do this!
//... do something read-only here ...
}
JUSTIFICATION :
The reasons to add this feature are essentially the same as those which motivated the Thread.holdsLock() feature. This feature is extremely useful in enforcing threading constraints via 'assert' and hence reducing debugging time by detecting when a method is accidentally called without establishing a lock on any necessary objects.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was surprised that the lock objects returned by the ReentrantReadWriteLock.readLock() and reentrantReadWriteLock.writeLock() methods do not support the ReentrantLock interface, which would have provided the isHeldByCurrentThread() method. Also, for some mysterious reason, the ReentrantReadWriteLock class provides a "isWriteLockedByCurrentThread()" method, but not a "isReadLockedByCurrentThread()".
Ideally, I would like to see ReentrantReadWriteLock return a ReentrantLock instance from its readLock() and writeLock() methods, instead of a Lock object as it does now. I would also like to see an "isReadLockedByCurrentThread()" method added to ReentrantReadWriteLock (which would parallel the existing isWriteLockedByCurrentThread() method.)
ACTUAL -
There appears to be no way to determine if the current thread holds a read lock on a ReentrantReadWriteLock object.
---------- BEGIN SOURCE ----------
Source code examples given above.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
I have not discovered any workarounds.
###@###.### 2004-12-13 23:37:57 GMT
- relates to
-
JDK-6294770 java.util.concurrent.locks.ReentrantReadWriteLock acquisition order
-
- Resolved
-
-
JDK-6281487 ReentrantReadWriteLock: readers repeatedly acquire lock while writer is waiting
-
- Resolved
-
-
JDK-6305337 Reentrant writeLock in Fair mode ReentrantReadWriteLock may block
-
- Closed
-
-
JDK-6364793 Add missing @since 1.6 in Core Library javadoc
-
- Resolved
-