public class SwtExec extends AbstractExecutorService implements ScheduledExecutorService, RxExecutor.Has
Executors
which execute on the SWT UI thread.
There are two primary kinds of SwtExec
:
async()
-> performs actions using Display.asyncExec
immediate()
-> performs actions immediately if called from a UI thread, otherwise delegates to asyncExec
.In addition to the standard executor methods, each SwtExec
also has a method guardOn
, which
returns a SwtExec.Guarded
instance - the cure for “Widget is disposed” errors. Guarded
is an Executor
and RxSubscriber
which
stops running tasks and cancels all subscriptions and futures when the guard widget is disposed.
SwtExec.immediate().guardOn(myWidget).subscribe(someFuture, value -> myWidget.setContentsTo(value));
In the example above, if the widget is disposed before the future completes, that’s fine! No “widget is disposed” errors.
blocking()
is similar to async()
and immediate()
, but it doesn’t support guard
- it’s just a simple Executor
.
It performs actions immediately if called from a UI thread, else delegates to the blocking Display.syncExec(java.lang.Runnable)
. It also
has the SwtExec.Blocking.get(Supplier)
method, which allows you to easily get a value using a function which must be called
on the SWT thread.
In the rare scenario where you need higher performance, it is possible to get similar behavior as immediate()
but with
less overhead (and safety) in swtOnly()
and sameThread()
. It is very rarely worth this sacrifice.
Modifier and Type | Class and Description |
---|---|
static class |
SwtExec.Blocking
An Executor (obtained via
blocking() ) which adds a blocking get() method. |
static class |
SwtExec.Guarded
|
Modifier and Type | Field and Description |
---|---|
protected RxExecutor |
rxExecutor |
Modifier and Type | Method and Description |
---|---|
static SwtExec |
async()
Returns an “async” SwtExecutor.
|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Deprecated.
|
static SwtExec.Blocking |
blocking()
Returns a “blocking” Executor for the SWT thread.
|
void |
execute(Runnable runnable)
Executes the given command at some time in the future.
|
RxExecutor |
getRxExecutor()
Returns an instance of
RxExecutor . |
SwtExec.Guarded |
guardOn(Chit chit)
Returns an API for performing actions which are guarded on the given Widget.
|
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.
|
static boolean |
isRunningOnUI()
Returns true iff called from the UI thread.
|
boolean |
isShutdown()
Deprecated.
|
boolean |
isTerminated()
Deprecated.
|
static SwtExec |
sameThread()
UNLESS YOU HAVE PERFORMANCE PROBLEMS, USE
immediate() INSTEAD. |
<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()
Deprecated.
|
List<Runnable> |
shutdownNow()
Deprecated.
|
static SwtExec |
swtOnly()
UNLESS YOU HAVE PERFORMANCE PROBLEMS, USE
immediate() INSTEAD. |
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 RxExecutor rxExecutor
public static boolean isRunningOnUI()
Returns true iff called from the UI thread.
public static SwtExec async()
Returns an “async” SwtExecutor.
When `execute(Runnable)` is called, the `Runnable` will be passed to Display.asyncExec
.
public static SwtExec immediate()
Returns an “immediate” SwtExecutor.
execute(Runnable)
is called from the SWT thread, the Runnable
will be executed immediately.Runnable
will be passed to Display.asyncExec
.In the rare case that immediate()
only ever receives events on the SWT thread, there are faster options:
swtOnly
is about 3x faster, and will throw an error if you call it from somewhere besides an SWT thread.sameThread
is about 15x faster, and will not throw an error if you call it from somewhere besides an SWT thread (but your callback probably will).It is very rare that sacrificing the safety of immediate()
is worth it. Here is the approximate throughput
of the three options on a Win 10, i7-2630QM machine.
immediate()
- 2.9 million events per secondswtOnly()
- 8.3 million events per secondsameThread()
- 50 million events per secondpublic static SwtExec.Blocking blocking()
Returns a “blocking” Executor for the SWT thread.
Display.syncExec
.get()
method for doing a get in the UI thread.public static void timerExec(int ms, Runnable runnable)
Executes the given runnable in the UI thread after the given delay.
public SwtExec.Guarded guardOn(Chit chit)
Returns an API for performing actions which are guarded on the given Widget.
public SwtExec.Guarded guardOn(Widget widget)
Returns an API for performing actions which are guarded on the given Widget.
public SwtExec.Guarded guardOn(ControlWrapper wrapper)
Returns an API for performing actions which are guarded on the given ControlWrapper.
public RxExecutor getRxExecutor()
Returns an instance of RxExecutor
.
getRxExecutor
in interface RxExecutor.Has
public void execute(Runnable runnable)
Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.
execute
in interface Executor
runnable
- the runnable taskRejectedExecutionException
- if this task cannot be
accepted for execution.NullPointerException
- if command is null@Deprecated public void shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.
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.@Deprecated public 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.
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.@Deprecated public boolean isShutdown()
Returns true if this executor has been shut down.
isShutdown
in interface ExecutorService
@Deprecated public boolean isTerminated()
Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.
isTerminated
in interface ExecutorService
@Deprecated public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
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)
Creates and executes a one-shot action that becomes enabled after the given delay.
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)
Creates and executes a ScheduledFuture that becomes enabled after the given delay.
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)
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. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.
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)
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. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.
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 zeropublic static SwtExec swtOnly()
UNLESS YOU HAVE PERFORMANCE PROBLEMS, USE immediate()
INSTEAD.
Returns an SwtExecutor which can only be called from the SWT
thread, and runs actions immediately. Has the same behavior
as immediate()
for callbacks on the SWT
thread. For values not on the SWT thread, immediate()
behaves
likes async()
, while swtOnly()
throws an exception.
public static SwtExec sameThread()
UNLESS YOU HAVE PERFORMANCE PROBLEMS, USE immediate()
INSTEAD.
Returns an SwtExec which runs actions immediately, without checking whether they were called from the SWT thread or not.