Package com.csse3200.game.concurrency
Class JobSystem
java.lang.Object
com.csse3200.game.concurrency.JobSystem
A job system provides a general-purpose way to run multi-threaded code. This is a recommended
approach for compute-heavy tasks which may slow down the game. When a 'job' is launched, it is
scheduled to be executed on an available thread at some time in the future. The job system makes
use of thread pooling and software tasks rather than spawning a new thread per task. See
Wiki/Concurrency for details.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> CompletableFuture<T>
Launch an asynchronous job which may be run on a separate thread.static <T> CompletableFuture<T>
launchBlocking
(Supplier<T> supplier) Launch an asynchronous job which may be run on a separate thread.
-
Method Details
-
launch
Launch an asynchronous job which may be run on a separate thread. The job should not block on anything except other jobs, i.e. using get(). For jobs which block on I/O, delays, etc. uselaunchBlocking(Supplier)
.- Type Parameters:
T
- Return type of the job- Parameters:
supplier
- Non-blocking method which is executed asynchronously.- Returns:
- A Future which evaluates to the job's return value. Calling get() will give the result of the supplied method, blocking until it's finished. Avoid calling get() in the main update loop.
-
launchBlocking
Launch an asynchronous job which may be run on a separate thread. This is much less efficient thanlaunch(Supplier)
since a new thread may be created for each call. Avoid unless blocking is necessary.- Type Parameters:
T
- Return type of the job- Parameters:
supplier
- Method which is executed asynchronously, and may block.- Returns:
- A Future which evaluates to the job's return value. Calling get() will give the result of the supplied method, blocking until it's finished.
-