See: Description
Interface | Description |
---|---|
Poolable |
An interface automatically implemented by the proxy instances returned from an
Pool . |
Class | Description |
---|---|
Pool |
A simple pool implementation that collects its unused components of a specific type automatically.
|
Pool.PoolingInvoker |
The
Invoker of the proxy. |
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:
Pool pool = new Pool(Checksum.class, new Resetter(){ public boolean reset(final Object object) { ((Checksum)object).reset(); return true; }}); pool.add(new CRC32()); if (true) { Checksum checksum = (Checksum)pool.get(); checksum.update("JUnit".getBytes(), 0, 5); System.out.println("CRC32 checksum of \"JUnit\": " + checksum.getValue()); } if (true) { Checksum checksum = (Checksum)pool.get(); if (checksum == null) { System.out.println("No checksum available, force gc ..."); System.gc(); } checksum = (Checksum)pool.get(); System.out.println("CRC32 of an resetted checksum: " + checksum.getValue()); ((Poolable)checksum).returnInstanceToPool(); }