diff --git a/object-pool/src/main/java/com/iluwatar/App.java b/object-pool/src/main/java/com/iluwatar/App.java index 1ad35a147..4fbc8044a 100644 --- a/object-pool/src/main/java/com/iluwatar/App.java +++ b/object-pool/src/main/java/com/iluwatar/App.java @@ -1,5 +1,23 @@ package com.iluwatar; +/** + * + * When it is necessary to work with a large number of objects that are particularly expensive to instantiate + * and each object is only needed for a short period of time, the performance of an entire application may be + * adversely affected. An object pool design pattern may be deemed desirable in cases such as these. + * + * The object pool design pattern creates a set of objects that may be reused. When a new object is needed, it + * is requested from the pool. If a previously prepared object is available it is returned immediately, avoiding + * the instantiation cost. If no objects are present in the pool, a new item is created and returned. When the + * object has been used and is no longer needed, it is returned to the pool, allowing it to be used again in the + * future without repeating the computationally expensive instantiation process. It is important to note that + * once an object has been used and returned, existing references will become invalid. + * + * In this example we have created OliphauntPool inheriting from generic ObjectPool. Oliphaunts can be checked + * out from the pool and later returned to it. The pool tracks created instances and their status (available, + * inUse). + * + */ public class App { public static void main( String[] args ) { diff --git a/object-pool/src/main/java/com/iluwatar/ObjectPool.java b/object-pool/src/main/java/com/iluwatar/ObjectPool.java index 3f9d6f3aa..4bb10faab 100644 --- a/object-pool/src/main/java/com/iluwatar/ObjectPool.java +++ b/object-pool/src/main/java/com/iluwatar/ObjectPool.java @@ -2,10 +2,16 @@ package com.iluwatar; import java.util.HashSet; +/** + * + * Generic object pool + * + * @param + */ public abstract class ObjectPool { - HashSet available = new HashSet<>(); - HashSet inUse = new HashSet<>(); + private HashSet available = new HashSet<>(); + private HashSet inUse = new HashSet<>(); protected abstract T create(); diff --git a/object-pool/src/main/java/com/iluwatar/Oliphaunt.java b/object-pool/src/main/java/com/iluwatar/Oliphaunt.java index 928168dfe..318387cae 100644 --- a/object-pool/src/main/java/com/iluwatar/Oliphaunt.java +++ b/object-pool/src/main/java/com/iluwatar/Oliphaunt.java @@ -1,5 +1,10 @@ package com.iluwatar; +/** + * + * Oliphaunts are expensive to create + * + */ public class Oliphaunt { private static int counter = 1; @@ -8,6 +13,11 @@ public class Oliphaunt { public Oliphaunt() { id = counter++; + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } } public int getId() { diff --git a/object-pool/src/main/java/com/iluwatar/OliphauntPool.java b/object-pool/src/main/java/com/iluwatar/OliphauntPool.java index 23c544630..98db8df9e 100644 --- a/object-pool/src/main/java/com/iluwatar/OliphauntPool.java +++ b/object-pool/src/main/java/com/iluwatar/OliphauntPool.java @@ -1,5 +1,10 @@ package com.iluwatar; +/** + * + * Oliphaunt object pool + * + */ public class OliphauntPool extends ObjectPool { @Override