public class ThreadUtilities extends Object
Modifier and Type | Method and Description |
---|---|
static void |
invokeAndWait(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(Runnable runnable)
Delegates to
EventQueue.invokeLater(Runnable) . |
static void |
invokeNowOrLater(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 <T> T |
invokeNowOrWait(Callable<T> callable)
Invokes the given Callable in the swing thread and returns the result.
|
static void |
invokeNowOrWait(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 boolean |
sleep(long milliseconds)
A convenience method for calling
Thread.sleep(long) without having to worry about handling the interrupted
exception. |
public static boolean sleep(long milliseconds)
Thread.sleep(long)
without having to worry about handling the interrupted
exception.milliseconds
- the length of time to sleep in milliseconds.Thread.sleep(long)
threw an InterruptedException
)public static void invokeNowOrWait(Runnable runnable)
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.runnable
- the Runnable whose run method should be executed synchronously on the EventQueueRuntimeException
- if EventQueue.invokeAndWait(Runnable)
throws any checked Exception (InterruptedException
or InvocationTargetException
)public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException
SwingUtilities.invokeAndWait(Runnable)
except it correctly handles spurious wake ups which can cause the method to
return before the runnable has completed.
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.runnable
- the Runnable
whose run
method should be executed
synchronously on the EventQueue
InterruptedException
- if any thread has
interrupted this threadInvocationTargetException
- if a throwable is thrown
when running runnable
public static void invokeLater(Runnable runnable)
EventQueue.invokeLater(Runnable)
. This method is provided here so that all invoke* type
methods are available from a single class.runnable
- the runnable that should be run soon in the Swing thread.public static <T> T invokeNowOrWait(Callable<T> callable)
callable
- the Callable whose run method should be executed synchronously on the EventQueueRuntimeException
- if EventQueue.invokeAndWait(Runnable)
throws any checked exception (InterruptedException
or InvocationTargetException
) or if the Callable throws an exception.public static void invokeNowOrLater(Runnable runnable)
EventQueue.invokeLater(Runnable)
.runnable
- the Runnable whose run method should be executed synchronously on the EventQueue