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 event of duplicate class loadings.
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 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.
Two lock files are considered the same if they refer to the same physical file. Internally,
this class normalizes the lock file's path to detect same physical files via equals(). Therefore,
the client does not need to normalize the lock file's path when constructing a ReadWriteProcessLock
.
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.io.File 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.io.File 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).
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 to detect same physical files via equals(), so the client does not need to normalize the file's path in advance.
The lock file may not yet exist. In order for the lock file to be created, the parent directory of the lock file must exist when this method is called.
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 lock file does not exist, or a
regular file with the same path as the lock file accidentally existspublic ReadWriteProcessLock.Lock readLock()
public ReadWriteProcessLock.Lock writeLock()