title: Java: synchronized buffer
body: <p>I have one thread, that is receiving tcp packets and add each line to a buffer that is an <code>ArrayList&lt;String&gt;</code>. 
  The other thread should periodically check if new data is available, but only if the buffer is currently not locked. But how do I check if it has been locked? In C++ I could explicitly lock a mutex. </p>
  
  <p>This is some pseudo code of what I'd like to do: </p>
  
  <pre><code> while(buffer.isLocked()) 
   { 
      buffer.wait();
   }
  
   buffer.lock();
   buffer.add(tcpPacket);
   buffer.unlock();
   buffer.notify();
  </code></pre>
  
  <p>This is my java code so far: </p>
  
  <pre><code>void process(String tcpPacket)
  {
  
       synchronized(buffer)
       {
           buffer.add(tcpPacket);
       }
       buffer.notify();
  }
  </code></pre>