title: How do determine if an object is locked (synchronized) so not to block in Java?
body: <p>I have a process A that contains a table in memory with a set of records (recordA, recordB, etc...)</p>
  
  <p>Now, this process can launch many threads that affect the records, and sometimes we can have 2 threads trying to access the same record - this situation must be denied. Specifically if a record is LOCKED by one thread I want the other thread to abort (I do not want to BLOCK or WAIT).</p>
  
  <p>Currently I do something like this:</p>
  
  <pre><code>synchronized(record)
  {
  performOperation(record);
  }
  </code></pre>
  
  <p>But this is causing me problems ... because while Process1 is performing the operation, if Process2 comes in it blocks/waits on the synchronized statement and when Process1 is finished it performs the operation. Instead I want something like this:</p>
  
  <pre><code>if (record is locked)
     return;
  
  synchronized(record)
  {
  performOperation(record);
  }
  </code></pre>
  
  <p>Any clues on how this can be accomplished?
  Any help would be much appreciated.
  Thanks,</p>