public final class ReadWriteProcessLock
extends java.lang.Object
ReentrantReadWriteLock
used to synchronize threads both within the same
JVM and across different JVMs, even in the presence of multiple custom class loaders.
Typically, there exists only one JVM in a process. Therefore, we call this class ReadWriteProcessLock
, although in a multi-JVM process, this is not strictly correct. Also, from
here we will use the term JVM and process interchangeably.
Threads and processes will be synchronized on the same lock file (two lock files are the same
if one equals() the other). The client using ReadWriteProcessLock
will provide a lock
file when constructing a ReadWriteProcessLock
instance. Then, different threads and
processes using the same or different ReadWriteProcessLock
instances on the same lock
file can be synchronized.
The basic usage of this class is similar to ReentrantReadWriteLock
and Lock
. Below is a typical example.
ReadWriteProcessLock readWriteProcessLock = new ReadWriteProcessLock(lockFile);
ReadWriteProcessLock.Lock lock =
useSharedLock
? readWriteProcessLock.readLock()
: readWriteProcessLock.writeLock();
lock.lock();
try {
runnable.run();
} finally {
lock.unlock();
}
The key usage difference between ReadWriteProcessLock
and a regular Java lock such as
ReentrantReadWriteLock
is that ReadWriteProcessLock
is itself not a lock object
(which threads and processes are directly synchronized on), but only a proxy to the actual lock
object (a lock file in this case). Therefore, there could be multiple instances of ReadWriteProcessLock
on the same lock file.
Another distinction is that two lock files are considered the same if one equals() the
other. Internally, this class normalizes the lock file's path so that the paths can be
correctly compared by equals(), but it is good practice for the client to also normalize the file
paths when creating ReadWriteProcessLock
instances.
This lock is not reentrant.
This class is thread-safe.
Modifier and Type | Class and Description |
---|---|
static interface |
ReadWriteProcessLock.Lock |
Constructor and Description |
---|
ReadWriteProcessLock(java.nio.file.Path lockFile)
Creates a
ReadWriteProcessLock instance for the given lock file. |
Modifier and Type | Method and Description |
---|---|
ReadWriteProcessLock.Lock |
readLock()
Returns the lock used for reading.
|
ReadWriteProcessLock.Lock |
writeLock()
Returns the lock used for writing.
|
public ReadWriteProcessLock(@NonNull java.nio.file.Path lockFile)
ReadWriteProcessLock
instance for the given lock file. Threads and
processes will be synchronized on the same lock file (two lock files are the same if one
equals() the other).
It is strongly recommended that the lock file is used solely for synchronization purposes. The client of this class should not access (read, write, or delete) the lock file. The lock file may or may not exist when this method is called. However, it will be created and will not be deleted once the client starts using the locking mechanism provided by this class. The client may delete the lock files only when the locking mechanism is no longer in use.
This method will normalize the lock file's path first so that the paths can be correctly compared by equals().
lockFile
- the lock file, used solely for synchronization purposespublic ReadWriteProcessLock.Lock readLock()
public ReadWriteProcessLock.Lock writeLock()