title: ReentrantReadWriteLock released by different thread
body: <p>I am implementing optimistic transactions (BOCC) on a single server. At commit time the read and write sets are validated against the current database state (transactions are aborted if the state has changed since the read). If the validation is successful all the objects are written to the database.</p>
  
  <p>Validations (plus the database update) on different object sets can be concurrent, but overlapping object sets must be secured using read and write locks.</p>
  
  <p>I use the <code>ReentrantReadWriteLock</code> to secure the validation, which works fine. Now I am writing a recovery mechanism that repeats the update process if because of some error (after the validation) not all objects where written to the database.</p>
  
  <p>So the recovery repeats the database writes and then attempts to release the locks (after the recovery was successful). The problem is that I try to release the lock from a different thread (since the recovery is performed by another background service), which throws a <code>IllegalMonitorStateException</code>. The comment on the <code>unlock</code> method verifies this behavior.</p>
  
  <pre><code>    /**
       * Attempts to release this lock.
       *
       * &lt;p&gt;If the current thread is the holder of this lock then
       * the hold count is decremented. If the hold count is now
       * zero then the lock is released.  If the current thread is
       * not the holder of this lock then {@link
       * IllegalMonitorStateException} is thrown.
       *
       * @throws IllegalMonitorStateException if the current thread does not
       * hold this lock
       */
      public void unlock() {
  </code></pre>
  
  <p>Now my question is: Is there a lock in Java that I can use if I need:</p>
  
  <ul>
  <li>to release locks independent of the thread that has acquired them.</li>
  <li>to have read and write locks (and possibly upgrades from read to write)</li>
  <li>not to wait (block) for locks (I only use <code>tryLock</code>)?</li>
  </ul>