RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.game.task.TaskManager Class Reference

The class that handles scheduling tasks, and processing them. More...

Public Member Functions

void cancel (Object attachment)
 Cancels a task that has a specific attachment.
void cancel (Object attachment, boolean logout)
 Cancels a task that has a specific attachment.
List< TaskgetTasks ()
synchronized void processTasks ()
 This method handles adding new tasks and processing of active tasks.
synchronized void schedule (Task task)
 Schedules a Task to be ran in the future.

Private Attributes

final List< Taskactive = new ArrayList<>()
 The list of tasks that are currently running.
final Queue< Taskpending = new ConcurrentLinkedQueue<>()
 The queue of tasks that are waiting to be executed.

Static Private Attributes

static final Logger logger = LogManager.getLogger(TaskManager.class)
 The single logger for this class.

Detailed Description

The class that handles scheduling tasks, and processing them.

Author
nshusa

Definition at line 18 of file TaskManager.java.

Member Function Documentation

◆ cancel() [1/2]

void com.runehive.game.task.TaskManager.cancel ( Object attachment)

Cancels a task that has a specific attachment.

Parameters
attachmentThe object that is attached to the task.

Definition at line 109 of file TaskManager.java.

109 {
110 cancel(attachment, false);
111 }

References cancel().

Referenced by cancel().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cancel() [2/2]

void com.runehive.game.task.TaskManager.cancel ( Object attachment,
boolean logout )

Cancels a task that has a specific attachment.

Parameters
attachmentThe object that is attached to the task.
logoutWhen the task gets canceled this flag determines if the player should logout.

Definition at line 122 of file TaskManager.java.

122 {
123 for (Task task : pending) {
124 if (attachment.equals(task.getAttachment().orElse(null))) {
125 task.cancel(logout);
126 }
127 }
128 }

References pending.

◆ getTasks()

List< Task > com.runehive.game.task.TaskManager.getTasks ( )

Definition at line 130 of file TaskManager.java.

130 {
131 return active;
132 }

References active.

◆ processTasks()

synchronized void com.runehive.game.task.TaskManager.processTasks ( )

This method handles adding new tasks and processing of active tasks.

Definition at line 79 of file TaskManager.java.

79 {
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 }

References active, logger, and pending.

◆ schedule()

synchronized void com.runehive.game.task.TaskManager.schedule ( Task task)

Schedules a Task to be ran in the future.

Parameters
taskThe task to schedule.

Definition at line 44 of file TaskManager.java.

44 {
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 }

References logger, and pending.

Member Data Documentation

◆ active

final List<Task> com.runehive.game.task.TaskManager.active = new ArrayList<>()
private

The list of tasks that are currently running.

A ArrayList is used here for very fast iteration speed.

Definition at line 36 of file TaskManager.java.

Referenced by getTasks(), and processTasks().

◆ logger

final Logger com.runehive.game.task.TaskManager.logger = LogManager.getLogger(TaskManager.class)
staticprivate

The single logger for this class.

Definition at line 23 of file TaskManager.java.

Referenced by processTasks(), and schedule().

◆ pending

final Queue<Task> com.runehive.game.task.TaskManager.pending = new ConcurrentLinkedQueue<>()
private

The queue of tasks that are waiting to be executed.

A ConcurrentLinkedQueue is used here so tasks can be added safely from multiple threads.

Definition at line 30 of file TaskManager.java.

Referenced by cancel(), processTasks(), and schedule().


The documentation for this class was generated from the following file: