Class ThreadUtilities
- java.lang.Object
-
- com.biomatters.geneious.publicapi.utilities.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 toSwingUtilities.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 toEventQueue.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 toEventQueue.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 toEventQueue.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 callingThread.sleep(long)
without having to worry about handling the interrupted exception.
-
-
-
Method Detail
-
sleep
public static boolean sleep(long milliseconds)
A convenience method for callingThread.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 anInterruptedException
)
-
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 toEventQueue.invokeAndWait(Runnable)
. If that method throws any checked exception (InterruptedException
orInvocationTargetException
) these will be wrapped in aRuntimeException
and thrown by this method. Note that this method should not be called from any thread that may potentially haveThread.interrupt()
called on it. If that does happen then this method may throw aRuntimeException
if this thread is interrupted.- Parameters:
runnable
- the Runnable whose run method should be executed synchronously on the EventQueue- Throws:
java.lang.RuntimeException
- ifEventQueue.invokeAndWait(Runnable)
throws any checked Exception (InterruptedException
orInvocationTargetException
)
-
invokeAndWait
public static void invokeAndWait(java.lang.Runnable runnable) throws java.lang.InterruptedException, java.lang.reflect.InvocationTargetException
Equivalent toSwingUtilities.invokeAndWait(Runnable)
except it correctly handles spurious wake ups which can cause the method to return before the runnable has completed.
Causesrunnable
to have itsrun
method called in the dispatch thread ofthe 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
- theRunnable
whoserun
method should be executed synchronously on theEventQueue
- Throws:
java.lang.InterruptedException
- if any thread has interrupted this threadjava.lang.reflect.InvocationTargetException
- if a throwable is thrown when runningrunnable
- Since:
- API 4.61 (Geneious 5.6.1)
-
invokeLater
public static void invokeLater(java.lang.Runnable runnable)
Delegates toEventQueue.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
- ifEventQueue.invokeAndWait(Runnable)
throws any checked exception (InterruptedException
orInvocationTargetException
) 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 toEventQueue.invokeLater(Runnable)
.- Parameters:
runnable
- the Runnable whose run method should be executed synchronously on the EventQueue
-
-