title: Synchronization on the local variables
body: <p>I have a multithreaded Java code in which:</p>
  
  <ul>
  <li>several threads read stateful objects from the synchronized shared storage (i.e. as a result of this, some threads may reference the same objects);</li>
  <li>each thread then invokes a method <code>process()</code> and passes its object there;</li>
  <li><code>process()</code> processes objects in some way, which may result changing the objects state;</li>
  <li>these state changes should be synchronized.</li>
  </ul>
  
  <p>I've created a method like that:</p>
  
  <pre><code>public void process(Foo[] foos) {
      for (final Foo foo : foos) {
          if (foo.needsProcessing()) {
              synchronized (foo) {
                  foo.process();  // foo's state may be changed here
              }
          }
      }
  }
  </code></pre>
  
  <p>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 <em>not</em> valid for me, as I'm <em>not</em> initializing foos in the method).</p>
  
  <p>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).</p>
  
  <p>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?</p>
  
  <p>Thanks in advance!</p>