public class InterProcessReadWriteLock extends Object
A re-entrant read/write 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. Further, this mutex is "fair" - each user will get the mutex in the order requested (from ZK's point of view).
A read write lock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader processes, so long as there are no writers. The write lock is exclusive.
Reentrancy
This lock allows both readers and writers to reacquire read or write locks in the style of a
re-entrant lock. Non-re-entrant readers are not allowed until all write locks held by the
writing thread/process have been released. Additionally, a writer can acquire the read lock, but not
vice-versa. If a reader tries to acquire the write lock it will never succeed.
Lock downgrading
Re-entrancy also allows downgrading from the write lock to a read lock, by acquiring the write
lock, then the read lock and then releasing the write lock. However, upgrading from a read
lock to the write lock is not possible.
Modifier and Type | Class and Description |
---|---|
static class |
InterProcessReadWriteLock.ReadLock |
static class |
InterProcessReadWriteLock.WriteLock |
Modifier | Constructor and Description |
---|---|
|
InterProcessReadWriteLock(CuratorFramework client,
String basePath) |
|
InterProcessReadWriteLock(CuratorFramework client,
String basePath,
byte[] lockData) |
protected |
InterProcessReadWriteLock(InterProcessReadWriteLock.WriteLock writeLock,
InterProcessReadWriteLock.ReadLock readLock) |
Modifier and Type | Method and Description |
---|---|
InterProcessReadWriteLock.ReadLock |
readLock()
Returns the lock used for reading.
|
InterProcessReadWriteLock.WriteLock |
writeLock()
Returns the lock used for writing.
|
public InterProcessReadWriteLock(CuratorFramework client, String basePath)
client
- the clientbasePath
- path to use for lockingpublic InterProcessReadWriteLock(CuratorFramework client, String basePath, byte[] lockData)
client
- the clientbasePath
- path to use for lockinglockData
- the data to store in the lock nodesprotected InterProcessReadWriteLock(InterProcessReadWriteLock.WriteLock writeLock, InterProcessReadWriteLock.ReadLock readLock)
public InterProcessReadWriteLock.ReadLock readLock()
public InterProcessReadWriteLock.WriteLock writeLock()
Copyright © 2011–2023 The Apache Software Foundation. All rights reserved.