title: Java Synchronization Not Working as Expected
body: <p>I have a &quot;simple&quot; 4 class example that reliably shows unexpected behavior from java synchronization on multiple machines.  As you can read below, given the contract of the java <code>sychronized</code> keyword, <code>Broke Synchronization</code> should never be printed from the class TestBuffer.</p>
  <p>Here are the 4 classes that will reproduce the issue (at least for me).  I'm not interested in how to fix this broken example, but rather <strong>why it breaks</strong> in the first place.</p>
  <p><a href="http://pastebin.com/Jw598wRr" rel="noreferrer">Sync Issue - Controller.java</a></p>
  <p><a href="http://pastebin.com/138xvmG9" rel="noreferrer">Sync Issue - SyncTest.java</a></p>
  <p><a href="http://pastebin.com/0KtLkT6J" rel="noreferrer">Sync Issue - TestBuffer.java</a></p>
  <p><a href="http://pastebin.com/PViwFQsj" rel="noreferrer">Sync Issue - Tuple3f.java</a></p>
  <p>And here is the output I get when I run it:</p>
  <pre><code>java -cp . SyncTest
  Before Adding
  Creating a TestBuffer
  Before Remove
  Broke Synchronization
  1365192
  Broke Synchronization
  1365193
  Broke Synchronization
  1365194
  Broke Synchronization
  1365195
  Broke Synchronization
  1365196
  Done
  </code></pre>
  <p>UPDATE:
  @Gray has the simplest example that breaks thus far.  His example can be found here: <a href="http://pastebin.com/AF85FRT7" rel="noreferrer">Strange JRC Race Condition</a></p>
  <p>Based on the feedback I've gotten from others, it looks like the issue may occur on Java 64-bit 1.6.0_20-1.6.0_31 (unsure about newer 1.6.0's) on Windows and OSX.  Nobody has been able to reproduce the issue on Java 7.  It may also require a multi-core machine to reproduce the issue.</p>
  <p>ORIGINAL QUESTION:</p>
  <p>I have a class which provides the following methods:</p>
  <ul>
  <li>remove - Removes the given item from the list</li>
  <li>getBuffer - Iterates over all the items in the list</li>
  </ul>
  <p>I've reduced the problem down to the 2 functions below, both of which are in the same object and they're both <code>synchronized</code>.  Unless I am mistaken, &quot;Broke Synchronization&quot; should never be printed because <code>insideGetBuffer</code> should always be set back to false before <code>remove</code> can be entered.  However, in my application it is printing &quot;Broke Synchronization&quot; when I have 1 thread calling remove repeatedly while the other calls getBuffer repeatedly.  The symptom is that I get a <code>ConcurrentModificationException</code>.</p>
  <h3>See Also:</h3>
  <p><a href="https://codereview.stackexchange.com/questions/12546/very-strange-race-condition-which-looks-like-a-jre-issue">Very strange race condition which looks like a JRE issue</a></p>
  <h3>Sun Bug Report:</h3>
  <p>This was confirmed as a bug in Java by Sun.  It is apparently fixed (unknowingly?) in jdk7u4, but they have not backported the fix to jdk6.
  <a href="http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7176993" rel="noreferrer">Bug ID: 7176993</a></p>