com.netflix.curator.framework.recipes.locks
Class InterProcessSemaphoreMutex

java.lang.Object
  extended by com.netflix.curator.framework.recipes.locks.InterProcessSemaphoreMutex
All Implemented Interfaces:
InterProcessLock

public class InterProcessSemaphoreMutex
extends java.lang.Object
implements InterProcessLock

A NON re-entrant mutex that works across JVMs. Uses Zookeeper to hold the lock. All processes in all JVMs that use the same lock path will achieve an inter-process critical section.


Constructor Summary
InterProcessSemaphoreMutex(com.netflix.curator.framework.CuratorFramework client, java.lang.String path)
           
 
Method Summary
 void acquire()
          Acquire the mutex - blocking until it's available.
 boolean acquire(long time, java.util.concurrent.TimeUnit unit)
          Acquire the mutex - blocks until it's available or the given time expires.
 boolean isAcquiredInThisProcess()
          Returns true if the mutex is acquired by a thread in this JVM
 void release()
          Perform one release of the mutex if the calling thread is the same thread that acquired it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InterProcessSemaphoreMutex

public InterProcessSemaphoreMutex(com.netflix.curator.framework.CuratorFramework client,
                                  java.lang.String path)
Parameters:
client - the client
path - path for the lock
Method Detail

acquire

public void acquire()
             throws java.lang.Exception
Description copied from interface: InterProcessLock
Acquire the mutex - blocking until it's available. Note: the same thread can call acquire re-entrantly. Each call to acquire must be balanced by a call to InterProcessLock.release()

Specified by:
acquire in interface InterProcessLock
Throws:
java.lang.Exception - ZK errors, interruptions, another thread owns the lock

acquire

public boolean acquire(long time,
                       java.util.concurrent.TimeUnit unit)
                throws java.lang.Exception
Description copied from interface: InterProcessLock
Acquire the mutex - blocks until it's available or the given time expires. Note: the same thread can call acquire re-entrantly. Each call to acquire that returns true must be balanced by a call to InterProcessLock.release()

Specified by:
acquire in interface InterProcessLock
Parameters:
time - time to wait
unit - time unit
Returns:
true if the mutex was acquired, false if not
Throws:
java.lang.Exception - ZK errors, interruptions, another thread owns the lock

release

public void release()
             throws java.lang.Exception
Description copied from interface: InterProcessLock
Perform one release of the mutex if the calling thread is the same thread that acquired it. If the thread had made multiple calls to acquire, the mutex will still be held when this method returns.

Specified by:
release in interface InterProcessLock
Throws:
java.lang.Exception - ZK errors, interruptions, current thread does not own the lock

isAcquiredInThisProcess

public boolean isAcquiredInThisProcess()
Description copied from interface: InterProcessLock
Returns true if the mutex is acquired by a thread in this JVM

Specified by:
isAcquiredInThisProcess in interface InterProcessLock
Returns:
true/false