1package com.runehive.game.task;
3import com.google.common.base.Preconditions;
4import org.apache.logging.log4j.LogManager;
5import org.apache.logging.log4j.Logger;
7import java.util.ArrayList;
9import java.util.ListIterator;
10import java.util.Queue;
11import java.util.concurrent.ConcurrentLinkedQueue;
30 private final Queue<Task>
pending =
new ConcurrentLinkedQueue<>();
36 private final List<Task>
active =
new ArrayList<>();
52 Preconditions.checkNotNull(
task);
55 if (!
task.canSchedule()) {
59 task.setRunning(
true);
62 if (
task.isInstant()) {
65 }
catch (Exception ex) {
66 logger.warn(String.format(
"error executing task: %s",
task.getClass().getSimpleName()), ex);
71 }
catch (Exception ex) {
72 logger.error(String.format(
"error scheduling task: %s",
task.getClass().getSimpleName()), ex);
81 while((t =
pending.poll()) !=
null) {
85 for (
final ListIterator<Task> itr =
active.listIterator(); itr.hasNext();) {
90 task.setExecutionTime();
92 }
catch (Exception ex) {
93 logger.warn(String.format(
"Error executing task: %s [pendingQueue: %d executionQueue: %d], ",
task.getClass().getSimpleName(),
pending.size(),
active.size()), ex);
97 if (!
task.isRunning()) {
110 cancel(attachment,
false);
122 public void cancel(Object attachment,
boolean logout) {
124 if (attachment.equals(
task.getAttachment().orElse(
null))) {
A game representing a cyclic unit of work.
The class that handles scheduling tasks, and processing them.
final List< Task > active
The list of tasks that are currently running.
synchronized void processTasks()
This method handles adding new tasks and processing of active tasks.
final Queue< Task > pending
The queue of tasks that are waiting to be executed.
void cancel(Object attachment, boolean logout)
Cancels a task that has a specific attachment.
static final Logger logger
The single logger for this class.
synchronized void schedule(Task task)
Schedules a Task to be ran in the future.
void cancel(Object attachment)
Cancels a task that has a specific attachment.