public final class ComputationScheduler extends Scheduler implements SchedulerMultiWorkerSupport
Scheduler.Worker
SchedulerMultiWorkerSupport.WorkerCallback
Constructor and Description |
---|
ComputationScheduler()
Create a scheduler with pool size equal to the available processor
count and using least-recent worker selection policy.
|
ComputationScheduler(ThreadFactory threadFactory)
Create a scheduler with pool size equal to the available processor
count and using least-recent worker selection policy.
|
Modifier and Type | Method and Description |
---|---|
Scheduler.Worker |
createWorker()
Retrieves or creates a new
Scheduler.Worker that represents sequential execution of actions. |
void |
createWorkers(int number,
SchedulerMultiWorkerSupport.WorkerCallback callback)
Creates the given number of
Scheduler.Worker instances
that are possibly backed by distinct threads
and calls the specified Consumer with them. |
Disposable |
scheduleDirect(Runnable run,
long delay,
TimeUnit unit)
Schedules the execution of the given task with the given time delay.
|
Disposable |
schedulePeriodicallyDirect(Runnable run,
long initialDelay,
long period,
TimeUnit unit)
Schedules a periodic execution of the given task with the given initial time delay and repeat period.
|
void |
shutdown()
Instructs the Scheduler instance to stop threads,
stop accepting tasks on any outstanding
Scheduler.Worker instances
and clean up any associated resources with this Scheduler. |
void |
start()
Allows the Scheduler instance to start threads
and accept tasks on them.
|
clockDriftTolerance, now, scheduleDirect, when
public ComputationScheduler()
public ComputationScheduler(ThreadFactory threadFactory)
threadFactory
- thread factory to use for creating worker threads. Note that this takes precedence over any
system properties for configuring new thread creation. Cannot be null.@NonNull public Scheduler.Worker createWorker()
Scheduler
Scheduler.Worker
that represents sequential execution of actions.
When work is completed, the Worker
instance should be released
by calling Disposable.dispose()
to avoid potential resource leaks in the
underlying task-execution scheme.
Work on a Scheduler.Worker
is guaranteed to be sequential and non-overlapping.
createWorker
in class Scheduler
public void createWorkers(int number, SchedulerMultiWorkerSupport.WorkerCallback callback)
SchedulerMultiWorkerSupport
Scheduler.Worker
instances
that are possibly backed by distinct threads
and calls the specified Consumer
with them.createWorkers
in interface SchedulerMultiWorkerSupport
number
- the number of workers to create, positivecallback
- the callback to send worker instances to@NonNull public Disposable scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit)
Scheduler
This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.
scheduleDirect
in class Scheduler
run
- the task to scheduledelay
- the delay amount, non-positive values indicate non-delayed schedulingunit
- the unit of measure of the delay amount@NonNull public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit)
Scheduler
This method is safe to be called from multiple threads but there are no ordering guarantees between tasks.
The periodic execution is at a fixed rate, that is, the first execution will be after the
initialDelay
, the second after initialDelay + period
, the third after
initialDelay + 2 * period
, and so on.
schedulePeriodicallyDirect
in class Scheduler
run
- the task to scheduleinitialDelay
- the initial delay amount, non-positive values indicate non-delayed schedulingperiod
- the period at which the task should be re-executedunit
- the unit of measure of the delay amountpublic void start()
Scheduler
Implementations should make sure the call is idempotent, thread-safe and
should not throw any RuntimeException
if it doesn't support this
functionality.
public void shutdown()
Scheduler
Scheduler.Worker
instances
and clean up any associated resources with this Scheduler.
Implementations should make sure the call is idempotent, thread-safe and
should not throw any RuntimeException
if it doesn't support this
functionality.
Copyright © 2019. All rights reserved.