RuneHive-Game
Loading...
Searching...
No Matches
TaskManager.java
Go to the documentation of this file.
1package com.runehive.game.task;
2
3import com.google.common.base.Preconditions;
4import org.apache.logging.log4j.LogManager;
5import org.apache.logging.log4j.Logger;
6
7import java.util.ArrayList;
8import java.util.List;
9import java.util.ListIterator;
10import java.util.Queue;
11import java.util.concurrent.ConcurrentLinkedQueue;
12
13/**
14 * The class that handles scheduling tasks, and processing them.
15 *
16 * @author nshusa
17 */
18public final class TaskManager {
19
20 /**
21 * The single logger for this class.
22 */
23 private static final Logger logger = LogManager.getLogger(TaskManager.class);
24
25 /**
26 * The queue of tasks that are waiting to be executed.
27 * A {@link ConcurrentLinkedQueue} is used here so tasks
28 * can be added safely from multiple threads.
29 */
30 private final Queue<Task> pending = new ConcurrentLinkedQueue<>();
31
32 /**
33 * The list of tasks that are currently running.
34 * A {@link ArrayList} is used here for very fast iteration speed.
35 */
36 private final List<Task> active = new ArrayList<>();
37
38 /**
39 * Schedules a {@link Task} to be ran in the future.
40 *
41 * @param task
42 * The task to schedule.
43 */
44 public synchronized void schedule(Task task) {
45 /*StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
46 System.out.println("Displaying Stack trace using StackTraceElement in Java");
47 String myString = "";
48 for(StackTraceElement st : stackTrace){
49 myString = myString + st + '\n';
50 }
51 LogManager.getLogger().info(myString);*/
52 Preconditions.checkNotNull(task);
53
54 try {
55 if (!task.canSchedule()) {
56 return;
57 }
58
59 task.setRunning(true);
60 task.onSchedule();
61
62 if (task.isInstant()) {
63 try {
64 task.baseExecute();
65 } catch (Exception ex) {
66 logger.warn(String.format("error executing task: %s", task.getClass().getSimpleName()), ex);
67 return;
68 }
69 }
70 pending.add(task);
71 } catch (Exception ex) {
72 logger.error(String.format("error scheduling task: %s", task.getClass().getSimpleName()), ex);
73 }
74 }
75
76 /**
77 * This method handles adding new tasks and processing of active tasks.
78 */
79 public synchronized void processTasks() {
80 Task t;
81 while((t = pending.poll()) != null) {
82 active.add(t);
83 }
84
85 for (final ListIterator<Task> itr = active.listIterator(); itr.hasNext();) {
86 final Task task = itr.next();
87
88 try {
89 //Start execution is called before process so that the task can get the elapsed time from the start of the task
90 task.setExecutionTime();
91 task.process();
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);
94 task.cancel();
95 }
96
97 if (!task.isRunning()) {
98 itr.remove();
99 }
100 }
101 }
102
103 /**
104 * Cancels a task that has a specific {@code attachment}.
105 *
106 * @param attachment
107 * The object that is attached to the task.
108 */
109 public void cancel(Object attachment) {
110 cancel(attachment, false);
111 }
112
113 /**
114 * Cancels a task that has a specific {@code attachment}.
115 *
116 * @param attachment
117 * The object that is attached to the task.
118 *
119 * @param logout
120 * When the task gets canceled this flag determines if the player should logout.
121 */
122 public void cancel(Object attachment, boolean logout) {
123 for (Task task : pending) {
124 if (attachment.equals(task.getAttachment().orElse(null))) {
125 task.cancel(logout);
126 }
127 }
128 }
129
130 public List<Task> getTasks() {
131 return active;
132 }
133}
A game representing a cyclic unit of work.
Definition Task.java:11
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.