publicvoidacquire() 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 release() public boolean acquire(long time, TimeUnit unit) 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 release()
Parameters: time - time to wait unit - time unit Returns: trueif the mutex was acquired, falseif not
publicvoidmakeRevocable(RevocationListener<T> listener) 将锁设为可撤销的. 当别的进程或线程想让你释放锁是Listener会被调用。 Parameters: listener - the listener
如果你请求撤销当前的锁, 调用Revoker方法。
1 2 3 4 5 6 7 8 9
publicstaticvoidattemptRevoke(CuratorFramework client, String path) throws Exception Utility to mark a lock for revocation. Assuming that the lock has been registered with a RevocationListener, it will get called and the lock should be released. Note, however, that revocation is cooperative. Parameters: client - the client path - the path of the lock - usually from something like InterProcessMutex.getParticipantNodes()
publicvoiduse()throws InterruptedException { // 真实环境中我们会在这里访问/维护一个共享的资源 //这个例子在使用锁的情况下不会非法并发异常IllegalStateException //但是在无锁的情况由于sleep了一段时间,很容易抛出异常 if (!inUse.compareAndSet(false, true)) { thrownewIllegalStateException("Needs to be used by one client at a time"); } try { Thread.sleep((long) (3 * Math.random())); } finally { inUse.set(false); } } }
publicvoiddoWork(long time, TimeUnit unit)throws Exception { if (!lock.acquire(time, unit)) { thrownewIllegalStateException(clientName + " could not acquire the lock"); } try { System.out.println(clientName + " has the lock"); resource.use(); //access resource exclusively } finally { System.out.println(clientName + " releasing the lock"); lock.release(); // always release the lock in a finally block } } }
publicvoiddoWork(long time, TimeUnit unit)throws Exception { if (!lock.acquire(time, unit)) { thrownewIllegalStateException(clientName + " could not acquire the lock"); } System.out.println(clientName + " has the lock"); if (!lock.acquire(time, unit)) { thrownewIllegalStateException(clientName + " could not acquire the lock"); } System.out.println(clientName + " has the lock again"); try { resource.use(); //access resource exclusively } finally { System.out.println(clientName + " releasing the lock"); lock.release(); // always release the lock in a finally block lock.release(); // always release the lock in a finally block } }
publicvoiddoWork(long time, TimeUnit unit)throws Exception { if (!writeLock.acquire(time, unit)) { thrownewIllegalStateException(clientName + " could not acquire the writeLock"); } System.out.println(clientName + " has the writeLock"); if (!readLock.acquire(time, unit)) { thrownewIllegalStateException(clientName + " could not acquire the readLock"); } System.out.println(clientName + " has the readLock too"); try { resource.use(); //access resource exclusively } finally { System.out.println(clientName + " releasing the lock"); readLock.release(); // always release the lock in a finally block writeLock.release(); // always release the lock in a finally block } } }
public Lease acquire() public Collection<Lease> acquire(int qty) public Lease acquire(long time, TimeUnit unit) public Collection<Lease> acquire(int qty, long time, TimeUnit unit)