Class GracefulScheduledExecutor
-
- All Implemented Interfaces:
-
java.lang.AutoCloseable
public abstract class GracefulScheduledExecutor implements AutoCloseable
We use a GracefulScheduledExecutor because ScheduledExecutorService prevents the process from exiting.
The Java Virtual Machine runs until all threads that are not daemon threads have died. And Executors.defaultThreadFactory() creates each new thread as a non-daemon thread. However, there is an overload of Executors.newSingleThreadScheduledExecutor(); which takes a ThreadFactory as a parameter, if you care to venture in that direction.
-
-
Field Summary
Fields Modifier and Type Field Description private final BooleanisClosedprivate DurationinitialDelayprivate Durationintervalprivate final ScheduledExecutorServiceexecutor
-
Constructor Summary
Constructors Constructor Description GracefulScheduledExecutor(Duration initialDelay, Duration interval, ScheduledExecutorService executor)
-
Method Summary
Modifier and Type Method Description final BooleanisClosed()final DurationgetInitialDelay()final UnitsetInitialDelay(Duration initialDelay)final DurationgetInterval()final UnitsetInterval(Duration interval)final ScheduledExecutorServicegetExecutor()final Unitstart(Duration initialDelay, Duration period, Function0<Unit> runnable)Starts the monitor at the given period with the specific runnable action Visible only for testing final Unitstart(Duration initialDelay, Duration period)Unitstart(Long period, TimeUnit unit)Starts the reporter polling at the given period. Unitstart(Long initialDelay, Long period, TimeUnit unit, Runnable runnable)Starts the reporter polling at the given period with the specific runnable action. Unitstart(Long initialDelay, Long period, TimeUnit unit)Starts the reporter polling at the given period. final Unitstart()abstract Unitrun()Unitclose()-
-
Constructor Detail
-
GracefulScheduledExecutor
GracefulScheduledExecutor(Duration initialDelay, Duration interval, ScheduledExecutorService executor)
-
-
Method Detail
-
getInitialDelay
final Duration getInitialDelay()
-
setInitialDelay
final Unit setInitialDelay(Duration initialDelay)
-
getInterval
final Duration getInterval()
-
setInterval
final Unit setInterval(Duration interval)
-
getExecutor
final ScheduledExecutorService getExecutor()
-
start
final Unit start(Duration initialDelay, Duration period, Function0<Unit> runnable)
Starts the monitor at the given period with the specific runnable action Visible only for testing
-
start
Unit start(Long period, TimeUnit unit)
Starts the reporter polling at the given period.
- Parameters:
period- the amount of time between pollsunit- the unit forperiod
-
start
@Synchronized() Unit start(Long initialDelay, Long period, TimeUnit unit, Runnable runnable)
Starts the reporter polling at the given period with the specific runnable action. Visible only for testing.
-
start
@Synchronized() Unit start(Long initialDelay, Long period, TimeUnit unit)
Starts the reporter polling at the given period.
- Parameters:
initialDelay- the time to delay the first executionperiod- the amount of time between pollsunit- the unit forperiodandinitialDelay
-
-
-
-