java.util.concurrent.locks
Stay organized with collections
Save and categorize content based on your preferences.
Interfaces and classes providing a framework for locking and waiting
for conditions that is distinct from built-in synchronization and
monitors. The framework permits much greater flexibility in the use of
locks and conditions, at the expense of more awkward syntax.
The Lock
interface supports
locking disciplines that differ in semantics (reentrant, fair, etc),
and that can be used in non-block-structured contexts including
hand-over-hand and lock reordering algorithms. The main implementation
is ReentrantLock
.
The ReadWriteLock
interface
similarly defines locks that may be shared among readers but are
exclusive to writers. Only a single implementation, ReentrantReadWriteLock
, is provided, since
it covers most standard usage contexts. But programmers may create
their own implementations to cover nonstandard requirements.
The Condition
interface
describes condition variables that may be associated with Locks.
These are similar in usage to the implicit monitors accessed using
Object.wait
, but offer extended capabilities.
In particular, multiple Condition
objects may be associated
with a single Lock
. To avoid compatibility issues, the
names of Condition
methods are different from the
corresponding Object
versions.
The AbstractQueuedSynchronizer
class serves as a useful superclass for defining locks and other
synchronizers that rely on queuing blocked threads. The AbstractQueuedLongSynchronizer
class
provides the same functionality but extends support to 64 bits of
synchronization state. Both extend class AbstractOwnableSynchronizer
, a simple
class that helps record the thread currently holding exclusive
synchronization. The LockSupport
class provides lower-level blocking and unblocking support that is
useful for those developers implementing their own customized lock
classes.
Interfaces
Condition |
Condition factors out the Object monitor
methods (wait , notify
and notifyAll ) into distinct objects to
give the effect of having multiple wait-sets per object, by
combining them with the use of arbitrary Lock implementations. |
Lock |
Lock implementations provide more extensive locking
operations than can be obtained using synchronized methods
and statements. |
ReadWriteLock |
A ReadWriteLock maintains a pair of associated locks , one for read-only operations and one for writing. |
Classes
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\u003eThis package provides a framework for implementing locks and conditions that offers greater flexibility than built-in synchronization.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eLock\u003c/code\u003e interface supports different locking disciplines and can be used in various contexts beyond block-structured synchronization.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eReadWriteLock\u003c/code\u003e interface allows for shared read access and exclusive write access to resources.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eCondition\u003c/code\u003e interface provides condition variables similar to \u003ccode\u003eObject.wait\u003c/code\u003e and \u003ccode\u003enotify\u003c/code\u003e but with extended capabilities.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eAbstractQueuedSynchronizer\u003c/code\u003e class facilitates building custom locks and synchronizers based on FIFO queues.\u003c/p\u003e\n"]]],[],null,["# java.util.concurrent.locks\n\nInterfaces and classes providing a framework for locking and waiting for conditions that is distinct from built-in synchronization and monitors. The framework permits much greater flexibility in the use of locks and conditions, at the expense of more awkward syntax.\n\nThe [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) interface supports\nlocking disciplines that differ in semantics (reentrant, fair, etc),\nand that can be used in non-block-structured contexts including\nhand-over-hand and lock reordering algorithms. The main implementation\nis [ReentrantLock](../../../../../reference/java/util/concurrent/locks/ReentrantLock.html).\n\nThe [ReadWriteLock](../../../../../reference/java/util/concurrent/locks/ReadWriteLock.html) interface\nsimilarly defines locks that may be shared among readers but are\nexclusive to writers. Only a single implementation, [ReentrantReadWriteLock](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.html), is provided, since\nit covers most standard usage contexts. But programmers may create\ntheir own implementations to cover nonstandard requirements.\n\nThe [Condition](../../../../../reference/java/util/concurrent/locks/Condition.html) interface\ndescribes condition variables that may be associated with Locks.\nThese are similar in usage to the implicit monitors accessed using\n`Object.wait`, but offer extended capabilities.\nIn particular, multiple `Condition` objects may be associated\nwith a single `Lock`. To avoid compatibility issues, the\nnames of `Condition` methods are different from the\ncorresponding `Object` versions.\n\nThe [AbstractQueuedSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractQueuedSynchronizer.html)\nclass serves as a useful superclass for defining locks and other\nsynchronizers that rely on queuing blocked threads. The [AbstractQueuedLongSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.html) class\nprovides the same functionality but extends support to 64 bits of\nsynchronization state. Both extend class [AbstractOwnableSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractOwnableSynchronizer.html), a simple\nclass that helps record the thread currently holding exclusive\nsynchronization. The [LockSupport](../../../../../reference/java/util/concurrent/locks/LockSupport.html)\nclass provides lower-level blocking and unblocking support that is\nuseful for those developers implementing their own customized lock\nclasses.\n\n### Interfaces\n\n|-----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Condition](../../../../../reference/java/util/concurrent/locks/Condition.html) | `Condition` factors out the `Object` monitor methods ([wait](../../../../../reference/java/lang/Object.html#wait()), [notify](../../../../../reference/java/lang/Object.html#notify()) and [notifyAll](../../../../../reference/java/lang/Object.html#notifyAll())) into distinct objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) implementations. |\n| [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) | `Lock` implementations provide more extensive locking operations than can be obtained using `synchronized` methods and statements. |\n| [ReadWriteLock](../../../../../reference/java/util/concurrent/locks/ReadWriteLock.html) | A `ReadWriteLock` maintains a pair of associated [locks](../../../../../reference/java/util/concurrent/locks/Lock.html), one for read-only operations and one for writing. |\n\n### Classes\n\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [AbstractOwnableSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractOwnableSynchronizer.html) | A synchronizer that may be exclusively owned by a thread. |\n| [AbstractQueuedLongSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.html) | A version of [AbstractQueuedSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractQueuedSynchronizer.html) in which synchronization state is maintained as a `long`. |\n| [AbstractQueuedLongSynchronizer.ConditionObject](../../../../../reference/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.ConditionObject.html) | Condition implementation for a [AbstractQueuedLongSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.html) serving as the basis of a [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) implementation. |\n| [AbstractQueuedSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractQueuedSynchronizer.html) | Provides a framework for implementing blocking locks and related synchronizers (semaphores, events, etc) that rely on first-in-first-out (FIFO) wait queues. |\n| [AbstractQueuedSynchronizer.ConditionObject](../../../../../reference/java/util/concurrent/locks/AbstractQueuedSynchronizer.ConditionObject.html) | Condition implementation for a [AbstractQueuedSynchronizer](../../../../../reference/java/util/concurrent/locks/AbstractQueuedSynchronizer.html) serving as the basis of a [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) implementation. |\n| [LockSupport](../../../../../reference/java/util/concurrent/locks/LockSupport.html) | Basic thread blocking primitives for creating locks and other synchronization classes. |\n| [ReentrantLock](../../../../../reference/java/util/concurrent/locks/ReentrantLock.html) | A reentrant mutual exclusion [Lock](../../../../../reference/java/util/concurrent/locks/Lock.html) with the same basic behavior and semantics as the implicit monitor lock accessed using `synchronized` methods and statements, but with extended capabilities. |\n| [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| [ReentrantReadWriteLock.ReadLock](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock.html) | The lock returned by method [ReentrantReadWriteLock.readLock()](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock()). |\n| [ReentrantReadWriteLock.WriteLock](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.WriteLock.html) | The lock returned by method [ReentrantReadWriteLock.writeLock()](../../../../../reference/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock()). |"]]