safe semaphore.release in finally block

This commit is contained in:
IgorNB 2019-10-02 19:55:06 +03:00 committed by GitHub
parent f3537acf6b
commit efb631f017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 2 deletions

View File

@ -11,9 +11,12 @@ public class SemaPhoreDemo {
System.out.println("Available permit : " + semaphore.availablePermits());
System.out.println("Number of threads waiting to acquire: " + semaphore.getQueueLength());
if (semaphore.tryAcquire()) {
if (semaphore.tryAcquire())
try {
// perform some critical operations
semaphore.release();
} finally {
semaphore.release();
}
}
}