Package com.prineside.tdi2
Class Threads
java.lang.Object
com.prineside.tdi2.Threads
Manages all of the (multi)threading stuff
Also triggers the EngineCrash event if it catches an unhandled exception in any of the configured
threads
Stores crash report in a file and provides methods to get the previous crash report on the next
game start. Crash-handling behavior may not look like fitting this class but it has a small
footprint and is still related to threads. Not moved into a separate Manager to make it possible
to read crash reports early, before Engine starts.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescription<T> void
asyncConcurrentLoop
(T[] elements, int start, int count, IntObjectConsumer<T> handler, Runnable onFinish, ObjectConsumer<Exception> onError) <T> void
concurrentLoop
(Array<T> elements, IntObjectConsumer<T> handler) Run the loop on multiple threads.<T> void
concurrentLoop
(T[] elements, int start, int count, IntObjectConsumer<T> handler) createThread
(String name, Runnable job, boolean shouldCrashTheGame) Create new daemon thread and handle its uncaught exceptions.static Threads
i()
void
postRunnable
(Runnable runnable) Executes the runnable with Gdx.app.postRunnable() and stores the stacktrace to print a detailed info if runnable throws an exception.void
postRunnableOnce
(Runnable runnable) Note: for ui-specific tasks (like updating some menu in response to in-game event) use UiManagervoid
Run a job on another thread.void
runAsync
(Runnable runnable, ObjectConsumer<Exception> onError) void
runOnMainThread
(Runnable runnable) Runs the runnable immediately if called from the main thread.void
runOnMainThreadBlocking
(Runnable runnable, float timeoutSeconds) Run some task on the main (rendering) thread and block current thread till the task is done.static <T> void
sort
(T[] array, Comparator<? super T> comparator) static <T> void
sortArraySlice
(T[] array, int start, int size, Comparator<? super T> comparator) static <T> void
sortGdxArray
(Array<T> array, Comparator<? super T> comparator) void
synchronize
(Object lock, Runnable runnable) void
tryCatch
(Runnable runnableToTry, ObjectConsumer<Throwable> onError) void
tryFinally
(Runnable runnable, Runnable onFinally)
-
Method Details
-
i
-
runOnMainThreadBlocking
Run some task on the main (rendering) thread and block current thread till the task is done. Will run on the same thread immediately if called from the main thread. Similar to Gdx.app.postRunnable() but won't delay the execution if called from the main thread and will pause current thread till done.- Parameters:
runnable
- task to run on the main (rendering) threadtimeoutSeconds
- max time (in seconds) the task may take. In case of timeout, the task will continue to run on the main thread
-
runOnMainThread
Runs the runnable immediately if called from the main thread. Posts a runnable to GDX if not in a main thread. Similar to Gdx.app.postRunnable() but won't delay the execution if called from the main thread. If you need to run something at the exactly next frame, use postRunnable() -
runAsync
Run a job on another thread. Uses a thread pool. -
runAsync
-
sortGdxArray
-
sort
-
sortArraySlice
public static <T> void sortArraySlice(T[] array, int start, int size, Comparator<? super T> comparator) -
postRunnable
Executes the runnable with Gdx.app.postRunnable() and stores the stacktrace to print a detailed info if runnable throws an exception. Writes to log if runnable takes more than 80ms to execute -
postRunnableOnce
Note: for ui-specific tasks (like updating some menu in response to in-game event) use UiManager -
createThread
Create new daemon thread and handle its uncaught exceptions. Thread must be started with start()- Parameters:
name
- name of the threadjob
- job to be done by the threadshouldCrashTheGame
- if true, uncaught exceptions in this thread will stop the game- Returns:
- new thread awaiting to be started
-
synchronize
-
tryFinally
-
tryCatch
-
concurrentLoop
Run the loop on multiple threads. Blocks the thread it is being called from. If any exception happens during the loop, the game will stop. -
concurrentLoop
-
asyncConcurrentLoop
public <T> void asyncConcurrentLoop(T[] elements, int start, int count, IntObjectConsumer<T> handler, Runnable onFinish, ObjectConsumer<Exception> onError)
-