Class ThreadUtilities


  • public class ThreadUtilities
    extends java.lang.Object
    Provides utility methods for managing threads.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void invokeAndWait​(java.lang.Runnable runnable)
      Equivalent to SwingUtilities.invokeAndWait(Runnable) except it correctly handles spurious wake ups which can cause the method to return before the runnable has completed.
      static void invokeLater​(java.lang.Runnable runnable)
      Delegates to EventQueue.invokeLater(Runnable).
      static void invokeNowOrLater​(java.lang.Runnable runnable)
      If the current thread is the swing thread, then immediately calls the given parameter's run method otherwise, this function is a wrapper to EventQueue.invokeLater(Runnable).
      static void invokeNowOrWait​(java.lang.Runnable runnable)
      If the current thread is the swing thread, then this method immediately calls the given parameter's run method otherwise, this method is a wrapper to EventQueue.invokeAndWait(Runnable).
      static <T> T invokeNowOrWait​(java.util.concurrent.Callable<T> callable)
      Invokes the given Callable in the swing thread and returns the result.
      static boolean sleep​(long milliseconds)
      A convenience method for calling Thread.sleep(long) without having to worry about handling the interrupted exception.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • sleep

        public static boolean sleep​(long milliseconds)
        A convenience method for calling Thread.sleep(long) without having to worry about handling the interrupted exception.
        Parameters:
        milliseconds - the length of time to sleep in milliseconds.
        Returns:
        true if the sleep completed. false if the sleep was interrupted (if Thread.sleep(long) threw an InterruptedException)
      • invokeNowOrWait

        public static void invokeNowOrWait​(java.lang.Runnable runnable)
        If the current thread is the swing thread, then this method immediately calls the given parameter's run method otherwise, this method is a wrapper to EventQueue.invokeAndWait(Runnable). If that method throws any checked exception (InterruptedException or InvocationTargetException) these will be wrapped in a RuntimeException and thrown by this method.

        Note that this method should not be called from any thread that may potentially have Thread.interrupt() called on it. If that does happen then this method may throw a RuntimeException if this thread is interrupted.

        Parameters:
        runnable - the Runnable whose run method should be executed synchronously on the EventQueue
        Throws:
        java.lang.RuntimeException - if EventQueue.invokeAndWait(Runnable) throws any checked Exception (InterruptedException or InvocationTargetException)
      • invokeAndWait

        public static void invokeAndWait​(java.lang.Runnable runnable)
                                  throws java.lang.InterruptedException,
                                         java.lang.reflect.InvocationTargetException
        Equivalent to SwingUtilities.invokeAndWait(Runnable) except it correctly handles spurious wake ups which can cause the method to return before the runnable has completed.

        Causes runnable to have its run method called in the dispatch thread of the system EventQueue. This will happen after all pending events are processed. The call blocks until this has happened. This method will throw an Error if called from the event dispatcher thread.
        Parameters:
        runnable - the Runnable whose run method should be executed synchronously on the EventQueue
        Throws:
        java.lang.InterruptedException - if any thread has interrupted this thread
        java.lang.reflect.InvocationTargetException - if a throwable is thrown when running runnable
        Since:
        API 4.61 (Geneious 5.6.1)
      • invokeLater

        public static void invokeLater​(java.lang.Runnable runnable)
        Delegates to EventQueue.invokeLater(Runnable). This method is provided here so that all invoke* type methods are available from a single class.
        Parameters:
        runnable - the runnable that should be run soon in the Swing thread.
        Since:
        API 4.700 (Geneious 7.0.0)
      • invokeNowOrWait

        public static <T> T invokeNowOrWait​(java.util.concurrent.Callable<T> callable)
        Invokes the given Callable in the swing thread and returns the result. If this method is not called from the Swing thread, it blocks until the swing thread can run the callable before returning.
        Parameters:
        callable - the Callable whose run method should be executed synchronously on the EventQueue
        Returns:
        the value returned from callable.
        Throws:
        java.lang.RuntimeException - if EventQueue.invokeAndWait(Runnable) throws any checked exception (InterruptedException or InvocationTargetException) or if the Callable throws an exception.
        Since:
        API 4.30 (Geneious 5.3.0)
      • invokeNowOrLater

        public static void invokeNowOrLater​(java.lang.Runnable runnable)
        If the current thread is the swing thread, then immediately calls the given parameter's run method otherwise, this function is a wrapper to EventQueue.invokeLater(Runnable).
        Parameters:
        runnable - the Runnable whose run method should be executed synchronously on the EventQueue