mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-06 16:24:37 +00:00
docs: update double-checked locking
This commit is contained in:
@@ -3,8 +3,10 @@ title: Double-Checked Locking
|
|||||||
category: Concurrency
|
category: Concurrency
|
||||||
language: en
|
language: en
|
||||||
tag:
|
tag:
|
||||||
|
- Lazy initialization
|
||||||
- Optimization
|
- Optimization
|
||||||
- Performance
|
- Performance
|
||||||
|
- Thread management
|
||||||
---
|
---
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
@@ -27,9 +29,9 @@ Wikipedia says
|
|||||||
|
|
||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
The Double-Checked Locking pattern is used in the HolderThreadSafe class to ensure that the Heavy object is only created once, even when accessed from multiple threads. Here's how it works:
|
The Double-Checked Locking pattern is used in the `HolderThreadSafe` class to ensure that the `Heavy` object is only created once, even when accessed from multiple threads. Here's how it works:
|
||||||
|
|
||||||
Check if the object is initialized (first check): If it is, return it immediately.
|
1. Check if the object is initialized (first check): If it is, return it immediately.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
if (heavy == null) {
|
if (heavy == null) {
|
||||||
@@ -37,7 +39,7 @@ if (heavy == null) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Synchronize the block of code where the object is created: This ensures that only one thread can create the object.
|
2. Synchronize the block of code where the object is created: This ensures that only one thread can create the object.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
@@ -45,7 +47,7 @@ synchronized (this) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Check again if the object is initialized (second check): If another thread has already created the object by the time the current thread enters the synchronized block, return the created object.
|
3. Check again if the object is initialized. If another thread has already created the object by the time the current thread enters the synchronized block, return the created object.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
if (heavy == null) {
|
if (heavy == null) {
|
||||||
@@ -53,13 +55,13 @@ if (heavy == null) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Return the created object.
|
4. Return the created object.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
return heavy;
|
return heavy;
|
||||||
```
|
```
|
||||||
|
|
||||||
Here's the complete code for the HolderThreadSafe class:
|
Here's the complete code for the `HolderThreadSafe` class:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class HolderThreadSafe {
|
public class HolderThreadSafe {
|
||||||
@@ -83,11 +85,7 @@ public class HolderThreadSafe {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In this code, the Heavy object is only created when the getHeavy() method is called for the first time. This is known as lazy initialization. The double-checked locking pattern is used to ensure that the Heavy object is only created once, even when the getHeavy() method is called from multiple threads simultaneously.
|
In this code, the `Heavy` object is only created when the `getHeavy` method is called for the first time. This is known as lazy initialization. The double-checked locking pattern is used to ensure that the `Heavy` object is only created once, even when the `getHeavy` method is called from multiple threads simultaneously.
|
||||||
|
|
||||||
## Class diagram
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user