8278255: Add more warning text in ReentrantLock and ReentrantReadWriteLock

Reviewed-by: prappo, alanb
This commit is contained in:
Viktor Klang 2024-05-11 18:37:43 +00:00
parent 32c7681cf3
commit 5053b70a7f
3 changed files with 9 additions and 7 deletions

View file

@ -80,11 +80,11 @@ import java.util.concurrent.TimeUnit;
*
* <pre> {@code
* Lock l = ...;
* l.lock();
* l.lock(); // lock() as the last statement before the try block
* try {
* // access the resource protected by this lock
* } finally {
* l.unlock();
* l.unlock(); // unlock() as the first statement in the finally block
* }}</pre>
*
* When locking and unlocking occur in different scopes, care must be

View file

@ -71,8 +71,9 @@ import jdk.internal.vm.annotation.ReservedStackAccess;
* is available even if other threads are waiting.
*
* <p>It is recommended practice to <em>always</em> immediately
* follow a call to {@code lock} with a {@code try} block, most
* typically in a before/after construction such as:
* follow a call to {@code lock} with a {@code try} block, and
* to <em>always</em> immediately call {@code unlock} as the
* first statement in the finally block, as follows:
*
* <pre> {@code
* class X {
@ -80,11 +81,11 @@ import jdk.internal.vm.annotation.ReservedStackAccess;
* // ...
*
* public void m() {
* lock.lock(); // block until condition holds
* lock.lock(); // lock() as the last statement before the try block
* try {
* // ... method body
* } finally {
* lock.unlock();
* lock.unlock(); // unlock() as the first statement in the finally block
* }
* }
* }}</pre>

View file

@ -141,6 +141,7 @@ import jdk.internal.vm.annotation.ReservedStackAccess;
*
* void processCachedData() {
* rwl.readLock().lock();
* // Code between the lock() above, and the unlock() below must not throw
* if (!cacheValid) {
* // Must release read lock before acquiring write lock
* rwl.readLock().unlock();
@ -158,7 +159,7 @@ import jdk.internal.vm.annotation.ReservedStackAccess;
* rwl.writeLock().unlock(); // Unlock write, still hold read
* }
* }
*
* // Make sure that code that could throw is executed inside the try block
* try {
* use(data);
* } finally {