public class Scheduler extends Object implements Closeable
ScheduledExecutorService
with core pool size 1. The behaviour
of this underlying scheduler service determines the semantics of the methods
in this class. Namely: Execution of background tasks never overlaps and is
FIFO for tasks scheduled for the same time.
In addition all tasks scheduled through methods of this class are automatically
wrapped into SafeRunnable
instances. The background thread executing
submitted tasks is a deamon thread.Constructor and Description |
---|
Scheduler(@Nullable String name)
Create a new instance with the given
name . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Close this scheduler.
|
void |
execute(@NotNull String name,
@NotNull Runnable task)
Immediately execute
task . |
void |
scheduleAtFixedRate(@NotNull String name,
long period,
@NotNull TimeUnit unit,
@NotNull Runnable task)
Run
task regularly at a given interval. |
void |
scheduleOnce(@NotNull String name,
long delay,
@NotNull TimeUnit unit,
@NotNull Runnable task)
Run
task once after some delay. |
void |
scheduleWithFixedDelay(@NotNull String name,
long delay,
@NotNull TimeUnit unit,
@NotNull Runnable task)
Run
task regularly after a fixed delay. |
public Scheduler(@Nullable @Nullable String name)
name
. The name is used to
derive the default name of the background thread from..name
- public void execute(@NotNull @NotNull String name, @NotNull @NotNull Runnable task)
task
. The background thread's name is
set to name
during execution of task
.name
- task
- Executor.execute(Runnable)
public void scheduleOnce(@NotNull @NotNull String name, long delay, @NotNull @NotNull TimeUnit unit, @NotNull @NotNull Runnable task)
task
once after some delay. The background thread's name is
set to name
during execution of task
.name
- delay
- unit
- task
- ScheduledExecutorService.schedule(Runnable, long, TimeUnit)
public void scheduleAtFixedRate(@NotNull @NotNull String name, long period, @NotNull @NotNull TimeUnit unit, @NotNull @NotNull Runnable task)
task
regularly at a given interval. The background thread's name is
set to name
during execution of task
.name
- period
- unit
- task
- ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
public void scheduleWithFixedDelay(@NotNull @NotNull String name, long delay, @NotNull @NotNull TimeUnit unit, @NotNull @NotNull Runnable task)
task
regularly after a fixed delay. The background thread's name is
set to name
during execution of task
.name
- delay
- unit
- task
- ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
public void close()
close
in interface Closeable
close
in interface AutoCloseable
ExecutorService.shutdown()
Copyright © 2012–2020 The Apache Software Foundation. All rights reserved.