Chapter 5. Pool


Not all Java objects are created equal. For example, some take considerably longer to create than othersan object that establishes a secure network connection can take several seconds to properly initialize. Other objects might consume significant resources. In order to minimize the creation of these objects, you may want to maintain a pool of objectsa little bit of bookkeeping to keep track of a group of objects, checked out and returned as needed. The Apache Jakarta Commons Pool package provides interfaces and implementation to make that task much easier.

Although some people might think that an object pool is easy to write, by using the Pool package, a wide suite of built-in, well-tested behaviors is provided for free. For example, you can use the Commons package to create a pool that will only allocate a maximum of ten objects and then block on future allocations until an object is returned or create up to fifty objects, with the pool automatically shrinking when the garbage collector needs the memory. By implementing a single interface and using the right combination of Apache Pool interfaces and implementations, you can configure a wide suite of pools, suitable for a broad range of uses.

Avoid Preoptimization

Maintaining an object pool simply to minimize the overhead of JVM-level object creation is generally not worth the bother. Modern JVM implementations are quite good at optimizing object creation. If the objects are expensive to create, however, it may be worth creating a pool.


It's worth noting that one of the most popular uses of a pool is to manage database connectivity. This is popular enough to warrant a specific Commons package, DBCP, which is based on the Pool suite described in this chapter. DBCP is described in the next chapter. In addition, Java 5.0 (also known as JDK 1.5) includes built-in support for ThreadPools and other useful operations, as described at http://java.sun.com/j2se/1.5.0/docs/guide/concurrency/index.html.



    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net