From 0509e48d37d981ccdf60b252a263af561f0f08a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Ayta=C5=9F?= Date: Sat, 6 Sep 2014 23:54:25 +0100 Subject: [PATCH 01/10] Update IvoryTower.java --- singleton/src/main/java/com/iluwatar/IvoryTower.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/singleton/src/main/java/com/iluwatar/IvoryTower.java b/singleton/src/main/java/com/iluwatar/IvoryTower.java index d4030cb2e5e1..30b8c4b299f6 100644 --- a/singleton/src/main/java/com/iluwatar/IvoryTower.java +++ b/singleton/src/main/java/com/iluwatar/IvoryTower.java @@ -7,12 +7,14 @@ */ public class IvoryTower { - private static IvoryTower instance = new IvoryTower(); + private static IvoryTower instance; - private IvoryTower() { - } + private IvoryTower() {} public static IvoryTower getInstance() { + if(instance == null){ + instance = IvoryTower(); + } return instance; } } From 1f0bedbbe13e231113234c0458942717877e5745 Mon Sep 17 00:00:00 2001 From: yusufaytas Date: Sun, 7 Sep 2014 00:34:26 +0100 Subject: [PATCH 02/10] double-checked-locking pattern is added. --- double-checked-locking/pom.xml | 9 +++++ .../src/main/java/com/iluwatar/App.java | 30 ++++++++++++++++ .../src/main/java/com/iluwatar/Inventory.java | 36 +++++++++++++++++++ .../src/main/java/com/iluwatar/Item.java | 5 +++ pom.xml | 1 + 5 files changed, 81 insertions(+) create mode 100644 double-checked-locking/pom.xml create mode 100644 double-checked-locking/src/main/java/com/iluwatar/App.java create mode 100644 double-checked-locking/src/main/java/com/iluwatar/Inventory.java create mode 100644 double-checked-locking/src/main/java/com/iluwatar/Item.java diff --git a/double-checked-locking/pom.xml b/double-checked-locking/pom.xml new file mode 100644 index 000000000000..5bb7c486b237 --- /dev/null +++ b/double-checked-locking/pom.xml @@ -0,0 +1,9 @@ + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.0-SNAPSHOT + + double-checked-locking + \ No newline at end of file diff --git a/double-checked-locking/src/main/java/com/iluwatar/App.java b/double-checked-locking/src/main/java/com/iluwatar/App.java new file mode 100644 index 000000000000..7e6bfd532c45 --- /dev/null +++ b/double-checked-locking/src/main/java/com/iluwatar/App.java @@ -0,0 +1,30 @@ +package com.iluwatar; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * + * In Inventory we store the items with a given size. However, + * we do not store more items than the inventory size. To address + * concurrent access problems we use double checked locking to add + * item to inventory. In this method, the thread which gets the lock + * first adds the item. + */ + +public class App +{ + public static void main( String[] args ) + { + final Inventory inventory = new Inventory(1000); + ExecutorService executorService = Executors.newFixedThreadPool(3); + for (int i = 0; i < 3; i++) { + executorService.execute(new Runnable() { + @Override + public void run() { + while(inventory.addItem(new Item())); + } + }); + } + } +} diff --git a/double-checked-locking/src/main/java/com/iluwatar/Inventory.java b/double-checked-locking/src/main/java/com/iluwatar/Inventory.java new file mode 100644 index 000000000000..a6ddc350d83d --- /dev/null +++ b/double-checked-locking/src/main/java/com/iluwatar/Inventory.java @@ -0,0 +1,36 @@ +package com.iluwatar; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + + +public class Inventory { + + private int inventorySize; + private List items; + private Lock lock = new ReentrantLock(); + + public Inventory(int inventorySize) { + this.inventorySize = inventorySize; + this.items = new ArrayList(inventorySize); + } + + public boolean addItem(Item item){ + if(items.size()strategy template-method visitor + double-checked-locking From 459b69677718c0ba6147d3745345da56a56d6ea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Ayta=C5=9F?= Date: Sun, 7 Sep 2014 00:42:00 +0100 Subject: [PATCH 03/10] Update pom.xml test dependency is added. --- double-checked-locking/pom.xml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/double-checked-locking/pom.xml b/double-checked-locking/pom.xml index 5bb7c486b237..4f608b70f0a8 100644 --- a/double-checked-locking/pom.xml +++ b/double-checked-locking/pom.xml @@ -6,4 +6,12 @@ 1.0-SNAPSHOT double-checked-locking - \ No newline at end of file + + + junit + junit + 3.8.1 + test + + + From 3d557490422caee1142a36eb33411406cf573bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Ayta=C5=9F?= Date: Sun, 7 Sep 2014 00:44:32 +0100 Subject: [PATCH 04/10] Update IvoryTower.java fixed the issue with class creation. --- singleton/src/main/java/com/iluwatar/IvoryTower.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/singleton/src/main/java/com/iluwatar/IvoryTower.java b/singleton/src/main/java/com/iluwatar/IvoryTower.java index 30b8c4b299f6..dadfc35484c8 100644 --- a/singleton/src/main/java/com/iluwatar/IvoryTower.java +++ b/singleton/src/main/java/com/iluwatar/IvoryTower.java @@ -13,7 +13,7 @@ private IvoryTower() {} public static IvoryTower getInstance() { if(instance == null){ - instance = IvoryTower(); + instance = new IvoryTower(); } return instance; } From c26ba609b221490eb67297011b69f91265f90a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Ayta=C5=9F?= Date: Sun, 7 Sep 2014 21:04:01 +0100 Subject: [PATCH 05/10] Added Double Checked Locking to documentation. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index d15edc87a4de..c2c47f13f0de 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,12 @@ * 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 * 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 +![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/double-checked-locking/etc/double_checked_locking.jpg "Double Checked Locking") + +**Applicability:** Use the Visitor pattern when +* an object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes +* 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 +* 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 # Frequently asked questions From c0c902870f3de62a3d661e99b3912e509a724c39 Mon Sep 17 00:00:00 2001 From: yusufaytas Date: Sun, 7 Sep 2014 21:06:26 +0100 Subject: [PATCH 06/10] Added class diagram to double checked locking. --- .../etc/double_checked_locking.jpeg | Bin 0 -> 23106 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 double-checked-locking/etc/double_checked_locking.jpeg diff --git a/double-checked-locking/etc/double_checked_locking.jpeg b/double-checked-locking/etc/double_checked_locking.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..5b219cc81832231fcd9cb7e73675ef9058b954fc GIT binary patch literal 23106 zcmcG$30M=^wl*9@L_v(q$`ln96*1a?v=9=N#)vdVKt(}_NGp?=RzM=el#0wljw3BL zL`4Kej01x*MM!{dX=MfpEt!sjKuncFC`qOAmFJv$zkAR5pZnc!_`^dzJgHP=uf5k^ z>s{}=3hi611h;tC4xb%3ojE$V!`Kf_`w6#b+fR%mM{!4S*h@z{jPt_j{9nGl-Q@rB z7CJiGmpJ43xRW|5x;iUxbBuL#jdip_9041n-rw*2ZM@%JI&*aA>dn)izrer{`#|+# z+#DTU-8pl0_4MY>#m=6^&g149>zORyv~8ZLf4KgNlV;zYzjS-P{q`r#<^kQ{O6Mb| zE-Wyxuw1g#YSn6oHEY+oxNdg)ev7-;58gYdJAHNq?%lUP=)l3?h@Xxgi;RkniBCv8 zoy0tooO&@WJtH$K`|_2m*RB`*a-;B0$=%X>W##uDJbhMGT~qt~MO}+P*xJ_q+wZS> zdi(kZ2Hy^eq%!$m-+ve#`#27L{xUVKnt{L0ezQvlr~B_y_Wx;@F=p4CxpQ^r>VLCK zXHL>L!;RUK1F0)gM!;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|V#ZC%uGMkxz8vOXriN6#U7W8)P37o01r}m@!}YSj&V-UCZ*`g-mqa0~0n~Ztp8~)}6>q}_gD#RWjD%Ey z2WAsQxUnwF-nYs{cvw74wC^`r^8eTQUwJ-1a|FB4X&PnlWcse269pa)VOQ4__g0qV zxg2r9q(eQ4|n?{!p4^c&EU237s` pSK7T5>)umkcz`wUnYNA{^77TA(?8AG{EuM#_rUzW^c(&#^q=J@cQ^n5 literal 0 HcmV?d00001 From 8fb4d3257eb100dc9fb3e0045a6560f842881819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Ayta=C5=9F?= Date: Sun, 7 Sep 2014 21:13:57 +0100 Subject: [PATCH 07/10] Added cases to use double-checked-locking. --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c2c47f13f0de..00043a2335be 100644 --- a/README.md +++ b/README.md @@ -241,11 +241,9 @@ ![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/double-checked-locking/etc/double_checked_locking.jpg "Double Checked Locking") -**Applicability:** Use the Visitor pattern when -* an object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes -* 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 -* 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 - +**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. # Frequently asked questions From 91a7980f7224843f4072ecbbe406d71434a3e3b6 Mon Sep 17 00:00:00 2001 From: yusufaytas Date: Sun, 7 Sep 2014 21:15:24 +0100 Subject: [PATCH 08/10] Added simple attributes to the Item. --- double-checked-locking/src/main/java/com/iluwatar/Item.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/double-checked-locking/src/main/java/com/iluwatar/Item.java b/double-checked-locking/src/main/java/com/iluwatar/Item.java index e0cada17e510..76e095e7e415 100644 --- a/double-checked-locking/src/main/java/com/iluwatar/Item.java +++ b/double-checked-locking/src/main/java/com/iluwatar/Item.java @@ -1,5 +1,6 @@ package com.iluwatar; public class Item { - + String name; + int level; } From d6aed142a7c07a3ab0e5723fe1a96e5b0796ebcd Mon Sep 17 00:00:00 2001 From: yusufaytas Date: Sun, 7 Sep 2014 21:21:24 +0100 Subject: [PATCH 09/10] removed the lazy instantiation from singleton to avoid threading problems. --- singleton/src/main/java/com/iluwatar/IvoryTower.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/singleton/src/main/java/com/iluwatar/IvoryTower.java b/singleton/src/main/java/com/iluwatar/IvoryTower.java index dadfc35484c8..113962eb84c7 100644 --- a/singleton/src/main/java/com/iluwatar/IvoryTower.java +++ b/singleton/src/main/java/com/iluwatar/IvoryTower.java @@ -7,14 +7,11 @@ */ public class IvoryTower { - private static IvoryTower instance; + private static IvoryTower instance = new IvoryTower(); private IvoryTower() {} public static IvoryTower getInstance() { - if(instance == null){ - instance = new IvoryTower(); - } return instance; } } From d1e1be13afe00b1637cddbc310b9d7fe64cc2012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Ayta=C5=9F?= Date: Mon, 8 Sep 2014 15:33:36 +0100 Subject: [PATCH 10/10] Added Intent to the Double Checked Locking. --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 00043a2335be..e032922e9138 100644 --- a/README.md +++ b/README.md @@ -239,7 +239,10 @@ * 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 * 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 -![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/double-checked-locking/etc/double_checked_locking.jpg "Double Checked Locking") +##Double Checked Locking +**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](https://github.com/yusufaytas/java-design-patterns/blob/master/double-checked-locking/etc/double_checked_locking.jpeg "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.