Synchronization on the local variables
java, multithreading, synchronization
Solution
if (foo.needsProcessing()) {
synchronized (foo) {
foo.process(); // foo's state may be changed here
}
}
I think there is a race condition in the above fragment that could result in `foo.process()` occasionally being called twice on the same object. It should be:
synchronized (foo) {
if (foo.needsProcessing()) {
foo.process(); // foo's state may be changed here
}
}
Is this really that bad to synchronize on locals?
It is not bad to synchronize on locals per se. The real issues are:
whether the different threads are synchronizing on the correct objects to achieve proper synchronization, and
whether something else could cause problems by synchronizing on those objects.
Problem
I have a multithreaded Java code in which: - several threads read stateful objects from the synchronized shared storage (i.e. as a result of this, some threads may reference the same objects); - each thread then invokes a method `process()` and passes its object there; - `process()` processes objects in some way, which may result changing the objects state; - these state changes should be synchronized. I've created a method like that: ``` public void process(Foo[] foos) { for (final Foo foo : foos) { if (foo.needsProcessing()) { synchronized (foo) { foo.process(); // foo's state may be changed here } } } } ``` To the best of my knowledge, this looks legit. However, IntelliJ's inspection complains about synchronizing on the local variable because "different threads are very likely to have different local instances" (this is not valid for me, as I'm not initializing foos in the method). Essentially what I want to achieve here is the same as having method Foo.process() synchronized (which is not an option for me, as Foo is a part of 3rd party library). I got used to the code without yellow marks, so any advice from the community is appreciated. Is this really that bad to do synchronization on locals? Is there an alternative which will work in my situation? Thanks in advance!