Class ResourceComputationScheduler<T>
java.lang.Object
org.eclipse.emf.compare.ide.ui.internal.logical.resolver.ResourceComputationScheduler<T>
- Type Parameters:
T
- type of keys used to keep track of currently ongoing computations.
Class providing facilities to schedule computations and hide as much as possible the multi-threaded
complexity. Each computation is identified by a key. Each computation is supposed to possibly load
resources, and the scheduler also provides a facility to unload resources that are no longer needed.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static enum
static enum
static class
-
Constructor Summary
ConstructorDescriptionConstructor, configured to wait for tasks completion for 5 seconds (will wait at most 10 seconds).ResourceComputationScheduler
(int shutdownWaitDuration, TimeUnit shutdownWaitUnit) Constructor.ResourceComputationScheduler
(int shutdownWaitDuration, TimeUnit shutdownWaitUnit, com.google.common.eventbus.EventBus eventBus) Constructor. -
Method Summary
Modifier and TypeMethodDescription<U> U
Executes the given callable as soon as possible.void
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 thecomputedKeys
variable.void
Ifshutdown has not been requested before
, it submits a new task toshut down
computingPool
andunloadingPool
.void
dispose()
Disposes this scheduler, which shuts down itsterminator
.com.google.common.collect.ImmutableSet<T>
Provides the set of keys of all the computations that have been run or are still running since its set of keyscomputedKeys
was last set.void
Initializes this scheduler, which instantiates itsterminator
.boolean
Indicates whether this scheduler is initialized, i.e. it can run computations.boolean
isScheduled
(T key) Evaluates whether a computation with the given key has been run or is still running since its set of keyscomputedKeys
was last set.void
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 thecomputedKeys
or in thecurrentlyComputing
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.
-
Constructor Details
-
ResourceComputationScheduler
public ResourceComputationScheduler()Constructor, configured to wait for tasks completion for 5 seconds (will wait at most 10 seconds). -
ResourceComputationScheduler
Constructor.- Parameters:
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.
-
ResourceComputationScheduler
public ResourceComputationScheduler(int shutdownWaitDuration, TimeUnit shutdownWaitUnit, com.google.common.eventbus.EventBus eventBus) Constructor.- Parameters:
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.eventBus
- TheEventBus
used to post events (shutdown events), can benull
-
-
Method Details
-
demandShutdown
public void demandShutdown()Ifshutdown has not been requested before
, it submits a new task toshut down
computingPool
andunloadingPool
. Do nothing if current thread already is interrupted. If a shutdown is actually started, events will be posted to the scheduler's eventBus if there is one. The events will be:- STARTED
- SUCCESS if shutdown has succeeded or FAILURE if shutdown has failed
-
initialize
public void initialize()Initializes this scheduler, which instantiates itsterminator
. Can be called several times with no problem. -
isInitialized
public boolean isInitialized()Indicates whether this scheduler is initialized, i.e. it can run computations.- Returns:
true
if and only if the scheduler is initialized.
-
dispose
public void dispose()Disposes this scheduler, which shuts down itsterminator
. -
call
Executes the given callable as soon as possible. Whatever happens, the given callback is run before returning (in a "finally" clause) and then the "notComputing" condition will be signalled and the lock released.Pre-conditions:
initialize()
has been calleddispose()
has not been called
- SETTING_UP
- SCHEDULED as soon as the set-up is finished
- FAILURE if and only if the given callable throws an exception
- FINISHING as soon as the given callable has finished running (successfully or not)
- FINISHED as soon as the tear-down is finished
- Type Parameters:
U
- the type of the return value.- Parameters:
callable
- will be executed as soon as this instance is no longer computing anything. Must not benull
.postTreatment
- will be called in a finally clause, whatever the outcome of the computation. Can benull
.- Returns:
- The result returned by the given callable execution.
-
computeAll
Schedules all the given computations, which will only be run if no computation for the same key is in thecomputedKeys
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.- Parameters:
computations
- An iterable over the computations to schedule.null
entries are silently ignored.
-
scheduleComputation
Schedules a given computation to be performed as soon as possible, if its key is not present in thecomputedKeys
or in thecurrentlyComputing
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)
, orrunAll(Iterable)
- Parameters:
computation
- The computation to run. Cannot benull
.- Returns:
true
if and only if the given key is not already among either the computed elements or the currently computing elements.
-
runAll
Executes all the runnables in the given iterable, and returns when all computations possibly running or launched by the given runnables are finished. This must be used when some treatment will possibly schedule computations but the exact list of computations cannot be computed beforehand.- Parameters:
runnables
- An iterable over the runnables to execute, must not benull
.null
entries are silently ignored.
-
scheduleUnload
public 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. This implementation uses a dedicated thread pool to perform these unloads.- Parameters:
runnable
- Runnable to run, must not benull
callback
- Callback to call upon completion, can benull
-
getComputedElements
Provides the set of keys of all the computations that have been run or are still running since its set of keyscomputedKeys
was last set.- Returns:
- 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.
-
isScheduled
Evaluates whether a computation with the given key has been run or is still running since its set of keyscomputedKeys
was last set.- Parameters:
key
- The key of the computation to check.- Returns:
- true, if a computation of the given key has been run or is still running.
-
clearComputedElements
public void clearComputedElements()Clears the set of computed keys. -
setComputedElements
Sets the computed keys with all the values in the given iterable.- Parameters:
elements
- An iterable over the elements to set as computed, must not benull
but can be empty.
-