safe semaphore.release in finally block
This commit is contained in:
parent
f3537acf6b
commit
efb631f017
@ -11,10 +11,13 @@ public class SemaPhoreDemo {
|
|||||||
System.out.println("Available permit : " + semaphore.availablePermits());
|
System.out.println("Available permit : " + semaphore.availablePermits());
|
||||||
System.out.println("Number of threads waiting to acquire: " + semaphore.getQueueLength());
|
System.out.println("Number of threads waiting to acquire: " + semaphore.getQueueLength());
|
||||||
|
|
||||||
if (semaphore.tryAcquire()) {
|
if (semaphore.tryAcquire())
|
||||||
|
try {
|
||||||
// perform some critical operations
|
// perform some critical operations
|
||||||
|
} finally {
|
||||||
semaphore.release();
|
semaphore.release();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user