safe semaphore.release in finally block
@@ -11,10 +11,13 @@ 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
+ } finally {
semaphore.release();
}
+ }
}
