ReadWriteLock
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
A ReadWriteLock
maintains a pair of associated locks
, one for read-only operations and one for writing.
The {@linkplain #readLock read lock} may be held simultaneously
by multiple reader threads, so long as there are no writers.
The {@linkplain #writeLock write lock} is exclusive.
All ReadWriteLock
implementations must guarantee that
the memory synchronization effects of writeLock
operations
(as specified in the Lock
interface) also hold with respect
to the associated readLock
. That is, a thread successfully
acquiring the read lock will see all updates made upon previous
release of the write lock.
A read-write lock allows for a greater level of concurrency in
accessing shared data than that permitted by a mutual exclusion lock.
It exploits the fact that while only a single thread at a time (a
writer thread) can modify the shared data, in many cases any
number of threads can concurrently read the data (hence reader
threads).
In theory, the increase in concurrency permitted by the use of a read-write
lock will lead to performance improvements over the use of a mutual
exclusion lock. In practice this increase in concurrency will only be fully
realized on a multi-processor, and then only if the access patterns for
the shared data are suitable.
Whether or not a read-write lock will improve performance over the use
of a mutual exclusion lock depends on the frequency that the data is
read compared to being modified, the duration of the read and write
operations, and the contention for the data - that is, the number of
threads that will try to read or write the data at the same time.
For example, a collection that is initially populated with data and
thereafter infrequently modified, while being frequently searched
(such as a directory of some kind) is an ideal candidate for the use of
a read-write lock. However, if updates become frequent then the data
spends most of its time being exclusively locked and there is little, if any
increase in concurrency. Further, if the read operations are too short
the overhead of the read-write lock implementation (which is inherently
more complex than a mutual exclusion lock) can dominate the execution
cost, particularly as many read-write lock implementations still serialize
all threads through a small section of code. Ultimately, only profiling
and measurement will establish whether the use of a read-write lock is
suitable for your application.
Although the basic operation of a read-write lock is straight-forward,
there are many policy decisions that an implementation must make, which
may affect the effectiveness of the read-write lock in a given application.
Examples of these policies include:
- Determining whether to grant the read lock or the write lock, when
both readers and writers are waiting, at the time that a writer releases
the write lock. Writer preference is common, as writes are expected to be
short and infrequent. Reader preference is less common as it can lead to
lengthy delays for a write if the readers are frequent and long-lived as
expected. Fair, or "in-order" implementations are also possible.
- Determining whether readers that request the read lock while a
reader is active and a writer is waiting, are granted the read lock.
Preference to the reader can delay the writer indefinitely, while
preference to the writer can reduce the potential for concurrency.
- Determining whether the locks are reentrant: can a thread with the
write lock reacquire it? Can it acquire a read lock while holding the
write lock? Is the read lock itself reentrant?
- Can the write lock be downgraded to a read lock without allowing
an intervening writer? Can a read lock be upgraded to a write lock,
in preference to other waiting readers or writers?
You should consider all of these things when evaluating the suitability
of a given implementation for your application.
Public Methods
public
abstract
Lock
readLock
()
Returns the lock used for reading.
Returns
- the lock used for reading
public
abstract
Lock
writeLock
()
Returns the lock used for writing.
Returns
- the lock used for writing
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eReadWriteLock\u003c/code\u003e provides a pair of locks: one for read operations (shared among multiple threads) and one for write operations (exclusive to a single thread).\u003c/p\u003e\n"],["\u003cp\u003eIt aims to enhance concurrency by allowing multiple readers to access data simultaneously as long as no writer is active.\u003c/p\u003e\n"],["\u003cp\u003eImplementation policies of a \u003ccode\u003eReadWriteLock\u003c/code\u003e can significantly influence its effectiveness, including decisions about writer/reader preference and lock reentrancy.\u003c/p\u003e\n"],["\u003cp\u003ePerformance benefits of using \u003ccode\u003eReadWriteLock\u003c/code\u003e over traditional mutual exclusion locks depend on data access patterns, operation durations, and thread contention.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eReadWriteLock\u003c/code\u003e offers two primary methods: \u003ccode\u003ereadLock()\u003c/code\u003e to acquire the read lock and \u003ccode\u003ewriteLock()\u003c/code\u003e to acquire the write lock.\u003c/p\u003e\n"]]],[],null,["public interface **ReadWriteLock** \n\n|---|---|---|\n| Known Indirect Subclasses [ReentrantReadWriteLock](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.html) |-----------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ReentrantReadWriteLock](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.html) | An implementation of [ReadWriteLock](../../../../../reference/java/util/concurrent/locks/ReadWriteLock.html) supporting similar semantics to [ReentrantLock](../../../../../reference/java/util/concurrent/locks/ReentrantLock.html). | |||\n\nA `ReadWriteLock` maintains a pair of associated [locks](../../../../../reference/java/util/concurrent/locks/Lock.html), one for read-only operations and one for writing.\nThe {@linkplain #readLock read lock} may be held simultaneously\nby multiple reader threads, so long as there are no writers.\nThe {@linkplain #writeLock write lock} is exclusive.\n\nAll `ReadWriteLock` implementations must guarantee that\nthe memory synchronization effects of `writeLock` operations\n(as specified in the [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) interface) also hold with respect\nto the associated `readLock`. That is, a thread successfully\nacquiring the read lock will see all updates made upon previous\nrelease of the write lock.\n\nA read-write lock allows for a greater level of concurrency in\naccessing shared data than that permitted by a mutual exclusion lock.\nIt exploits the fact that while only a single thread at a time (a\n*writer* thread) can modify the shared data, in many cases any\nnumber of threads can concurrently read the data (hence *reader*\nthreads).\nIn theory, the increase in concurrency permitted by the use of a read-write\nlock will lead to performance improvements over the use of a mutual\nexclusion lock. In practice this increase in concurrency will only be fully\nrealized on a multi-processor, and then only if the access patterns for\nthe shared data are suitable.\n\nWhether or not a read-write lock will improve performance over the use\nof a mutual exclusion lock depends on the frequency that the data is\nread compared to being modified, the duration of the read and write\noperations, and the contention for the data - that is, the number of\nthreads that will try to read or write the data at the same time.\nFor example, a collection that is initially populated with data and\nthereafter infrequently modified, while being frequently searched\n(such as a directory of some kind) is an ideal candidate for the use of\na read-write lock. However, if updates become frequent then the data\nspends most of its time being exclusively locked and there is little, if any\nincrease in concurrency. Further, if the read operations are too short\nthe overhead of the read-write lock implementation (which is inherently\nmore complex than a mutual exclusion lock) can dominate the execution\ncost, particularly as many read-write lock implementations still serialize\nall threads through a small section of code. Ultimately, only profiling\nand measurement will establish whether the use of a read-write lock is\nsuitable for your application.\n\n\nAlthough the basic operation of a read-write lock is straight-forward,\nthere are many policy decisions that an implementation must make, which\nmay affect the effectiveness of the read-write lock in a given application.\nExamples of these policies include:\n\n- Determining whether to grant the read lock or the write lock, when both readers and writers are waiting, at the time that a writer releases the write lock. Writer preference is common, as writes are expected to be short and infrequent. Reader preference is less common as it can lead to lengthy delays for a write if the readers are frequent and long-lived as expected. Fair, or \"in-order\" implementations are also possible.\n- Determining whether readers that request the read lock while a reader is active and a writer is waiting, are granted the read lock. Preference to the reader can delay the writer indefinitely, while preference to the writer can reduce the potential for concurrency.\n- Determining whether the locks are reentrant: can a thread with the write lock reacquire it? Can it acquire a read lock while holding the write lock? Is the read lock itself reentrant?\n- Can the write lock be downgraded to a read lock without allowing an intervening writer? Can a read lock be upgraded to a write lock, in preference to other waiting readers or writers?\n\nYou should consider all of these things when evaluating the suitability of a given implementation for your application.\n\n\u003cbr /\u003e\n\nSee Also\n\n- [ReentrantReadWriteLock](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.html)\n- [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html)\n- [ReentrantLock](../../../../../reference/java/util/concurrent/locks/ReentrantLock.html) \n\nPublic Method Summary\n\n|--------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) | [readLock](../../../../../reference/java/util/concurrent/locks/ReadWriteLock.html#readLock())() Returns the lock used for reading. |\n| abstract [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) | [writeLock](../../../../../reference/java/util/concurrent/locks/ReadWriteLock.html#writeLock())() Returns the lock used for writing. |\n\nPublic Methods \n\npublic abstract [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html)\n**readLock**\n() \nReturns the lock used for reading. \n\nReturns\n\n- the lock used for reading \n\npublic abstract [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html)\n**writeLock**\n() \nReturns the lock used for writing. \n\nReturns\n\n- the lock used for writing"]]