Skip navigation links

Package com.thoughtworks.proxy.toys.pool

A toy to create object pools based on proxies.

See: Description

Package com.thoughtworks.proxy.toys.pool Description

A toy to create object pools based on proxies.

The package provides a basic object pool based on proxies, that enable the return of their pooled object into the pool, when the proxy gets out of scope and it is garbage collected. Main component is the Pool toy that manages the objects of a single pool. Any object retrieved from the pool is wrapped by such a pool proxy. The proxy also implements Poolable, that can be used to return the object manually.

In the following example demonstrates the usage of the pool. The pooled object is once returned by the garbage collector and one time manually. See also the automated reset of the element's status:

Resetter<Checksum>() {
    public boolean reset(Checksum object) {
        object.reset();
        return true;
    }
};
Pool<Checksum> pool = Pool.create(Checksum.class).resettedBy(resetter).build(new CglibProxyFactory());
pool.add(new CRC32());
{
    Checksum checksum = pool.get();
    checksum.update("JUnit".getBytes(), 0, 5);
    System.out.println("CRC32 checksum of \"JUnit\": " + checksum.getValue());
}
{
    Checksum checksum = pool.get();
    if (checksum == null) {
        System.out.println("No checksum available, force gc ...");
        System.gc();
    }
    checksum = pool.get();
    System.out.println("CRC32 of an resetted checksum: " + checksum.getValue());
    Poolable.class.cast(checksum).returnInstanceToPool();
}
Skip navigation links

Copyright © 2005–2024 Codehaus. All rights reserved.