I have a class protected by the Container stream:
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 scroll through the list, grab the container lock at each iteration and, only at the end of the loop, release all the locks. Something like that:
for(Container c : containers) { c.lock(); //do stuff } for(Container c : containers) c.unlock();
I still want other threads to continue to use the getX and setX methods of unlocked containers, but for various reasons, I donβt want to allow this for containers already analyzed.
Do you know Java code for this?
Best ideas are also welcome.
source share