@ThreadSafe final class FsResourceAccountant extends Object
Closeable resources
(InputStream, OutputStream etc.) which are used in multiple
threads.
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 |
| Modifier and Type | Field and Description |
|---|---|
private static ConcurrentMap<Closeable,FsResourceAccountant.Account> |
accounts
The pool of all accounted closeable resources.
|
private Condition |
condition |
private static int |
INITIAL_CAPACITY
The initial capacity for the hash map accounts for the number of
available processors, a 90% blocking factor for typical I/O and a 2/3
map resize threshold.
|
private Lock |
lock |
| 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) int |
localResources()
Returns the number of closeable resources which have been accounted for
by the current thread.
|
(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.
|
(package private) int |
totalResources()
Returns the number of closeable resources which have been accounted for
by all threads.
|
(package private) int |
waitForeignResources(long timeout)
Waits until all closeable resources which have been started accounting
for by other threads get stopped accounting for or a timeout
occurs or the current thread gets interrupted, whatever happens first.
|
private static final ConcurrentMap<Closeable,FsResourceAccountant.Account> accounts
private final Condition condition
private static final int INITIAL_CAPACITY
private final Lock lock
FsResourceAccountant(Lock lock)
waitForeignResources(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 Exceptionint localResources()
This method must not get called if the lock is not
acquired!
void startAccountingFor(@WillCloseWhenClosed Closeable resource)
resource - the closeable resource to start accounting for.void stopAccountingFor(@WillNotClose Closeable resource)
Closeable.close() in the given closeable resource.resource - the closeable resource to stop accounting for.int totalResources()
This method must not get called if the lock is not
acquired!
int waitForeignResources(long timeout)
Waiting for such resources can get cancelled immediately by interrupting the current thread. Unless the number of closeable resources which have been accounted for by all threads is zero, this will leave the interrupt status of the current thread cleared. If no such foreign resources exist, then interrupting the current thread does not have any effect.
Upon return of this method, threads may immediately start accounting for closeable resources again unless the caller has acquired the lock provided to the constructor - use with care!
Note that this method WILL NOT WORK if any two instances of this class share the same lock provided to their constructor!
timeout - the number of milliseconds to await the closing of
resources which have been accounted for by other
threads once the lock has been acquired.
If this is non-positive, then there is no timeout for waiting.Copyright © 2005-2012 Schlichtherle IT Services. All Rights Reserved.