@Immutable
public final class ReadWriteProcessLock
extends java.lang.Object
ReentrantReadWriteLock
used to synchronize threads both within the same
JVM and across different JVMs, even when classes (including this class) are loaded multiple times
by different 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 they refer to the same physical file). 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 differences between ReadWriteProcessLock
and a regular Java lock such as
ReentrantReadWriteLock
are:
ReadWriteProcessLock
is itself not a lock object (which threads and processes are
directly synchronized on), but 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.
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.
|
java.lang.String |
toString() |
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 they
refer to the same physical file).
The lock file may or may not exist when this constructor is called. It will be created if it does not yet exist and will not be deleted after this constructor is called.
In order for the lock file to be created (if it does not yet exist), the parent directory of the lock file must exist when this constructor is called.
IMPORTANT: The lock file must be used solely for synchronization purposes. The client of this class must not access (read, write, or delete) the lock file. The client may delete the lock file only when the locking mechanism is no longer in use.
This constructor will normalize the lock file's path first to detect same physical files via equals(). However, it is still recommended that the client normalize the file's path in advance before calling this constructor.
lockFile
- the lock file, used solely for synchronization purposes; it may not yet
exist, but its parent directory must existjava.lang.IllegalArgumentException
- if the parent directory of the lock file does not exist, or
if a directory or a regular (non-empty) file with the same path as the lock file
accidentally exists@NonNull public ReadWriteProcessLock.Lock readLock()
@NonNull public ReadWriteProcessLock.Lock writeLock()
public java.lang.String toString()
toString
in class java.lang.Object