public class EDTExecutor
extends java.util.concurrent.AbstractExecutorService
EDTExecutor executes tasks in the swing event dispatcher thread (EDT).
EDTExecutor is a helper that allows to submit tasks to the EDT like
SwingUtilities.invokeLater(Runnable) does. The advantage is that it handles the case
of executing the code from inside or outside of the EDT in a transparent way.
In the same way, EDTExecutor allows to run commands synchronously inside the EDT
in the same way SwingUtilities.invokeAndWait(Runnable) does.
SwingWorker has something in common with EDTExecutor but it is too heavy
and for most applications we don't need such a complexity.
We don't really need EDTExecutor to be a ExecutorService because it
is very easy but in future we might want to extend by adding more features
(without reaching the same completeness of SwingWorker of course).
Life cycle methods are kept easy because this class does not manage the life cycle of the EDT.
In this perspective, and to avoid useless blocking due to synchronization, EDTExecutor
does not keep track of tasks scheduled to be run and not yet executed (@see shutdownNow().
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
hasBeenShutdown
A flag set is the executor has been shut down.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout,
java.util.concurrent.TimeUnit unit) |
void |
execute(java.lang.Runnable command)
Executes the passed command in the EDT.
|
void |
executeSync(java.lang.Runnable command)
Execute the passed command in the EDT synchronously.
|
static EDTExecutor |
instance() |
boolean |
isShutdown() |
boolean |
isTerminated() |
void |
shutdown()
Shuts down the Executor (and not the swing EDT):
EDTExecutor will not submit other tasks to the EDT. |
java.util.List<java.lang.Runnable> |
shutdownNow()
Shuts down the Executor (and not the swing EDT):
EDTExecutor will not submit other tasks to the EDT. |
protected volatile boolean hasBeenShutdown
public static EDTExecutor instance()
public void shutdown()
EDTExecutor will not submit other tasks to the EDT.public java.util.List<java.lang.Runnable> shutdownNow()
EDTExecutor will not submit other tasks to the EDT.null because this executor does not have a queue of tasks to submit.public boolean isShutdown()
public boolean isTerminated()
public boolean awaitTermination(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic void execute(java.lang.Runnable command)
It can be called from inside or outside of the swing event dispatcher thread.
command - The command to execute in the EDTpublic void executeSync(java.lang.Runnable command)
throws java.lang.reflect.InvocationTargetException,
java.lang.InterruptedException
It can be called from inside or outside of the swing event dispatcher thread.
command - The command to execute in the EDTjava.lang.reflect.InvocationTargetException - if an exception is thrown while running the commandjava.lang.InterruptedException - if we're interrupted while waiting for the event dispatching thread to finish excecuting command.run()