Class ReadWriteThreadLock
- java.lang.Object
-
- com.android.utils.concurrency.ReadWriteThreadLock
-
@Immutable public final class ReadWriteThreadLock extends java.lang.ObjectA lock similar toReentrantReadWriteLockused to synchronize threads within the same JVM, even when classes (including this class) are loaded multiple times by different class loaders.Motivation of
ReadWriteThreadLock: When attempting to synchronize threads within the same JVM (e.g., making sure that only one instance of a class executes some action at a time), we may choose to create a lock associated with the class and require instances of the class to acquire the lock before executing. However, if that class is loaded multiple times by different class loaders, the JVM considers them as different classes, and there will be multiple locks associated with those classes. The desired effect of synchronizing all threads within the JVM is not achieved; instead, each lock can only take effect for instances of the same class loaded by the same class loader.We create
ReadWriteThreadLockto address that limitation. AReadWriteThreadLockcan be used to synchronize *all* threads in a JVM, even when a class usingReadWriteThreadLockor theReadWriteThreadLockclass itself is loaded multiple times by different class loaders.Threads will be synchronized on the same lock object (two lock objects are the same if one equals() the other). The client using
ReadWriteThreadLockwill provide a lock object when constructing aReadWriteThreadLockinstance. Then, different threads using the same or differentReadWriteThreadLockinstances on the same lock object can be synchronized.The basic usage of this class is similar to
ReentrantReadWriteLockandLock. Below is a typical example.ReadWriteThreadLock readWriteThreadLock = new ReadWriteThreadLock(lockObject); ReadWriteThreadLock.Lock lock = useSharedLock ? readWriteThreadLock.readLock() : readWriteThreadLock.writeLock(); lock.lock(); try { runnable.run(); } finally { lock.unlock(); }The key usage differences between
ReadWriteThreadLockand a regular Java lock such asReentrantReadWriteLockare:ReadWriteThreadLockis itself not a lock object (which threads are directly synchronized on), but a proxy to the actual lock object. Therefore, there could be multiple instances ofReadWriteThreadLockon the same lock object.- Two lock objects are considered the same if one equals() the other.
This lock is reentrant, down-gradable, but not upgradable (similar to
ReentrantReadWriteLock).This class is thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceReadWriteThreadLock.Lock
-
Constructor Summary
Constructors Constructor Description ReadWriteThreadLock(java.lang.Object lockObject)Creates aReadWriteThreadLockinstance for the given lock object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ReadWriteThreadLock.LockreadLock()Returns the lock used for reading.java.lang.StringtoString()ReadWriteThreadLock.LockwriteLock()Returns the lock used for writing.
-
-
-
Constructor Detail
-
ReadWriteThreadLock
public ReadWriteThreadLock(@NonNull java.lang.Object lockObject)Creates aReadWriteThreadLockinstance for the given lock object. Threads will be synchronized on the same lock object (two lock objects are the same if one equals() the other).The class of the lock object must be loaded only once, to avoid accidentally comparing two objects of the same class where the class is loaded multiple times by different class loaders, and therefore one never equals() the other.
If the client uses a file as the lock object, in order for this class to detect same physical files via equals() the client needs to normalize the lock file's path when constructing a
ReadWriteThreadLock(preferably usingPath.toRealPath(LinkOption...)with follow-link orFile.getCanonicalFile(), as there are subtle issues with other methods such asPath.normalize()orFile.getCanonicalPath()).Pathis slightly preferred toFilefor consistency withReadWriteProcessLock.- Parameters:
lockObject- the lock object, whose class must be loaded only once
-
-
Method Detail
-
readLock
@NonNull public ReadWriteThreadLock.Lock readLock()
Returns the lock used for reading.
-
writeLock
@NonNull public ReadWriteThreadLock.Lock writeLock()
Returns the lock used for writing.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-