In this post, we will take a look at how we can create an object pool in Java. In recent years, the performance of the JVM has multiplied manifold that object pooling for better performance has been made almost redundant for most types of objects. In essence, the creation of objects is no longer considered as expensive as it was before.
However, there are some kinds of objects that certainly prove costly upon creation. Objects such as Threads, database connection objects etc are not lightweight objects and are slightly more expensive to create. In any application, we require the use of multiple objects of the above kind. So it would be great if there was an easy way to create and maintain an object pool of that type so that objects can be dynamically used and reused without the client code being bothered about the live cycle of the objects.