title: synchronized vs ReadWriteLock
body: <p>Hi i have extremly simplified a java problem in the following code snippet</p>
  
  <pre><code>public class WhichJavaSynchroIsBestHere {
  
      private BlockingQueue&lt;CustomObject&gt; queue = new PriorityBlockingQueue&lt;CustomObject&gt;();
  
      public void add( CustomObject customO ) {
  
          // The custom objects do never have the same id
          // so it's no horizontal concurrency but more vertical one a la producer/consumer
          if ( !queue.contains( customO ) ) {
              // Between the two statement a remove can happen
              queue.add( customO );
          }
      }
  
      public void remove( CustomObject customO ) {
          queue.remove( customO );
      }
  
      public static class CustomObject {
          long id;
          @Override
          public boolean equals( Object obj ) {
              if ( obj == null || getClass() != obj.getClass() )
                  return false;
              CustomObject other = (CustomObject) obj;
              return ( id == other.id;
          }
      }
  }
  </code></pre>
  
  <p>So this more of producer / consumer problem because presumably the two thread calling add do not pass the same <code>Customobject</code> (id), but this can happen when if one thread is calling add with same object as a second thread calling remove. The code section in between the if condition and the adding is what seems to me as not thread, safe, i was thinking about the object Lock (no <code>synchronized</code> blocks) to secure that section, but is a <code>ReadWriteLock</code> better?</p>