@ThreadSafe @DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class) public final class FsResourceAccountant extends Object
Closeable resources
(InputStream, OutputStream etc.) which are used in multiple
threads.
This class is only publicly accessible so that you can easily look up the
Javadoc for its member thread class FsResourceAccountant.Collector.
You cannot instantiate this class outside its package, however.
For synchronization, each accountant uses a lock which has to be provided
to its constructor.
In order to start accounting for a closeable resource,
call startAccountingFor(Closeable).
In order to stop accounting for a closeable resource,
call stopAccountingFor(Closeable).
FsResourceController| Modifier and Type | Class and Description |
|---|---|
private class |
FsResourceAccountant.Account
A reference to a
Closeable which can notify its
FsResourceAccountant. |
static class |
FsResourceAccountant.Collector
A high priority daemon thread which runs an endless loop in order to
collect account references which have been picked up by the garbage
collector and notify their respective resource accountant.
|
| Modifier and Type | Field and Description |
|---|---|
private Condition |
condition |
private Lock |
lock |
private static Logger |
logger |
private Map<Closeable,FsResourceAccountant.Account> |
threads
The pool of all accounted closeable resources.
|
| Constructor and Description |
|---|
FsResourceAccountant(Lock lock)
Constructs a new resource accountant with the given lock.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) <X extends Exception> |
closeAllResources(ExceptionHandler<? super IOException,X> handler)
For each accounted closeable resource,
stops accounting for it and closes it.
|
(package private) void |
startAccountingFor(Closeable resource)
Starts accounting for the given closeable resource.
|
(package private) void |
stopAccountingFor(Closeable resource)
Stops accounting for the given closeable resource.
|
private int |
threadLocalResources()
Returns the number of closeable resources which have been accounted for
in the current thread.
|
(package private) int |
waitOtherThreads(long timeout)
Waits until all closeable resources which have been started accounting
for in other threads get stopped accounting for or a timeout
occurs.
|
private final Condition condition
private final Lock lock
private static final Logger logger
@Nullable private volatile Map<Closeable,FsResourceAccountant.Account> threads
FsResourceAccountant(Lock lock)
waitOtherThreads(long) will not work as designed!lock - the lock to use for accounting resources.
Though not required by the use in this class, this
parameter should normally be an instance of
ReentrantLock because chances are that it gets
locked recursively.<X extends Exception> void closeAllResources(ExceptionHandler<? super IOException,X> handler) throws X extends Exception
Upon return of this method, threads may immediately start accounting for closeable resources again unless the caller also locks the lock provided to the constructor - use with care!
X extends Exceptionvoid startAccountingFor(Closeable resource)
resource - the closeable resource to start accounting for.void stopAccountingFor(Closeable resource)
Closeable.close() in the given closeable resource.resource - the closeable resource to stop accounting for.private int threadLocalResources()
lock is not locked!int waitOtherThreads(long timeout)
java.util.logging and control is returned to the caller.
Upon return of this method, threads may immediately start accounting for closeable resources again unless the caller also locks the lock provided to the constructor - use with care!
Mind that this method WILL NOT WORK if any two instances of this class share the same lock that has been provided to their constructor!
timeout - the number of milliseconds to await the closing of
resources which have been accounted for in other
threads.
If this is 0, then there is no timeout for waiting.Copyright © 2004-2011 Schlichtherle IT Services. All Rights Reserved.