Package io.github.classgraph.utils
Class Recycler<T extends Closeable,E extends Exception>
- java.lang.Object
-
- io.github.classgraph.utils.Recycler<T,E>
-
- Type Parameters:
T
- The type to recycle.E
- An exception type that can be thrown while acquiring an instance of the type to recycle.
- All Implemented Interfaces:
AutoCloseable
public abstract class Recycler<T extends Closeable,E extends Exception> extends Object implements AutoCloseable
Recycle instances of type T. The method T#close() is called when this class' own close() method is called. Use RuntimeException for type E if the newInstance() method does not throw an exception. Example usage:// Autoclose the Recycler when last instance has been released try (Recycler<ZipFile, IOException> recycler = new Recycler<>() { @Override public ZipFile newInstance() throws IOException { return new ZipFile(zipFilePath); } }) { // Repeat the following as many times as needed, on as many threads as needed try { ZipFile zipFile = recycler.acquire(); try { // Read from zipFile -- don't put recycler.acquire() in this try block, otherwise the // finally block will try to release the zipfile even when recycler.acquire() failed // [...] } finally { recycler.release(zipFile); zipFile = null; } } catch (IOException e) { // May be thrown by recycler.acquire() } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
Recycler.Recyclable
An AutoCloseable wrapper for a recyclable object instance.
-
Constructor Summary
Constructors Constructor Description Recycler()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Recycler.Recyclable
acquire()
Acquire a Recyclable wrapper around an object instance.void
close()
Calls close() on all the unused instances.abstract T
newInstance()
Create a new instance.
-
-
-
Method Detail
-
acquire
public Recycler.Recyclable acquire() throws E extends Exception
Acquire a Recyclable wrapper around an object instance. Use in try-with-resources.
-
close
public void close()
Calls close() on all the unused instances. May be called multiple times, ifacquire()
is called again afterclose()
.- Specified by:
close
in interfaceAutoCloseable
-
-