This class collects AutoCloseable instances to be closed later. This class is useful when users need to keep track of unclosed resources and close them all later.
The closeables added to a closer will be closed in reverse order. I.e. the last one added will be closed first.
This class is thread-safe. Closeable can be added concurrently, and the class can be closed concurrently, multiple times.
For each closeable added to this class they will be closed exactly once when close() is called.
It is recommended that instances are used with try-with-resources statement.
public | Creates a new instance without any enclosed closeables. |
public | ResourceCloser( Creates a new instance initialized with the given closeables. |
public | ResourceCloser( Creates a new instance initialized with the given closeables. |
public void | add( Adds the closeables to this resource closer. |
public void | add( Adds the closeables to this resource closer. |
public void | Clears any closeables in this resource closer without actually closing the contained closeables. |
public void | close() Closes this stream and releases any system resources associated
with it.
|
As noted in AutoCloseable.close(), cases where the
close may fail require careful attention. It is strongly advised
to relinquish the underlying resources and to internally
mark the Closeable
as closed, prior to throwing
the IOException
.
Any exceptions thrown by the added closeables will be rethrown as IOException instances.