问题
What is the proper way to wrap semaphore actions in a try-catch block? What happens if the acquire action is interrupted after it has acquired some number, but not all, of the permits requested? How do you know how many to release again? Should the release go in a "finally" block, but then aren't you possibly releasing permits you didn't get if the action was interrupted?
try {
lock.acquire(permits);
//Do some things that require synchronization
//Make sure to release all of the permits again!
lock.release(permits);
} catch (InterruptedException e) {
log.error("Interrupted!");
}
回答1:
The Semaphore.acquire(int)
method is an all or nothing operation, either you get all the permits requested or you block. You can use a double try around your code, or let the (possible) interrupted exception from acquiring bubble up your call stack.
Double try block:
try {
lock.acquire(permits);
try {
// do some stuff here
} finally {
lock.release(permits);
}
} catch(final InterruptedException ie) {
// handle acquire failure here
}
Bubble 'acquisition' exception:
lock.acquire(permits);
try {
// do some stuff here
} finally {
lock.release(permits);
}
On a tangent, do keep in mind that semaphores must be kept balanced by strict programming convention, so you should always release as many permits as you acquired.
回答2:
Take add
method in BoundedHashSet
for example,
public boolean add(T o) throws InterruptedException {
sem.acquire();
boolean wasAdded = false;
try {
wasAdded = set.add(o);
return wasAdded;
} finally {
if (!wasAdded)
sem.release();
}
}
If sem.acquire();
throws InterruptedException, try block and finally block are skipped.
Otherwise, we acquire the semaphore successfully, try block and finally block will be executed altogether. That is, we will release the same number of permits we have acquired.
来源:https://stackoverflow.com/questions/15139231/proper-way-to-try-catch-a-semaphore