@ThreadSafe public class TridentTaskScheduler extends com.google.common.collect.ForwardingCollection<ScheduledTask> implements Scheduler
The scheduler is designed to stage-heavy/run-light philosophy: most overhead in the scheduler is to the run staging, which adds the ScheduledTask to the queue, and constructs the wrapper which assigns the ScheduledTask executor and constructs the logic runnables. In contrast, running the wrapper would perform the pre-constructed logic and mark the ScheduledTask then move on. This ensures that the ScheduledTask will be delayed preferable when it is scheduled, instead of when it will be planned to run.
Logic of ScheduledTask types:
The benchmarks and testing units for the TridentTaskScheduler can be found at: http://git.io/nifjcg.
Insertion logic places the ScheduledTask wrapped by the implementation of ScheduledTask
to perform the run logic and scheduling decisions plus automatic
ScheduledTask cancellation. Then, the overriden runnable with the ScheduledTask to be run is ScheduledRunnable.markSchedule(net.tridentsdk.concurrent.ScheduledTask)
ed to indicate the
ScheduledTask delegate is available.
Thread safety is ensured a single iteration thread, the tick thread. Tasks added first put in the task list, then the task is marked. The execution has a higher priority over the access to the task scheduling period. Also, most tasks will be allowed to complete before any change is needed. Task execution occurs in a single thread, the tick method adds to an executor which does not share the state of the task implementation.
The scheduler is high performance due to lock-free execution. The internal task list is a
ConcurrentLinkedQueue
, iterated in the tick method which schedules a runnable assigned
to the task during construction. The most overhead occurs when the runnable is scheduled, and when the logic for
the scheduling method is decided during the task wrapper's construction.
Modifier and Type | Method and Description |
---|---|
ScheduledTask |
asyncLater(Plugin plugin,
ScheduledRunnable runnable,
long delay)
Asynchronously run a task after a specified time has passed
|
ScheduledTask |
asyncRepeat(Plugin plugin,
ScheduledRunnable runnable,
long delay,
long initialInterval)
Asynchronously run a task repeatedly
|
ScheduledTask |
asyncRun(Plugin plugin,
ScheduledRunnable runnable)
Asynchronously run a task after the next tick
|
static TridentTaskScheduler |
create()
Creates a new scheduler
|
protected Collection<ScheduledTask> |
delegate() |
void |
shutdown() |
ScheduledTask |
syncLater(Plugin plugin,
ScheduledRunnable runnable,
long delay)
Asynchronously run a task after a specified time has passed
|
ScheduledTask |
syncRepeat(Plugin plugin,
ScheduledRunnable runnable,
long delay,
long initialInterval)
Synchronously run a task repeatedly
|
ScheduledTask |
syncRun(Plugin plugin,
ScheduledRunnable runnable)
Synchronously run a task after the next tick
|
void |
tick() |
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, standardAddAll, standardClear, standardContains, standardContainsAll, standardIsEmpty, standardRemove, standardRemoveAll, standardRetainAll, standardToArray, standardToArray, standardToString, toArray, toArray
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray
public static TridentTaskScheduler create()
public void tick()
public void shutdown()
public ScheduledTask asyncRun(Plugin plugin, ScheduledRunnable runnable)
Scheduler
public ScheduledTask syncRun(Plugin plugin, ScheduledRunnable runnable)
Scheduler
public ScheduledTask asyncLater(Plugin plugin, ScheduledRunnable runnable, long delay)
Scheduler
asyncLater
in interface Scheduler
plugin
- the plugin which the task is registered torunnable
- the runnable to perform the taskdelay
- the amount of ticks to wait until the task is executedpublic ScheduledTask syncLater(Plugin plugin, ScheduledRunnable runnable, long delay)
Scheduler
public ScheduledTask asyncRepeat(Plugin plugin, ScheduledRunnable runnable, long delay, long initialInterval)
Scheduler
asyncRepeat
in interface Scheduler
plugin
- the plugin which the task is registered torunnable
- the runnable to perform the taskdelay
- the amount of ticks to wait until the task is executedinitialInterval
- the amount of time between each execution which occurs repeatedly until cancelledpublic ScheduledTask syncRepeat(Plugin plugin, ScheduledRunnable runnable, long delay, long initialInterval)
Scheduler
syncRepeat
in interface Scheduler
plugin
- the plugin which the task is registered torunnable
- the runnable to perform the taskdelay
- the amount of ticks to wait until the task is executedinitialInterval
- the amount of time between each execution which occurs repeatedly until cancelledprotected Collection<ScheduledTask> delegate()
delegate
in class com.google.common.collect.ForwardingCollection<ScheduledTask>
Copyright © 2016. All rights reserved.