Class ReadWriteProcessLock
- java.lang.Object
-
- com.android.utils.concurrency.ReadWriteProcessLock
-
@Immutable public final class ReadWriteProcessLock extends java.lang.ObjectA lock similar toReentrantReadWriteLockused 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
ReadWriteProcessLockwill provide a lock file when constructing aReadWriteProcessLockinstance. Then, different threads and processes using the same or differentReadWriteProcessLockinstances on the same lock file can be synchronized.The basic usage of this class is similar to
ReentrantReadWriteLockandLock. 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
ReadWriteProcessLockand a regular Java lock such asReentrantReadWriteLockare:ReadWriteProcessLockis 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 ofReadWriteProcessLockon the same lock file.- Two lock files are considered the same if they refer to the same physical file.
This lock is not reentrant.
This class is thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceReadWriteProcessLock.Lock
-
Constructor Summary
Constructors Constructor Description ReadWriteProcessLock(java.nio.file.Path lockFile)Creates aReadWriteProcessLockinstance for the given lock file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ReadWriteProcessLock.LockreadLock()Returns the lock used for reading.java.lang.StringtoString()ReadWriteProcessLock.LockwriteLock()Returns the lock used for writing.
-
-
-
Constructor Detail
-
ReadWriteProcessLock
public ReadWriteProcessLock(@NonNull java.nio.file.Path lockFile)Creates aReadWriteProcessLockinstance 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.
- Parameters:
lockFile- the lock file, used solely for synchronization purposes; it may not yet exist, but its parent directory must exist- Throws:
java.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
-
-
Method Detail
-
readLock
@NonNull public ReadWriteProcessLock.Lock readLock()
Returns the lock used for reading.
-
writeLock
@NonNull public ReadWriteProcessLock.Lock writeLock()
Returns the lock used for writing.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-