5

I have a thread safe class Container:

public class Container {
  private int x;
  ...
  public synchronized int getX();
  public synchronized void setX(int x);
}

Then I have a list of containers:

List<Container> containers;

I would like to iterate through the list, aquire the container's lock at each iteration and, only at the end of loop, release all locks. Something like this:

for(Container c : containers) {
  c.lock();
  //do stuff
}
for(Container c : containers)
  c.unlock();

I still want other threads to be able to continue to use getX and setX methods of unlocked containers, but for various reasons I do not want to allow that for already analysed containers.

Do you know the java code for that?

Better ideas are also appreciated.

6
  • How about having lock() and unlock() first and last in getX and setX?
    – aioobe
    Jun 12, 2015 at 8:27
  • @see ReentrantLock
    – lance-java
    Jun 12, 2015 at 8:28
  • @aioobe: I would like to combine synchronized methods with explicit locks (hoping that it is possibile, but I think it is because behind the scenes the synchronized keyword acquires the object lock).
    – Kami
    Jun 12, 2015 at 8:30
  • @Lance Java: But in my case how would you use try-finally inside the for loop? Maybe try around all the for loop and finally releasing all the locks? Is ok to unlock a unlocked lock?
    – Kami
    Jun 12, 2015 at 8:33
  • Regarding different approaches: if you explain what you do with Container maybe a solution with immutable Containers would be better...
    – zbig
    Jun 12, 2015 at 8:34

1 Answer 1

1

This is not possible I'm afraid. The Java Language imposes a strict nesting principle with the synchronized (...) { ... } blocks (and synchronized methods).

That being said, there are nasty workarounds. In bytecode a synchronized block translates to two separate monitorenter / monitorexit instructions. The same thing can be achieved with sun.mis.Unsafe.monitorEnter() and monitorExit().

In this case however, I would strongly encourage you to rethink the design. I would suggest you let getX and setX acquire / release an internal Lock (which the traversal method also uses). In particular, you could use ReadWriteLock for your setX and getX methods. In fact, this approach is better than having synchronized methods for other reasons too. See for instance:

Why is it a good practice to have separate locks instead of having lock on object that get modified in the synchronized block in java?

9
  • Does not the synchronized keyword acquire the lock of the object itself? It is like writing synchronized(this){} inside the method. If we are locking on this, why can not we create a lock that lock the object (this)?
    – Kami
    Jun 12, 2015 at 8:40
  • With an internal lock, would not I have the same problem? How can I acquire multiple internal locks (one at each iteration) without using the synchronized keyword?
    – Kami
    Jun 12, 2015 at 8:43
  • Your understanding is correct. The only reason we can't just do something like lock(this); and then later unlock(this); (which would correspond to the opening brace { resp. closing brace }of synchronized (this)) is that the only way to acquire this type of lock is through the synchronized keyword (unless you resort to unsafe operations as I've mentioned in my answer) and the synchronized keyword has the syntactical constraint of a { ... } block.
    – aioobe
    Jun 12, 2015 at 8:44
  • No, with an internal lock, you could use Lock which has separate lock() and unlock() methods! (Answer updated.)
    – aioobe
    Jun 12, 2015 at 8:45
  • Thank you, but how can I write a try-finally block inside the for loop? I mean finally should I unlock all locks?
    – Kami
    Jun 12, 2015 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.