Class ScheduledExecutors
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ScheduledExecutorFactorycreateFactory(Lifecycle lifecycle) static ScheduledExecutorServiceemittingDelayMetric(ScheduledExecutorService exec, ServiceEmitter emitter, String metricName, Map<String, Object> metricDimensions) Wraps aScheduledExecutorServiceto emit a metric each time a task fromScheduledExecutorService.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit)runs.static ScheduledExecutorServiceCreates a newScheduledExecutorServicewith a minimum number of threads.static ScheduledThreadPoolExecutorfixedWithKeepAliveTime(int corePoolSize, String nameFormat, long keepAliveTimeInMillis) Creates a newScheduledExecutorServicewith a minimum number of threads along with a keep-alive time for idle non-core threads.static voidscheduleAtFixedRate(ScheduledExecutorService exec, org.joda.time.Duration rate, Callable<ScheduledExecutors.Signal> callable) static voidscheduleAtFixedRate(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration period, Runnable runnable) Schedules a runnable to execute repeatedly at a fixed rate.static voidscheduleAtFixedRate(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration rate, Callable<ScheduledExecutors.Signal> callable) static voidscheduleWithFixedDelay(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration delay, Runnable runnable) Run runnable repeatedly with the given delay between calls, after the given initial delay.static voidscheduleWithFixedDelay(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration delay, Callable<ScheduledExecutors.Signal> callable) Run callable repeatedly with the given delay between calls, until it returns Signal.STOP or the executor is shut down.
-
Constructor Details
-
ScheduledExecutors
public ScheduledExecutors()
-
-
Method Details
-
scheduleWithFixedDelay
public static void scheduleWithFixedDelay(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration delay, Runnable runnable) Run runnable repeatedly with the given delay between calls, after the given initial delay. Exceptions are caught and logged as errors. -
scheduleWithFixedDelay
public static void scheduleWithFixedDelay(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration delay, Callable<ScheduledExecutors.Signal> callable) Run callable repeatedly with the given delay between calls, until it returns Signal.STOP or the executor is shut down. Exceptions are caught and logged as errors, and do not prevent subsequent executions. -
scheduleAtFixedRate
public static void scheduleAtFixedRate(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration period, Runnable runnable) Schedules a runnable to execute repeatedly at a fixed rate. The first execution occurs after the initial delay, and subsequent executions are scheduled at fixed intervals measured from the start of each execution.This differs from
scheduleWithFixedDelay(java.util.concurrent.ScheduledExecutorService, org.joda.time.Duration, org.joda.time.Duration, java.lang.Runnable)in that the period is measured from the start of each execution rather than from the completion. If an execution takes longer than the period, the next execution will begin immediately after the current one completes.This also differs from
ScheduledExecutorService.scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)in that it prevents task pileup: only one future execution is scheduled at a time rather than scheduling all future executions upfront. This prevents a backlog of pending tasks from building up if the executor is delayed or tasks run slowly.Exceptions thrown by the task are caught and logged as errors, and do not prevent subsequent executions. Scheduling also stops if the executor is shut down.
- Parameters:
exec- the ScheduledExecutorService to use for schedulinginitialDelay- the duration to wait before the first executionperiod- the target duration between the start of consecutive executionsrunnable- the task to execute repeatedly
-
scheduleAtFixedRate
public static void scheduleAtFixedRate(ScheduledExecutorService exec, org.joda.time.Duration rate, Callable<ScheduledExecutors.Signal> callable) -
scheduleAtFixedRate
public static void scheduleAtFixedRate(ScheduledExecutorService exec, org.joda.time.Duration initialDelay, org.joda.time.Duration rate, Callable<ScheduledExecutors.Signal> callable) -
emittingDelayMetric
public static ScheduledExecutorService emittingDelayMetric(ScheduledExecutorService exec, ServiceEmitter emitter, String metricName, Map<String, Object> metricDimensions) Wraps aScheduledExecutorServiceto emit a metric each time a task fromScheduledExecutorService.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit)runs. The metric value is the scheduling lag: the difference between the actual delay and the intended delay, in milliseconds, floored at zero.- Parameters:
exec- the executor to wrapemitter- the emitter to emit metrics tometricName- the name of the metric to emitmetricDimensions- dimensions to include with the metric
-
createFactory
-
fixed
Creates a newScheduledExecutorServicewith a minimum number of threads.- Parameters:
corePoolSize- the minimum number of threads in the poolnameFormat- the naming format for threads created by the pool- Returns:
- a new
ScheduledExecutorServicewith the specified configuration
-
fixedWithKeepAliveTime
public static ScheduledThreadPoolExecutor fixedWithKeepAliveTime(int corePoolSize, String nameFormat, long keepAliveTimeInMillis) Creates a newScheduledExecutorServicewith a minimum number of threads along with a keep-alive time for idle non-core threads.
-