public class SwtExec extends AbstractExecutorService implements ScheduledExecutorService, Rx.HasRxExecutor
There are three "kinds" of SwtExec
:
async
-> performs actions using Display.asyncExec(java.lang.Runnable)
immediate
-> performs actions immediately if called from a UI thread, otherwise delegates to Display.asyncExec(java.lang.Runnable)
.blocking
-> performs actions immediately if called from a UI thread, else delegates to Display.syncExec(java.lang.Runnable)
.
In addition to the standard executor methods, each SwtExec
also has a method guardOn(org.eclipse.swt.widgets.Widget)
, which
returns a SwtExec.Guarded
instance - the cure for "Widget is disposed" errors. Guarded
has methods like
SwtExec.Guarded.wrap(java.lang.Runnable)
and SwtExec.Guarded.execute(java.lang.Runnable)
, whose contents are only executed if the guarded widget is not disposed.
SwtExec.Guarded
also contains the full API of Rx
,
which allows subscribing to Observable
s and ListenableFuture
s while guarding on a given widget.
Modifier and Type | Class and Description |
---|---|
static class |
SwtExec.Blocking
An SwtExec (obtained via
blocking() ) which adds a blocking get() method. |
static class |
SwtExec.Guarded
|
Modifier and Type | Field and Description |
---|---|
protected Display |
display |
protected Rx.RxExecutor |
rxExecutor |
protected Scheduler |
scheduler |
Modifier and Type | Method and Description |
---|---|
static SwtExec |
async()
Returns an "async" SwtExecutor.
|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown
request, or the timeout occurs, or the current thread is
interrupted, whichever happens first.
|
static SwtExec.Blocking |
blocking()
Returns a "blocking" SwtExecutor.
|
void |
execute(Runnable runnable)
Executes the given command at some time in the future.
|
Rx.RxExecutor |
getRxExecutor()
Returns an instance of
Rx.RxExecutor . |
Scheduler |
getRxScheduler()
Returns a Scheduler appropriate for Rx.
|
SwtExec.Guarded |
guardOn(ControlWrapper wrapper)
Returns an API for performing actions which are guarded on the given ControlWrapper.
|
SwtExec.Guarded |
guardOn(Widget widget)
Returns an API for performing actions which are guarded on the given Widget.
|
static SwtExec |
immediate()
Returns an "immediate" SwtExecutor.
|
boolean |
isShutdown()
Returns true if this executor has been shut down.
|
boolean |
isTerminated()
Returns true if all tasks have completed following shut down.
|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
long delay,
TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the
given delay.
|
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit)
Creates and executes a one-shot action that becomes enabled
after the given delay.
|
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit)
Creates and executes a periodic action that becomes enabled first
after the given initial delay, and subsequently with the given
period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
|
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit)
Creates and executes a periodic action that becomes enabled first
after the given initial delay, and subsequently with the
given delay between the termination of one execution and the
commencement of the next.
|
void |
shutdown()
Initiates an orderly shutdown in which previously submitted
tasks are executed, but no new tasks will be accepted.
|
List<Runnable> |
shutdownNow()
Attempts to stop all actively executing tasks, halts the
processing of waiting tasks, and returns a list of the tasks that were
awaiting execution.
|
static void |
timerExec(int ms,
Runnable runnable)
Executes the given runnable in the UI thread after the given delay.
|
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
protected final Display display
protected final Scheduler scheduler
protected final Rx.RxExecutor rxExecutor
public static SwtExec async()
When execute(Runnable)
is called, the Runnable
will be passed to Display.asyncExec
.
public static SwtExec immediate()
execute(Runnable)
is called from the SWT thread, the Runnable
will be executed immediately.Runnable
will be passed to Display.asyncExec
.public static SwtExec.Blocking blocking()
execute(Runnable)
is called from the SWT thread, the Runnable
will be executed immediately.Runnable
will be passed to Display.syncExec
.get()
method for doing a get in the UI thread.public static void timerExec(int ms, Runnable runnable)
public SwtExec.Guarded guardOn(Widget widget)
public SwtExec.Guarded guardOn(ControlWrapper wrapper)
public Rx.RxExecutor getRxExecutor()
Rx.RxExecutor
.getRxExecutor
in interface Rx.HasRxExecutor
public Scheduler getRxScheduler()
public void execute(Runnable runnable)
execute
in interface Executor
runnable
- the runnable taskRejectedExecutionException
- if this task cannot be
accepted for execution.NullPointerException
- if command is nullpublic void shutdown()
shutdown
in interface ExecutorService
SecurityException
- if a security manager exists and
shutting down this ExecutorService may manipulate
threads that the caller is not permitted to modify
because it does not hold RuntimePermission
("modifyThread"),
or the security manager's checkAccess method
denies access.public List<Runnable> shutdownNow()
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt()
, so any task that fails to respond to interrupts may never terminate.
shutdownNow
in interface ExecutorService
SecurityException
- if a security manager exists and
shutting down this ExecutorService may manipulate
threads that the caller is not permitted to modify
because it does not hold RuntimePermission
("modifyThread"),
or the security manager's checkAccess method
denies access.public boolean isShutdown()
isShutdown
in interface ExecutorService
public boolean isTerminated()
isTerminated
in interface ExecutorService
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
awaitTermination
in interface ExecutorService
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if interrupted while waitingpublic ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
command
- the task to executedelay
- the time from now to delay executionunit
- the time unit of the delay parameterRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if command is nullpublic <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
callable
- the function to executedelay
- the time from now to delay executionunit
- the time unit of the delay parameterRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if callable is nullpublic ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleAtFixedRate
in interface ScheduledExecutorService
command
- the task to executeinitialDelay
- the time to delay first executionperiod
- the period between successive executionsunit
- the time unit of the initialDelay and period parametersRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if command is nullIllegalArgumentException
- if period less than or equal to zeropublic ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleWithFixedDelay
in interface ScheduledExecutorService
command
- the task to executeinitialDelay
- the time to delay first executiondelay
- the delay between the termination of one
execution and the commencement of the nextunit
- the time unit of the initialDelay and delay parametersRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if command is nullIllegalArgumentException
- if delay less than or equal to zero