Skip to content

Commit 106b0b9

Browse files
committed
Merge pull request iluwatar#1 from yusufaytas/master
Update creating mechanism.
2 parents 1eddfa8 + d1e1be1 commit 106b0b9

File tree

8 files changed

+98
-2
lines changed

8 files changed

+98
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,14 @@
239239
* many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them
240240
* the classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes
241241

242+
##Double Checked Locking
243+
**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.
242244

245+
![alt text](https://github.com/yusufaytas/java-design-patterns/blob/master/double-checked-locking/etc/double_checked_locking.jpeg "Double Checked Locking")
246+
247+
**Applicability:** Use the Double Checked Locking pattern when
248+
* 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.
249+
* 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.
243250

244251
# Frequently asked questions
245252

22.6 KB
Loading

double-checked-locking/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>com.iluwatar</groupId>
5+
<artifactId>java-design-patterns</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>double-checked-locking</artifactId>
9+
<dependencies>
10+
<dependency>
11+
<groupId>junit</groupId>
12+
<artifactId>junit</artifactId>
13+
<version>3.8.1</version>
14+
<scope>test</scope>
15+
</dependency>
16+
</dependencies>
17+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.iluwatar;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
*
8+
* In Inventory we store the items with a given size. However,
9+
* we do not store more items than the inventory size. To address
10+
* concurrent access problems we use double checked locking to add
11+
* item to inventory. In this method, the thread which gets the lock
12+
* first adds the item.
13+
*/
14+
15+
public class App
16+
{
17+
public static void main( String[] args )
18+
{
19+
final Inventory inventory = new Inventory(1000);
20+
ExecutorService executorService = Executors.newFixedThreadPool(3);
21+
for (int i = 0; i < 3; i++) {
22+
executorService.execute(new Runnable() {
23+
@Override
24+
public void run() {
25+
while(inventory.addItem(new Item()));
26+
}
27+
});
28+
}
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.iluwatar;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
9+
public class Inventory {
10+
11+
private int inventorySize;
12+
private List<Item> items;
13+
private Lock lock = new ReentrantLock();
14+
15+
public Inventory(int inventorySize) {
16+
this.inventorySize = inventorySize;
17+
this.items = new ArrayList<Item>(inventorySize);
18+
}
19+
20+
public boolean addItem(Item item){
21+
if(items.size()<inventorySize){
22+
lock.lock();
23+
try{
24+
if(items.size()<inventorySize){
25+
items.add(item);
26+
System.out.println(Thread.currentThread());
27+
return true;
28+
}
29+
}finally{
30+
lock.unlock();
31+
}
32+
}
33+
return false;
34+
}
35+
36+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.iluwatar;
2+
3+
public class Item {
4+
String name;
5+
int level;
6+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<module>strategy</module>
4242
<module>template-method</module>
4343
<module>visitor</module>
44+
<module>double-checked-locking</module>
4445
</modules>
4546

4647
<build>

singleton/src/main/java/com/iluwatar/IvoryTower.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ public class IvoryTower {
99

1010
private static IvoryTower instance = new IvoryTower();
1111

12-
private IvoryTower() {
13-
}
12+
private IvoryTower() {}
1413

1514
public static IvoryTower getInstance() {
1615
return instance;

0 commit comments

Comments
 (0)