Building a Simple Java Object Pool with LinkedBlockingQueue and Factory Interface
This article describes how to build a lightweight custom object pool in Java using a LinkedBlockingQueue and a factory interface, detailing its design, implementation, code examples, and a test script that demonstrates borrowing, returning, and size control of pooled objects.
After studying commons-pool2, the author found it too heavyweight for simple scenarios and decided to implement a lightweight custom object pool.
The design follows the ideas of commons-pool2: use a queue to store cached objects, provide methods to borrow and return objects, and offer optional APIs to control pool size without enforcing them.
The implementation leverages Java's LinkedBlockingQueue for thread‑safe storage and a factory interface to create new instances, similar to Go's sync.Pool approach.
Code
<code style="padding: 16px; color: #abb2bf; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px">package com.funtester.funpool
import java.util.concurrent.LinkedBlockingQueue
/**
* Object pool, using LinkedBlockingQueue to store objects, and using factory to create new objects, and using offer to return objects, and using poll to borrow objects, and using trimQueue to trim queue size
* @param <F>
*/
class FunPool<F> {
/**
* Factory to create new objects
*/
FunPooledFactory<F> factory
/**
* Object pool, using LinkedBlockingQueue to store objects
*/
LinkedBlockingQueue<F> pool = new LinkedBlockingQueue<F>()
FunPool(FunPooledFactory<F> factory) {
this.factory = factory
}
/**
* Borrow an object from the pool
*
* @param o the object to be borrowed
* @return
*/
F borrow() {
F f = pool.poll()
if (f == null) {
f = factory.newInstance()
}
return f
}
/**
* Return the object to the pool
*
* @param f the object to be returned
*/
def back(F f) {
boolean offer = pool.offer(f)
if (!offer) f = null
}
/**
* return size of object pool
*
* @return
*/
int size() {
return pool.size()
}
/**
* Trim the queue size
* @param size the size to be trimmed
*/
def trimQueue(int size) {
while (true) {
if (size() <= size) break
pool.poll()
}
}
}
</code>The accompanying factory interface is defined as:
<code style="padding: 16px; color: #abb2bf; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px">package com.funtester.funpool
/**
* Factory to create new objects
* @param <F>
*/
interface FunPooledFactory<F> {
/**
* Create new objects
* @return
*/
F newInstance()
}
</code>The code walkthrough explains the core concepts: object‑pool design pattern, use of the factory pattern for flexible object creation, thread‑safety via LinkedBlockingQueue, borrowing and returning mechanisms, and queue size control through trimQueue().
Test
A test script creates a pool of Demo objects, borrows and returns them, and prints the pool size, demonstrating that the same object is reused after being returned.
<code style="padding: 16px; color: #abb2bf; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px">static void main(String[] args) {
def pool = new FunPool<Demo>(new FunPooledFactory<Demo>() {
@Override
Demo newInstance() {
return new Demo()
}
})
def first = pool.borrow()
println first.hashCode()
2.times {
def borrow = pool.borrow()
println borrow.hashCode()
pool.back(borrow)
}
output(pool.size())
}
/**
* A simple class
*/
static class Demo {
}
</code>The console output shows different hash codes for the first borrow and the subsequent two borrows, confirming that the pool reuses objects correctly.
Overall, this article provides a concise yet functional example of an object pool that can improve performance and resource utilization in Java applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
