Package com.diffplug.common.swt
Class SwtExec
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- com.diffplug.common.swt.SwtExec
-
- All Implemented Interfaces:
RxExecutor.Has,java.util.concurrent.Executor,java.util.concurrent.ExecutorService,java.util.concurrent.ScheduledExecutorService
public class SwtExec extends java.util.concurrent.AbstractExecutorService implements java.util.concurrent.ScheduledExecutorService, RxExecutor.Has
Executorswhich execute on the SWT UI thread. There are two primary kinds of `SwtExec`: -async()-> performs actions usingDisplay.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 methodguardOn, which returns aSwtExec.Guardedinstance - the cure for "Widget is disposed" errors. `Guarded` is anExecutorandRxSubscriberwhich stops running tasks and cancels all subscriptions and futures when the guard widget is disposed. ```java 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 blockingDisplay.syncExec(java.lang.Runnable). It also has theSwtExec.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 asimmediate()but with less overhead (and safety) inswtOnly()andsameThread(). It is very rarely worth this sacrifice.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSwtExec.BlockingAn Executor (obtained viablocking()) which adds a blockingget()method.static classSwtExec.GuardedExecutorandRxfor conducting actions which are guarded on an SWT widget.
-
Field Summary
Fields Modifier and Type Field Description protected RxExecutorrxExecutor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static SwtExecasync()Returns an "async" SwtExecutor.booleanawaitTermination(long timeout, java.util.concurrent.TimeUnit unit)Deprecated.static SwtExec.Blockingblocking()Returns a "blocking" Executor for the SWT thread.voidexecute(java.lang.Runnable runnable)Executes the given command at some time in the future.RxExecutorgetRxExecutor()Returns an instance ofRxExecutor.SwtExec.GuardedguardOn(Chit chit)Returns an API for performing actions which are guarded on the given Widget.SwtExec.GuardedguardOn(ControlWrapper wrapper)Returns an API for performing actions which are guarded on the given ControlWrapper.SwtExec.GuardedguardOn(Widget widget)Returns an API for performing actions which are guarded on the given Widget.static SwtExecimmediate()Returns an "immediate" SwtExecutor.static booleanisRunningOnUI()Returns true iff called from the UI thread.booleanisShutdown()Deprecated.booleanisTerminated()Deprecated.static SwtExecsameThread()UNLESS YOU HAVE PERFORMANCE PROBLEMS, USEimmediate()INSTEAD.java.util.concurrent.ScheduledFuture<?>schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)Creates and executes a one-shot action that becomes enabled after the given delay.<V> java.util.concurrent.ScheduledFuture<V>schedule(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit)Creates and executes a ScheduledFuture that becomes enabled after the given delay.java.util.concurrent.ScheduledFuture<?>scheduleAtFixedRate(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.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.java.util.concurrent.ScheduledFuture<?>scheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.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.voidshutdown()Deprecated.java.util.List<java.lang.Runnable>shutdownNow()Deprecated.static SwtExecswtOnly()UNLESS YOU HAVE PERFORMANCE PROBLEMS, USEimmediate()INSTEAD.static voidtimerExec(int ms, java.lang.Runnable runnable)Executes the given runnable in the UI thread after the given delay.-
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
-
-
-
-
Field Detail
-
rxExecutor
protected final RxExecutor rxExecutor
-
-
Method Detail
-
isRunningOnUI
public static boolean isRunningOnUI()
Returns true iff called from the UI thread.
-
async
public static SwtExec async()
Returns an "async" SwtExecutor.When `execute(Runnable)` is called, the `Runnable` will be passed to
Display.asyncExec.
-
immediate
public static SwtExec immediate()
Returns an "immediate" SwtExecutor. - When `execute(Runnable)` is called from the SWT thread, the `Runnable` will be executed immediately. - Else, the `Runnable` will be passed toDisplay.asyncExec. In the rare case that `immediate()` only ever receives events on the SWT thread, there are faster options: -swtOnlyis about 3x faster, and will throw an error if you call it from somewhere besides an SWT thread. -sameThreadis 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 second - `swtOnly()` - 8.3 million events per second - `sameThread()` - 50 million events per second
-
blocking
public static SwtExec.Blocking blocking()
Returns a "blocking" Executor for the SWT thread.- When `execute(Runnable)` is called from the SWT thread, the `Runnable` will be executed immediately.
- Else, the `Runnable` will be passed to
Display.syncExec.
get()method for doing a get in the UI thread.
-
timerExec
public static void timerExec(int ms, java.lang.Runnable runnable)Executes the given runnable in the UI thread after the given delay.
-
guardOn
public SwtExec.Guarded guardOn(Chit chit)
Returns an API for performing actions which are guarded on the given Widget.
-
guardOn
public SwtExec.Guarded guardOn(Widget widget)
Returns an API for performing actions which are guarded on the given Widget.
-
guardOn
public SwtExec.Guarded guardOn(ControlWrapper wrapper)
Returns an API for performing actions which are guarded on the given ControlWrapper.
-
getRxExecutor
public RxExecutor getRxExecutor()
Returns an instance ofRxExecutor.- Specified by:
getRxExecutorin interfaceRxExecutor.Has
-
execute
public void execute(java.lang.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.- Specified by:
executein interfacejava.util.concurrent.Executor- Parameters:
runnable- the runnable task- Throws:
java.util.concurrent.RejectedExecutionException- if this task cannot be accepted for execution.java.lang.NullPointerException- if command is null
-
shutdown
@Deprecated public void shutdown()
Deprecated.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.- Specified by:
shutdownin interfacejava.util.concurrent.ExecutorService- Throws:
java.lang.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 holdRuntimePermission("modifyThread"), or the security manager's checkAccess method denies access.
-
shutdownNow
@Deprecated public java.util.List<java.lang.Runnable> shutdownNow()
Deprecated.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.- Specified by:
shutdownNowin interfacejava.util.concurrent.ExecutorService- Returns:
- list of tasks that never commenced execution
- Throws:
java.lang.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 holdRuntimePermission("modifyThread"), or the security manager's checkAccess method denies access.
-
isShutdown
@Deprecated public boolean isShutdown()
Deprecated.Returns true if this executor has been shut down.- Specified by:
isShutdownin interfacejava.util.concurrent.ExecutorService- Returns:
- true if this executor has been shut down
-
isTerminated
@Deprecated public boolean isTerminated()
Deprecated.Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.- Specified by:
isTerminatedin interfacejava.util.concurrent.ExecutorService- Returns:
- true if all tasks have completed following shut down
-
awaitTermination
@Deprecated public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionDeprecated.Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.- Specified by:
awaitTerminationin interfacejava.util.concurrent.ExecutorService- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- true if this executor terminated and false if the timeout elapsed before termination
- Throws:
java.lang.InterruptedException- if interrupted while waiting
-
schedule
public java.util.concurrent.ScheduledFuture<?> schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)Creates and executes a one-shot action that becomes enabled after the given delay.- Specified by:
schedulein interfacejava.util.concurrent.ScheduledExecutorService- Parameters:
command- the task to executedelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a ScheduledFuture representing pending completion of the task and whose get() method will return null upon completion
- Throws:
java.util.concurrent.RejectedExecutionException- if the task cannot be scheduled for executionjava.lang.NullPointerException- if command is null
-
schedule
public <V> java.util.concurrent.ScheduledFuture<V> schedule(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit)Creates and executes a ScheduledFuture that becomes enabled after the given delay.- Specified by:
schedulein interfacejava.util.concurrent.ScheduledExecutorService- Parameters:
callable- the function to executedelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a ScheduledFuture that can be used to extract result or cancel
- Throws:
java.util.concurrent.RejectedExecutionException- if the task cannot be scheduled for executionjava.lang.NullPointerException- if callable is null
-
scheduleAtFixedRate
public java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.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.- Specified by:
scheduleAtFixedRatein interfacejava.util.concurrent.ScheduledExecutorService- Parameters:
command- the task to executeinitialDelay- the time to delay first executionperiod- the period between successive executionsunit- the time unit of the initialDelay and period parameters- Returns:
- a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
- Throws:
java.util.concurrent.RejectedExecutionException- if the task cannot be scheduled for executionjava.lang.NullPointerException- if command is nulljava.lang.IllegalArgumentException- if period less than or equal to zero
-
scheduleWithFixedDelay
public java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.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.- Specified by:
scheduleWithFixedDelayin interfacejava.util.concurrent.ScheduledExecutorService- Parameters:
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 parameters- Returns:
- a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
- Throws:
java.util.concurrent.RejectedExecutionException- if the task cannot be scheduled for executionjava.lang.NullPointerException- if command is nulljava.lang.IllegalArgumentException- if delay less than or equal to zero
-
swtOnly
public static SwtExec swtOnly()
UNLESS YOU HAVE PERFORMANCE PROBLEMS, USEimmediate()INSTEAD. Returns an SwtExecutor which can only be called from the SWT thread, and runs actions immediately. Has the same behavior asimmediate()for callbacks on the SWT thread. For values not on the SWT thread, `immediate()` behaves likesasync(), while `swtOnly()` throws an exception.
-
sameThread
public static SwtExec sameThread()
UNLESS YOU HAVE PERFORMANCE PROBLEMS, USEimmediate()INSTEAD. Returns an SwtExec which runs actions immediately, without checking whether they were called from the SWT thread or not.
-
-