Implements a thread safe pool for arbitrary objects.
The basic principles of a pool is to create, hold, provide and possibly discard objects.
Pools are meant to be used for object instances that are expensive to create, such as Sockets, Parsers, Validators.
Pretty much anything related to I/O or mechanisms needing to read up data from disc is expensive to instantiate.
So why discard the instance if it can be used mulitple times? But what if your code needs more than one instance to function?
This is where the pool comes into play.
Features such as:
- Holds any arbitrary objects
Your factory decides what type is shall hold - Bounded size.
You determine the maximum size of the pool - Validation of instances returned to the pool (Optional).
Instances failing the validation will be discarded and destroyed - Destruction of idle instances (Optional)
Configure a maximum time instances are allowed to sit idle before they are destroyed - LIFO or FIFO mode
Choose how instances are picked from the pool. Last-In-First-Out or First-In-First-Out
There are numerous of pool implementations out there.
Heck I've written several of my own over the years.
This one is meant to be what it's called, simple.
Not a zillion of options creating complexity.
Not a huge amount of code (unknown to you).
Minimum amount of synchronized points, the bare minimum to be able to be thread safe.
Written in and for Java 8 relying on Lambda expressions for neatness.
The Factory is the starting point. It's used to create the Pool.
The factory works according to a builder pattern where one starts by creating the factory poolFor and finishes by invoking create.
In between those invocations one may add optional behavior, such as the ofSize (size) of the pool.
Let's illustrate the creation of the factory with a few simple examples.
The example below creates a pool factory for sockets.
Factory<Socket> factory = Factory.poolFor(() -> new Socket("127.0.0.1", 6969));
factory = factory.ofSize(20);
factory = factory.withValidator(socket -> socket.isConnected());
factory = factory.withDestructor(socket -> {
try {
socket.close();
}
catch(Exception ex){}
});
One can of course write it all in a single statements:
Factory<Socket> factory = Factory.poolFor(() -> new Socket("127.0.0.1", 6969)).ofSize(10).withValidator(s -> s.isConnected()).withDestructor(s -> {
try {
s.close();
} catch (Exception ex) {
}
});
The Pool is the holder of your instances.
Once you have your Factory you create pool instances using the create method.
Pool<Socket> pool = factory.create();
Copyright 2016 Peter Nerg.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.