Class Threads

java.lang.Object
com.prineside.tdi2.Threads

public final class Threads extends Object
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.
  • Method Details

    • i

      public static Threads i()
    • runOnMainThreadBlocking

      public void runOnMainThreadBlocking(Runnable runnable, float timeoutSeconds)
      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) thread
      timeoutSeconds - max time (in seconds) the task may take. In case of timeout, the task will continue to run on the main thread
    • runOnMainThread

      public void runOnMainThread(Runnable runnable)
      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

      public void runAsync(Runnable runnable)
      Run a job on another thread. Uses a thread pool.
    • runAsync

      public void runAsync(Runnable runnable, ObjectConsumer<Exception> onError)
    • sortGdxArray

      public static <T> void sortGdxArray(Array<T> array, Comparator<? super T> comparator)
    • sort

      public static <T> void sort(T[] array, Comparator<? super T> comparator)
    • sortArraySlice

      public static <T> void sortArraySlice(T[] array, int start, int size, Comparator<? super T> comparator)
    • postRunnable

      public 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. Writes to log if runnable takes more than 80ms to execute
    • postRunnableOnce

      public void postRunnableOnce(Runnable runnable)
      Note: for ui-specific tasks (like updating some menu in response to in-game event) use UiManager
    • createThread

      public Thread createThread(String name, Runnable job, boolean shouldCrashTheGame)
      Create new daemon thread and handle its uncaught exceptions. Thread must be started with start()
      Parameters:
      name - name of the thread
      job - job to be done by the thread
      shouldCrashTheGame - if true, uncaught exceptions in this thread will stop the game
      Returns:
      new thread awaiting to be started
    • synchronize

      public void synchronize(Object lock, Runnable runnable)
    • tryFinally

      public void tryFinally(Runnable runnable, Runnable onFinally)
    • tryCatch

      public void tryCatch(Runnable runnableToTry, ObjectConsumer<Throwable> onError)
    • concurrentLoop

      public <T> void concurrentLoop(Array<T> elements, IntObjectConsumer<T> handler)
      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

      public <T> void concurrentLoop(T[] elements, int start, int count, IntObjectConsumer<T> handler)
    • asyncConcurrentLoop

      public <T> void asyncConcurrentLoop(T[] elements, int start, int count, IntObjectConsumer<T> handler, Runnable onFinish, ObjectConsumer<Exception> onError)