Skip to content

Commit eeea3c7

Browse files
committed
iluwatar#590 add explanation for Object Pool
1 parent 76fb9af commit eeea3c7

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

object-pool/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,104 @@ short periods of time it is advantageous to utilize the Object Pool pattern.
1515
The Object Pool provides a cache for instantiated objects tracking which ones
1616
are in use and which are available.
1717

18+
## Explanation
19+
20+
Real world example
21+
22+
> In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they are extremely expensive to create. The solution is to create a pool of them, track which ones are in-use, and instead of disposing them re-use the instances.
23+
24+
In plain words
25+
26+
> Object Pool manages a set of instances instead of creating and destroying them on demand.
27+
28+
Wikipedia says
29+
30+
> The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand.
31+
32+
**Programmatic Example**
33+
34+
Here's the basic Oliphaunt class. These are very expensive to create.
35+
36+
```java
37+
public class Oliphaunt {
38+
39+
private static AtomicInteger counter = new AtomicInteger(0);
40+
41+
private final int id;
42+
43+
public Oliphaunt() {
44+
id = counter.incrementAndGet();
45+
try {
46+
Thread.sleep(1000);
47+
} catch (InterruptedException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
52+
public int getId() {
53+
return id;
54+
}
55+
56+
@Override
57+
public String toString() {
58+
return String.format("Oliphaunt id=%d", id);
59+
}
60+
}
61+
```
62+
63+
Next we present the Object Pool and more specifically Oliphaunt Pool.
64+
65+
```java
66+
public abstract class ObjectPool<T> {
67+
68+
private Set<T> available = new HashSet<>();
69+
private Set<T> inUse = new HashSet<>();
70+
71+
protected abstract T create();
72+
73+
public synchronized T checkOut() {
74+
if (available.isEmpty()) {
75+
available.add(create());
76+
}
77+
var instance = available.iterator().next();
78+
available.remove(instance);
79+
inUse.add(instance);
80+
return instance;
81+
}
82+
83+
public synchronized void checkIn(T instance) {
84+
inUse.remove(instance);
85+
available.add(instance);
86+
}
87+
88+
@Override
89+
public synchronized String toString() {
90+
return String.format("Pool available=%d inUse=%d", available.size(), inUse.size());
91+
}
92+
}
93+
94+
public class OliphauntPool extends ObjectPool<Oliphaunt> {
95+
96+
@Override
97+
protected Oliphaunt create() {
98+
return new Oliphaunt();
99+
}
100+
}
101+
```
102+
103+
And finally here's how we utilize the pool.
104+
105+
```java
106+
var pool = new OliphauntPool();
107+
var oliphaunt1 = pool.checkOut();
108+
var oliphaunt2 = pool.checkOut();
109+
var oliphaunt3 = pool.checkOut();
110+
pool.checkIn(oliphaunt1);
111+
pool.checkIn(oliphaunt2);
112+
var oliphaunt4 = pool.checkOut();
113+
var oliphaunt5 = pool.checkOut();
114+
```
115+
18116
## Class diagram
19117
![alt text](./etc/object-pool.png "Object Pool")
20118

0 commit comments

Comments
 (0)