Skip to content

Commit e0bf323

Browse files
committed
Added comments.
1 parent bff34cf commit e0bf323

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

object-pool/src/main/java/com/iluwatar/App.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* When it is necessary to work with a large number of objects that are particularly expensive to instantiate
6+
* and each object is only needed for a short period of time, the performance of an entire application may be
7+
* adversely affected. An object pool design pattern may be deemed desirable in cases such as these.
8+
*
9+
* The object pool design pattern creates a set of objects that may be reused. When a new object is needed, it
10+
* is requested from the pool. If a previously prepared object is available it is returned immediately, avoiding
11+
* the instantiation cost. If no objects are present in the pool, a new item is created and returned. When the
12+
* object has been used and is no longer needed, it is returned to the pool, allowing it to be used again in the
13+
* future without repeating the computationally expensive instantiation process. It is important to note that
14+
* once an object has been used and returned, existing references will become invalid.
15+
*
16+
* In this example we have created OliphauntPool inheriting from generic ObjectPool. Oliphaunts can be checked
17+
* out from the pool and later returned to it. The pool tracks created instances and their status (available,
18+
* inUse).
19+
*
20+
*/
321
public class App {
422

523
public static void main( String[] args ) {

object-pool/src/main/java/com/iluwatar/ObjectPool.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
import java.util.HashSet;
44

5+
/**
6+
*
7+
* Generic object pool
8+
*
9+
* @param <T>
10+
*/
511
public abstract class ObjectPool<T> {
612

7-
HashSet<T> available = new HashSet<>();
8-
HashSet<T> inUse = new HashSet<>();
13+
private HashSet<T> available = new HashSet<>();
14+
private HashSet<T> inUse = new HashSet<>();
915

1016
protected abstract T create();
1117

object-pool/src/main/java/com/iluwatar/Oliphaunt.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Oliphaunts are expensive to create
6+
*
7+
*/
38
public class Oliphaunt {
49

510
private static int counter = 1;
@@ -8,6 +13,11 @@ public class Oliphaunt {
813

914
public Oliphaunt() {
1015
id = counter++;
16+
try {
17+
Thread.sleep(1000);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
1121
}
1222

1323
public int getId() {

object-pool/src/main/java/com/iluwatar/OliphauntPool.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Oliphaunt object pool
6+
*
7+
*/
38
public class OliphauntPool extends ObjectPool<Oliphaunt> {
49

510
@Override

0 commit comments

Comments
 (0)