Files
abstract-document
abstract-factory
adapter
aggregator-microservices
api-gateway
async-method-invocation
bridge
builder
business-delegate
caching
callback
chain
command
composite
dao
data-mapper
decorator
delegation
dependency-injection
double-checked-locking
etc
src
README.md
pom.xml
double-dispatch
event-aggregator
event-driven-architecture
execute-around
facade
factory-kit
factory-method
feature-toggle
fluentinterface
flux
flyweight
front-controller
half-sync-half-async
hexagonal
intercepting-filter
interpreter
iterator
layers
lazy-loading
mediator
memento
message-channel
model-view-controller
model-view-presenter
monad
monostate
multiton
mute-idiom
mutex
naked-objects
null-object
object-pool
observer
page-object
poison-pill
private-class-data
producer-consumer
promise
property
prototype
proxy
publish-subscribe
reactor
reader-writer-lock
repository
resource-acquisition-is-initialization
semaphore
servant
service-layer
service-locator
singleton
specification
state
step-builder
strategy
template-method
thread-pool
tolerant-reader
twin
value-object
visitor
.gitignore
.travis.yml
CODE_COVERAGE.md
CONTRIBUTING.MD
LICENSE.md
README.md
checkstyle-suppressions.xml
checkstyle.xml
exclude-pmd.properties
faq.md
pom.xml
update-ghpages.sh
java-design-patterns/double-checked-locking/README.md

26 lines
1004 B
Markdown
Raw Normal View History

---
layout: pattern
title: Double Checked Locking
folder: double-checked-locking
permalink: /patterns/double-checked-locking/
categories: Concurrency
2015-12-28 15:52:44 +02:00
tags:
- Java
- Difficulty-Beginner
- Idiom
---
## Intent
Reduce the overhead of acquiring a lock by first testing the
locking criterion (the "lock hint") without actually acquiring the lock. Only
if the locking criterion check indicates that locking is required does the
actual locking logic proceed.
![alt text](./etc/double_checked_locking_1.png "Double Checked Locking")
## Applicability
Use the Double Checked Locking pattern when
* there is a concurrent access in object creation, e.g. singleton, where you want to create single instance of the same class and checking if it's null or not maybe not be enough when there are two or more threads that checks if instance is null or not.
* there is a concurrent access on a method where method's behaviour changes according to the some constraints and these constraint change within this method.