tags:

views:

179

answers:

3

If I have acquired a number of locks, how do I release the Set of locks atomically?

+2  A: 

Take a look at java.util.concurrent package description in JDK, maybe there you'll find suitable sync primitibe. Information in your question isn't sufficient to give more concrete answer.

One simple approach (but maybe not the most effective) is to use 'uberLock' to guard all needed locks, e.g.:

private final Lock uberLock = new ReentrantLock();
void lockAll() {
    uberLock.lock();
    try {
        // call lock() on all needed locks
    } finally {
        uberLock.unlock();
    }
}
Victor Sorokin
+5  A: 

I'm puzzled about why you'd need to release them atomically. Getting them atomically, I could understand, although the code to do that would be a strange beast - one clean way would be to hide them from all code behind a master lock manager, and synchronizing on that. But what code would care that two locks were being released all at once or none at all?

Paul Brinkley
+2  A: 

As locks are treated as independend objects (see this question) and the Java language does not (yet, as of my knowledge) provide a method in its JVM (where it would have to be implemented to be really atomically) to release multiple locks in one step, it is simply not possible.

Maybe it is possible to redesign your application so that an atomic release of several locks is not needed anymore. (Maybe you could also give more details on your specific problem that we can get a better understanding)

Kosi2801