Package com.deco2800.game.concurrency
Class AsyncTaskQueue
java.lang.Object
com.deco2800.game.concurrency.AsyncTaskQueue
public class AsyncTaskQueue
extends java.lang.Object
An event pool which runs long-running rendering tasks on a single thread. These expensive
tasks are a part of an unbounded queue which executes them sequentially.
class XYZComponent extends UIComponent{
onCreate(){
// Listen to multiple events coming in at the same time
entity.getEvents("newAchievement", this::renderAchievementCard);
//...
}
private void renderAchievementCard(Achievement achievement){
// Aim: A card should render for at least 5 seconds and then disposed
// Then the next achievement card should be displayed
AsyncTaskQueue.enqueue(() -> {
paintCardUI();
// Display it for 5 seconds
Thread.sleep(5000); // long running code
destroyCardUI();
});
}
dispose(){
// Since the game area is out of focus, cancel all future rendering
// tasks immediately to prevent memory leaks and other nasty bugs.
AsyncTaskQueue.cancelFutureTasksNow();
//...
}
}
-
Method Summary
Modifier and Type Method Description static void
cancelFutureTasksNow()
Shut down the task queue immediately and cancel all future tasks.static void
enqueueTask(java.lang.Runnable task)
Dispatch a long-running task to the queue.static boolean
isShutDown()
Check if the asynchronous task queue is accepting any tasksstatic void
newQueue()
-
Method Details
-
newQueue
public static void newQueue() -
isShutDown
public static boolean isShutDown()Check if the asynchronous task queue is accepting any tasks- Returns:
- isShutdown
-
cancelFutureTasksNow
public static void cancelFutureTasksNow()Shut down the task queue immediately and cancel all future tasks. -
enqueueTask
public static void enqueueTask(java.lang.Runnable task)Dispatch a long-running task to the queue. An event pool which runs long-running rendering tasks on a single thread. These expensive tasks are a part of an unbounded queue which executes them sequentially.- Parameters:
task
- Preferably a lambda function with some long-running piece of code.
-