Skip to content

Commit efb631f

Browse files
authored
safe semaphore.release in finally block
1 parent f3537ac commit efb631f

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

  • core-java-modules/core-java-concurrency-basic/src/main/java/com/baeldung/concurrent/semaphore

‎core-java-modules/core-java-concurrency-basic/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ public void execute() throws InterruptedException {
1111
System.out.println("Available permit : " + semaphore.availablePermits());
1212
System.out.println("Number of threads waiting to acquire: " + semaphore.getQueueLength());
1313

14-
if (semaphore.tryAcquire()) {
14+
if (semaphore.tryAcquire())
15+
try {
1516
// perform some critical operations
16-
semaphore.release();
17+
} finally {
18+
semaphore.release();
19+
}
1720
}
1821

1922
}

0 commit comments

Comments
 (0)