T
- type of keys used to keep track of currently ongoing computations.public class ResourceComputationScheduler<T> extends Object
Constructor and Description |
---|
ResourceComputationScheduler()
Constructor, configured to wait for tasks completion for 5 seconds (will wait at most 10 seconds).
|
ResourceComputationScheduler(int shutdownWaitDuration,
TimeUnit shutdownWaitUnit)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
<U> U |
call(Callable<U> callable,
Runnable postTreatment)
Executes the given callable as soon as possible.
|
void |
clearComputedElements()
Clears the set of computed keys.
|
void |
computeAll(Iterable<? extends IComputation<T>> computations)
Schedules all the given computations, which will only be run if no computation for the same key is in
the
computedKeys variable. |
void |
demandShutdown()
If
shutdown has not been requested before , it submits a new task to
shut down computingPool and unloadingPool . |
void |
dispose()
Disposes this scheduler, which shuts down its
terminator . |
com.google.common.collect.ImmutableSet<T> |
getComputedElements()
Provides the set of keys of all the computations that have been run or are still running since its set
of keys
computedKeys was last set. |
void |
initialize()
Initializes this scheduler, which instantiates its
terminator . |
boolean |
isInitialized()
Indicates whether this scheduler is initialized, i.e. it can run computations.
|
void |
runAll(Iterable<? extends Runnable> runnables)
Executes all the runnables in the given iterable, and returns when all computations possibly running or
launched by the given runnables are finished.
|
boolean |
scheduleComputation(IComputation<T> computation)
Schedules a given computation to be performed as soon as possible, if its key is not present in the
computedKeys or in the currentlyComputing keys, in which case the computation is
ignored. |
void |
scheduleUnload(Runnable runnable,
com.google.common.util.concurrent.FutureCallback<Object> callback)
Schedule a job that is suppoed to unload resource(s) that are no longer needed.
|
void |
setComputedElements(Iterable<T> elements)
Sets the computed keys with all the values in the given iterable.
|
public ResourceComputationScheduler()
public ResourceComputationScheduler(int shutdownWaitDuration, TimeUnit shutdownWaitUnit)
shutdownWaitDuration
- Time to wait for current tasks completion when shutting down the pools (will wait at most
twice this amount of time).shutdownWaitUnit
- Unit to use to interpret the other parameter.public void demandShutdown()
shutdown has not been requested before
, it submits a new task to
shut down
computingPool
and unloadingPool
. Do nothing if
current thread already is interrupted.public void initialize()
terminator
. Can be called several times
with no problem.public boolean isInitialized()
true
if and only if the scheduler is initialized.public void dispose()
terminator
.public <U> U call(Callable<U> callable, Runnable postTreatment) throws InterruptedException
Pre-conditions:
initialize()
has been calleddispose()
has not been calledU
- the type of the return value.callable
- will be executed as soon as this instance is no longer computing anything. Must not be
null
.postTreatment
- will be called in a finally clause, whatever the outcome of the computation. Can be
null
.InterruptedException
- If the computation is interruptedpublic void computeAll(Iterable<? extends IComputation<T>> computations)
computedKeys
variable. It is up to the caller to make sure that the semantics of
computations previously run is the same as thos they are submitting, otherwise computations completely
unrelated to what is being submitted may have marked a key as already computed. Returns after all the
currently running plus submitted computations have finished.computations
- An iterable over the computations to schedule. null
entries are silently ignored.public boolean scheduleComputation(IComputation<T> computation)
computedKeys
or in the currentlyComputing
keys, in which case the computation is
ignored. It is up to the caller to make sure that they submit homogeneous computations, in order for
the filtering of computations by key to be meaningful.
WARNING! In a multi-threaded execution, this method may return before the computation is run. It
is up to callers to make sure they only invoke that inside of a more general call to
call(Callable, Runnable)
, computeAll(Iterable)
, or runAll(Iterable)
computation
- The computation to run. Cannot be null
.true
if and only if the given key is not already among either the computed elements or
the currently computing elements.public void runAll(Iterable<? extends Runnable> runnables)
runnables
- An iterable over the runnables to execute, must not be null
. null
entries
are silently ignored.public void scheduleUnload(Runnable runnable, com.google.common.util.concurrent.FutureCallback<Object> callback)
runnable
- Runnable to run, must not be null
callback
- Callback to call upon completion, can be null
public com.google.common.collect.ImmutableSet<T> getComputedElements()
computedKeys
was last set.computedKeys
was last set.public void clearComputedElements()
Copyright (c) 2006, 2014 Obeo and others. All rights reserved.