Skip to content

Commit cf32821

Browse files
committed
docs: update object pool
1 parent a26f4aa commit cf32821

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

object-pool/README.md

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@ title: Object Pool
33
category: Creational
44
language: en
55
tag:
6-
- Game programming
7-
- Performance
6+
- Game programming
7+
- Instantiation
8+
- Memory management
9+
- Performance
10+
- Resource management
11+
- Scalability
812
---
913

1014
## Also known as
1115

12-
Resource Pool
16+
* Resource Pool
1317

1418
## Intent
1519

16-
When objects are expensive to create and they are needed only for short periods of time it is
17-
advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated
18-
objects tracking which ones are in use and which are available.
20+
The Object Pool design pattern manages a pool of reusable objects, optimizing resource use by recycling objects rather than creating and destroying them repeatedly.
1921

2022
## Explanation
2123

2224
Real world example
2325

24-
> In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they
25-
> are extremely expensive to create. The solution is to create a pool of them, track which ones are
26-
> in-use, and instead of disposing them re-use the instances.
26+
> 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.
2727
2828
In plain words
2929

3030
> Object Pool manages a set of instances instead of creating and destroying them on demand.
3131
3232
Wikipedia says
3333

34-
> The object pool pattern is a software creational design pattern that uses a set of initialized
35-
> objects kept ready to use – a "pool" – rather than allocating and destroying them on demand.
34+
> 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.
3635
3736
**Programmatic Example**
3837

@@ -65,7 +64,7 @@ public class Oliphaunt {
6564
}
6665
```
6766

68-
Next we present the `ObjectPool` and more specifically `OliphauntPool`.
67+
Next, we present the `ObjectPool` and more specifically `OliphauntPool`.
6968

7069
```java
7170
public abstract class ObjectPool<T> {
@@ -137,11 +136,46 @@ Pool available=0 inUse=3
137136

138137
## Class diagram
139138

140-
![alt text](./etc/object-pool.png "Object Pool")
139+
![Object Pool](./etc/object-pool.png "Object Pool")
141140

142141
## Applicability
143142

144143
Use the Object Pool pattern when
145144

146-
* The objects are expensive to create (allocation cost).
147-
* You need a large number of short-lived objects (memory fragmentation).
145+
* You need to frequently create and destroy objects, leading to high resource allocation and deallocation costs.
146+
* The objects are expensive to create and maintain (e.g., database connections, thread pools).
147+
* A fixed number of objects need to be controlled, like in connection pooling.
148+
* Object reuse can significantly improve system performance and resource management.
149+
150+
## Known Uses
151+
152+
* Database connection pooling in Java applications.
153+
* Thread pooling in Java concurrent programming.
154+
* Pooling of socket connections in network applications.
155+
* Object pools in game development for frequently created and destroyed game objects.
156+
157+
## Consequences
158+
159+
Benefits:
160+
161+
* Improved Performance: Reduces the overhead of object creation and garbage collection.
162+
* Resource Management: Controls the number of instances, reducing resource contention and limiting resource usage.
163+
* Scalability: Allows the application to handle more requests by reusing a fixed number of objects.
164+
165+
Trade-offs:
166+
167+
* Complexity: Adds complexity to the codebase, requiring careful management of the pool.
168+
* Thread Safety: Requires careful handling of concurrent access to the pool, introducing potential synchronization issues.
169+
* Initialization Cost: Initial creation of the pool can be resource-intensive.
170+
171+
## Related Patterns
172+
173+
[Singleton](https://java-design-patterns.com/patterns/singleton/): Ensures a single instance of the pool is used, providing a global point of access.
174+
[Flyweight](https://java-design-patterns.com/patterns/flyweight/): Shares fine-grained objects to reduce memory usage, complementing object pooling by managing object state efficiently.
175+
[Factory Method](https://java-design-patterns.com/patterns/factory-method/): Often used to create objects within the pool, abstracting the instantiation process.
176+
177+
## Credits
178+
179+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
180+
* [Effective Java](https://amzn.to/4cGk2Jz)
181+
* [Java Concurrency in Practice](https://amzn.to/4aRMruW)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
*/
2525
package com.iluwatar.object.pool;
2626

27-
import java.util.concurrent.atomic.AtomicInteger;
27+
import java.util.concurrent.atomic.AtomicInteger;import lombok.extern.slf4j.Slf4j;
2828

2929
/**
3030
* Oliphaunts are expensive to create.
3131
*/
32+
@Slf4j
3233
public class Oliphaunt {
3334

3435
private static final AtomicInteger counter = new AtomicInteger(0);
@@ -43,7 +44,7 @@ public Oliphaunt() {
4344
try {
4445
Thread.sleep(1000);
4546
} catch (InterruptedException e) {
46-
e.printStackTrace();
47+
LOGGER.error("Error occurred: ", e);
4748
}
4849
}
4950

0 commit comments

Comments
 (0)