The Object Pool
Object pools provide a method for sharing a limited number of resources among an unlimited number of users. The most prominent of these implementations is in the area of supporting database connections in JDBC-based applications. Relational database management systems limit the number of simultaneous users for performance, security, and system stability reasons. For a multithreaded server application, where an unlimited number of Internet users are trying to hit the database at the same time, it makes sense to maximize the number of simultaneous access to that database. By pooling database connections, a resource manager thread can perform round-robin assignments of the connections to the incoming requests and provide a queue for other requests to wait until a connection is available.
These connection pools may incorporate timeout, reconnection, and exception-handling logic that are not necessary for applications of an object pool where minimizing garbage collection is the objective. To minimize garbage collection, all you need is an array or vector to hold the object instances, and an indicator to the pool as to which resource is in use and which is free. Threads requesting an object from the pool can block itself, if necessary, on the array or vector object. When an object becomes available in the pool, it simply needs to make a notify() call. Each time a thread receives an object instance from the pool, the thread should explicitly initialize all the member variables of that instance.
-- T.J.
Copyright © 1999, Dr. Dobb's Journal