title: Apache Curator Lock Recipes Revoking
body: <p>I am trying to test the revocable Locking in Apache Curator. I have two threads which tries to acquire a lock. If the first test acquires the lock, the second thread can ask the first thread to release the lock so that the 2nd thread can acquire it</p>
  
  <pre><code>        RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMills, maxRetries);
          CuratorFramework client = CuratorFrameworkFactory.newClient(hosts, retryPolicy);
          client.start();
  
          final InterProcessMutex lock = new InterProcessMutex(client, lockBasePath);
  
          Collection&lt;String&gt; nodes =  lock.getParticipantNodes();
  
          lock.makeRevocable(new RevocationListener&lt;InterProcessMutex&gt;(){
  
              @Override
              public void revocationRequested(InterProcessMutex lock1) {
                  try {
                      if(lock.isAcquiredInThisProcess()){
                          lock.release();
                      }
  
                  } catch (Exception e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
              }
  
          });
  
          if(nodes!=null &amp;&amp; !nodes.isEmpty()){
              Revoker.attemptRevoke(client, nodes.iterator().next());
          }
  
          if (lock.acquire(waitTimeSeconds, TimeUnit.SECONDS)) {
              try {
                  doSomeWork(lockName);
              } finally {
                  lock.release();
              }
          } else {
              System.err.printf("%s timed out after %d seconds waiting to acquire lock on %s\n",
                      lockName, waitTimeSeconds, lockPath);
          }
  </code></pre>
  
  <p>The problem is, when the 2nd thread calls the attemptRevoke, the callback async method is called on the first process, but since its a call back method that's a third thread, and if that invokes the lock.release it throws an Exception </p>
  
  <blockquote>
    <p>java.lang.IllegalMonitorStateException: You do not own the lock</p>
  </blockquote>
  
  <p>That is as per the api </p>
  
  <blockquote>
    <p>release() Perform one release of the mutex if the calling thread is
    the same thread that acquired it.</p>
  </blockquote>
  
  <p>So basically this is never possible because callbacks will always be another thread. Is there any other way to achieve this? </p>
  
  <p>Thanks for any suggestions</p>
  
  <p>-Tatha</p>