title: Sharing a resource among Threads, different behavior in different java versions
body: <p>This is the first time I've encountered something like below.</p>
  <ul>
  <li><p>Multiple Threads (Inner classes implementing Runnable) sharing a Data Structure (instance variable of the upper class).</p>
  </li>
  <li><p>Working: took classes from Eclipse project's bin folder, ran on a Unix machine.</p>
  </li>
  <li><p>NOT WORKING: directly compiled the src on Unix machine and used those class files. Code compiles and then runs with no errors/warnings, but one thread is not able to access shared resource properly.</p>
  </li>
  <li><p>PROBLEM: One thread adds elements to the above common DS. Second thread does the following...</p>
  <pre><code>  while(true){
     if(myArrayList.size() &gt; 0){
     //do stuff
     }
  </code></pre>
  <p>}</p>
  </li>
  <li><p>The Log shows that the size is updated in Thread 1.</p>
  </li>
  <li><p>For some mystic reason, the workflow is not enetering if() ...</p>
  </li>
  </ul>
  <p>Same exact code runs perfectly if I directly paste the class files from Eclipse's bin folder.</p>
  <p>I apologize if I missed anything obvious.</p>
  <p>Code:</p>
  <pre><code>ArrayList&lt;CSRequest&gt; newCSRequests = new ArrayList&lt;CSRequest&gt;();
  </code></pre>
  <p>//Thread 1</p>
  <pre><code>private class ListeningSocketThread implements Runnable {
      ServerSocket listeningSocket;
  
      public void run() {
          try {
              LogUtil.log(&quot;Initiating...&quot;);
              init(); // creates socket
              processIncomongMessages();
              listeningSocket.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      private void processIncomongMessages() throws IOException {     
          while (true) {
              try {
                  processMessage(listeningSocket.accept());
              } catch (ClassNotFoundException e) {                    
                  e.printStackTrace();
              }
          }
      }
  
      private void processMessage(Socket s) throws IOException, ClassNotFoundException {
          // read message
          ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
          Object message = ois.readObject();
          LogUtil.log(&quot;adding...: before size: &quot; + newCSRequests.size());
          synchronized (newCSRequests) {
                  newCSRequests.add((CSRequest) message);
          }
          LogUtil.log(&quot;adding...: after size: &quot; + newCSRequests.size()); // YES, THE SIZE IS UPDATED TO &gt; 0
          //closing....
          
      }
      
  ........
  </code></pre>
  <p>}</p>
  <pre><code>//Thread 2
  private class CSRequestResponder implements Runnable {
  
          public void run() {
              LogUtil.log(&quot;Initiating...&quot;); // REACHES..
              while (true) {
  //              LogUtil.log(&quot;inside while...&quot;); // IF NOT COMMENTED, FLOODS THE CONSOLE WITH THIS MSG...
                  if (newCSRequests.size() &gt; 0) { // DOES NOT PASS
                      LogUtil.log(&quot;inside if size &gt; 0...&quot;); // NEVER REACHES....
                      try {
                          handleNewCSRequests();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
  ....
  }
  </code></pre>
  <p><strong>UPDATE</strong></p>
  <p>Solution was to add synchronized(myArrayList) before I check the size in the Thread 2.</p>