From 788c41c33074d6b377dcee5a907e4a6c4b09298e Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Thu, 28 Apr 2011 20:05:48 +0100 Subject: [PATCH] Improved the Scheduler. Adds nag message when async tasks are not properly shut down and adds a limiter for sync tasks. Once they use 35ms in a single tick, any remaining tasks are not executed until later ticks. Adds a method to report the pending tasks and one to report active worker threads By: Raphfrk --- .../org/bukkit/scheduler/BukkitScheduler.java | 36 ++++++++++++++----- .../java/org/bukkit/scheduler/BukkitTask.java | 31 ++++++++++++++++ .../org/bukkit/scheduler/BukkitWorker.java | 35 ++++++++++++++++++ 3 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 paper-api/src/main/java/org/bukkit/scheduler/BukkitTask.java create mode 100644 paper-api/src/main/java/org/bukkit/scheduler/BukkitWorker.java diff --git a/paper-api/src/main/java/org/bukkit/scheduler/BukkitScheduler.java b/paper-api/src/main/java/org/bukkit/scheduler/BukkitScheduler.java index 67ec06f7af..26d252a4d2 100644 --- a/paper-api/src/main/java/org/bukkit/scheduler/BukkitScheduler.java +++ b/paper-api/src/main/java/org/bukkit/scheduler/BukkitScheduler.java @@ -3,6 +3,7 @@ package org.bukkit.scheduler; import org.bukkit.plugin.Plugin; import java.util.concurrent.Callable; import java.util.concurrent.Future; +import java.util.List; public interface BukkitScheduler { @@ -22,7 +23,7 @@ public interface BukkitScheduler { * This task will be executed by the main server thread * * @param Plugin Plugin that owns the task - * @param Runnable Task to be executed + * @param Runnable Task to be executed * @return int Task id number (-1 if scheduling failed) */ public int scheduleSyncDelayedTask(Plugin plugin, Runnable task); @@ -32,7 +33,7 @@ public interface BukkitScheduler { * This task will be executed by the main server thread * * @param Plugin Plugin that owns the task - * @param Runnable Task to be executed + * @param Runnable Task to be executed * @param long Delay in server ticks before executing first repeat * @param long Period in server ticks of the task * @return int Task id number (-1 if scheduling failed) @@ -106,27 +107,44 @@ public interface BukkitScheduler { /** * Check if the task currently running. - * + * * A repeating task might not be running currently, but will be running in the future. * A task that has finished, and does not repeat, will not be running ever again. - * + * * Explicitly, a task is running if there exists a thread for it, and that thread is alive. - * + * * @param taskId The task to check. - * + * * @return If the task is currently running. */ public boolean isCurrentlyRunning(int taskId); /** * Check if the task queued to be run later. - * + * * If a repeating task is currently running, it might not be queued now but could be in the future. * A task that is not queued, and not running, will not be queued again. - * + * * @param taskId The task to check. - * + * * @return If the task is queued to be run. */ public boolean isQueued(int taskId); + + /** + * Returns a list of all active workers. + * + * This list contains asynch tasks that are being executed by separate threads. + * + * @return Active workers + */ + public List getActiveWorkers(); + + /** + * Returns a list of all pending tasks. The ordering of the tasks is not related to their order of execution. + * + * @return Active workers + */ + public List getPendingTasks(); + } diff --git a/paper-api/src/main/java/org/bukkit/scheduler/BukkitTask.java b/paper-api/src/main/java/org/bukkit/scheduler/BukkitTask.java new file mode 100644 index 0000000000..f82514cf25 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/scheduler/BukkitTask.java @@ -0,0 +1,31 @@ +package org.bukkit.scheduler; + +import org.bukkit.plugin.Plugin; + +/** + * Represents a task being executed by the scheduler + */ + +public interface BukkitTask { + + /** + * Returns the taskId for the task + * + * @return Task id number + */ + public int getTaskId(); + + /** + * Returns the Plugin that owns this task + * + * @return The Plugin that owns the task + */ + public Plugin getOwner(); + + /** + * Returns true if the Task is a sync task + * + * @return true if the task is run by main thread + */ + public boolean isSync(); +} diff --git a/paper-api/src/main/java/org/bukkit/scheduler/BukkitWorker.java b/paper-api/src/main/java/org/bukkit/scheduler/BukkitWorker.java new file mode 100644 index 0000000000..b85a01cd5d --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/scheduler/BukkitWorker.java @@ -0,0 +1,35 @@ +package org.bukkit.scheduler; + +import org.bukkit.plugin.Plugin; + +/** + * Represents a worker thread for the scheduler. This gives information about + * the Thread object for the task, owner of the task and the taskId. + * + * Workers are used to execute async tasks. + */ + +public interface BukkitWorker { + + /** + * Returns the taskId for the task being executed by this worker + * + * @return Task id number + */ + public int getTaskId(); + + /** + * Returns the Plugin that owns this task + * + * @return The Plugin that owns the task + */ + public Plugin getOwner(); + + /** + * Returns the thread for the worker + * + * @return The Thread object for the worker + */ + public Thread getThread(); + +}