Class Async
Tasks are run in their own thread. Existing idle threads will be reused if possible to minimise overhead of creating new threads.
This is intended to replace the use of
new Thread(new Runnable() {
public void run() {
// do stuff
}
}).start();
with
Async.execute(() -> {
// do stuff
});
Repeating tasks can also be submitted in a similar way to using ScheduledExecutorServices.
For most uses, a thread running to execute something every second or so is idle for the majority
of the time. This allows fewer threads to be running and to reduce the idle time.- Since:
- 9.8
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA future that wraps a ListeningFuture and accepts success/failure callbacks.static classA future that wraps a ListeningScheduledFuture and accepts success/failure callbacks. -
Method Summary
Modifier and TypeMethodDescriptionstatic Async.ListeningFuture<?> Submit a callable object to be run asynchronously.static voidRun aRunnablein its own thread.static voidRun aRunnablein its own named thread.static Future<Collection<Future<Object>>> executeAll(Runnable... tasks) Submit a collection ofRunnablesto be executed concurrently.static Future<Collection<Future<Object>>> executeAll(Collection<Runnable> tasks) Submit a collection ofRunnablesto be executed concurrently.static Async.ListeningScheduledFuture<?> Schedule a task to run after a given delay.static Async.ListeningScheduledFuture<?> Schedule a task to run after a given delay.static <T> Async.ListeningScheduledFuture<T> Schedule a task to run after a given delay.static <T> Async.ListeningScheduledFuture<T> Schedule a task to run after a given delay.static ScheduledFuture<?> scheduleAtFixedRate(Runnable target, long delay, long period, TimeUnit unit) Schedule a task to be run repeatedly at a fixed rate after an initial delay.static Async.ListeningScheduledFuture<?> scheduleAtFixedRate(Runnable target, long delay, long period, TimeUnit unit, String nameFormat, Object... args) Schedule a task to be run repeatedly at a fixed rate after an initial delay.static ScheduledFuture<?> scheduleWithFixedDelay(Runnable target, long delay, long period, TimeUnit unit) Schedule a task to be run repeatedly with a fixed delay between executions.static Async.ListeningScheduledFuture<?> scheduleWithFixedDelay(Runnable target, long delay, long period, TimeUnit unit, String nameFormat, Object... args) Schedule a task to be run repeatedly with a fixed delay between executions.static Async.ListeningFuture<?> Submit a task to be run in its own thread.static Async.ListeningFuture<?> Submit a task to be run in its own thread.static <T> Async.ListeningFuture<T> Submit a task to be run in its own thread.static <T> Async.ListeningFuture<T> Submit a task to be run in its own named thread.static <T> Future<Collection<Future<T>>> submitAll(Collection<Callable<T>> tasks) Submit a list of callables to be executed concurrently.static <T> Future<Collection<Future<T>>> Submit a list of callables to be executed concurrently.
-
Method Details
-
execute
Run aRunnablein its own thread.- Parameters:
target- The task to be run- Since:
- 9.8
- See Also:
-
execute
Run aRunnablein its own named thread. The name is created usingString.format(java.lang.String, java.lang.Object...)with the given nameFormat and args- Parameters:
target- The task to be runnameFormat- The name of the thread to run this taskargs- Objects to format the name with- Since:
- 9.8
- See Also:
-
submit
Submit a task to be run in its own thread. Returns a Future giving access to the completion or error state of the task and allowing tasks to be cancelled.The Future also gives access to the return value of the
target.The returned Future can have callbacks added to be run on success or failure of the callable. See
onSuccessandonFailure.- Type Parameters:
T- the return type of the task to be run- Parameters:
target- The task to be run- Returns:
- A Future
- Since:
- 9.8
- See Also:
-
submit
public static <T> Async.ListeningFuture<T> submit(Callable<T> target, String nameFormat, Object... args) Submit a task to be run in its own named thread. Returns a Future giving access to the completion or error state of the task and allowing tasks to be cancelled.The Future also gives access to the return value of the
Callable.This method differs from
submit(Callable)in that it renames the thread before running the task. The name is created usingString.format(java.lang.String, java.lang.Object...)with the given nameFormat and args.The returned Future can have callbacks added to be run on success or failure of the callable. See
onSuccessandonFailure.- Type Parameters:
T- the return type of the task to be run- Parameters:
target- The task to be runnameFormat- For the thread used to run this taskargs- Objects to format the name with- Returns:
- A Future
- Since:
- 9.8
- See Also:
-
submit
Submit a task to be run in its own thread. Returns a Future giving access to the completion or error state of the task and allowing tasks to be cancelled.The Future's get method will return
nullon successful completion.The returned Future can have callbacks added to be run on success or failure of the runnable. See
onSuccessandonFailure.- Parameters:
target- The task to be run- Returns:
- Future giving access to the state of the running task
- Since:
- 9.8
- See Also:
-
submit
Submit a task to be run in its own thread. Returns a Future giving access to the completion or error state of the task and allowing tasks to be cancelled.The Future's get method will return
nullon successful completionThis differs from
submit(Runnable)in that it renames the thread before running the task. The name is created usingString.format(java.lang.String, java.lang.Object...)with the given nameFormat and args.The returned Future can have callbacks added to be run on success or failure of the runnable. See
onSuccessandonFailure.- Parameters:
target- The task to be runnameFormat- For the thread used to run this taskargs- Objects to format the name with- Returns:
- Future giving access to the state of the running task
- Since:
- 9.8
- See Also:
-
submitAll
Submit a list of callables to be executed concurrently. Returns a future whosegetmethod returns a list ofFuturesrepresenting the individual tasks. The future will only complete when all of the individual tasks are complete (whether with success or failure or cancellation).- Type Parameters:
T- The return type of the given callables- Parameters:
tasks- a collection of callables to be executed concurrently.- Returns:
- a Future representing the state of all the tasks
- See Also:
-
submitAll
Submit a list of callables to be executed concurrently. Returns a future whosegetmethod returns a list ofFuturesrepresenting the individual tasks. The future will only complete when all of the individual tasks are complete (whether with success or failure or cancellation).- Type Parameters:
T- The return type of the given callables- Parameters:
tasks- a collection of callables to be executed concurrently.- Returns:
- a Future representing the state of all the tasks
- See Also:
-
executeAll
Submit a collection ofRunnablesto be executed concurrently. Returns a Future whosegetmethod returns a list ofFutureswhen all tasks are complete (whether successful or not). For successful runnables, thegetmethod of the returned Future will return null.The
future.getmethod of unsuccessful tasks will throw an ExecutionException.- Parameters:
tasks- The collection of tasks to execute concurrently- Returns:
- Future holding list of futures of the individual tasks
- See Also:
-
executeAll
Submit a collection ofRunnablesto be executed concurrently. Returns a Future whosegetmethod returns a list ofFutureswhen all tasks are complete (whether successful or not). For successful runnables, thegetmethod of the returned Future will return null.The
future.getmethod of unsuccessful tasks will throw an ExecutionException.- Parameters:
tasks- The collection of tasks to execute concurrently- Returns:
- Future holding list of futures of the individual tasks
- See Also:
-
scheduleAtFixedRate
public static ScheduledFuture<?> scheduleAtFixedRate(Runnable target, long delay, long period, TimeUnit unit) Schedule a task to be run repeatedly at a fixed rate after an initial delay.This differs from
scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)in that the time between executions can vary if the duration of the task is variable.- Parameters:
target- The task to rundelay- The time before the initial executionperiod- The period between successive executionsunit- Time unit of delay and period- Returns:
- ScheduledFuture giving caller ability to check status and cancel task
- Since:
- 9.8
- See Also:
-
scheduleAtFixedRate
public static Async.ListeningScheduledFuture<?> scheduleAtFixedRate(Runnable target, long delay, long period, TimeUnit unit, String nameFormat, Object... args) Schedule a task to be run repeatedly at a fixed rate after an initial delay.This differs from
scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)in that the time between executions can vary if the duration of the task is variable.This differs from
scheduleAtFixedRate(Runnable, long, long, TimeUnit)in that it renames the thread before running the task. The name is created usingString.format(java.lang.String, java.lang.Object...)with the given nameFormat and args.- Parameters:
target- The task to rundelay- The time before the initial executionperiod- The period between successive executionsunit- Time unit of delay and periodnameFormat- For the thread used to run this taskargs- Objects to format the name with- Returns:
- Future giving caller ability to check status and cancel task
- Since:
- 9.8
- See Also:
-
scheduleWithFixedDelay
public static ScheduledFuture<?> scheduleWithFixedDelay(Runnable target, long delay, long period, TimeUnit unit) Schedule a task to be run repeatedly with a fixed delay between executions. The first execution can be delayed by a different amount of time.This differs from
scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)in that the period can vary if the duration of the task is variable;- Parameters:
target- The task to rundelay- The time before the initial executionperiod- The time between successive executionsunit- Time unit of delay and period- Returns:
- ScheduledFuture giving caller ability to check status and cancel task
- Since:
- 9.8
- See Also:
-
scheduleWithFixedDelay
public static Async.ListeningScheduledFuture<?> scheduleWithFixedDelay(Runnable target, long delay, long period, TimeUnit unit, String nameFormat, Object... args) Schedule a task to be run repeatedly with a fixed delay between executions. The first execution can be delayed by a different amount of time.This differs from
scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)in that the period can vary if the duration of the task is variable.This differs from
scheduleWithFixedDelay(Runnable, long, long, TimeUnit)in that it renames the thread before running the task. The name is created usingString.format(java.lang.String, java.lang.Object...)with the given nameFormat and args.- Parameters:
target- The task to rundelay- The time before the initial executionperiod- The time between successive executionsunit- Time unit of delay and periodnameFormat- For the thread used to run this taskargs- Objects to format the name with- Returns:
- ScheduledFuture giving caller ability to check status and cancel task
- Since:
- 9.8
- See Also:
-
schedule
public static Async.ListeningScheduledFuture<?> schedule(Runnable target, long delay, TimeUnit unit) Schedule a task to run after a given delay. The remaining time and status of the task can be checked by the caller using the returned future.Future.get()will return null after the task is complete.The returned Future can have callbacks added to be run on success or failure of the runnable. See
onSuccessandonFailure.- Parameters:
target- The task to rundelay- The time to wait before runningunit- The units of the given delay- Returns:
- A ScheduledFuture giving access to status of task
- Since:
- 9.8
- See Also:
-
schedule
public static Async.ListeningScheduledFuture<?> schedule(Runnable target, long delay, TimeUnit unit, String nameFormat, Object... args) Schedule a task to run after a given delay. The remaining time and status of the task can be checked by the caller using the returned future.Future.get()will return null after the task is complete.This differs from
schedule(Runnable, long, TimeUnit)in that it renames the thread before running the task. The name is created usingString.format(java.lang.String, java.lang.Object...)with the given nameFormat and args.The returned Future can have callbacks added to be run on success or failure of the runnable. See
onSuccessandonFailure.- Parameters:
target- The task to rundelay- The time to wait before runningunit- The units of the given delaynameFormat- For the thread used to run this taskargs- Objects to format the name with- Returns:
- A ScheduledFuture giving access to status of task
- Since:
- 9.8
- See Also:
-
schedule
public static <T> Async.ListeningScheduledFuture<T> schedule(Callable<T> target, long delay, TimeUnit unit) Schedule a task to run after a given delay. The remaining time and status of the task can be checked by the caller using the returned future.Future.get()will return the result of the task after completion.The returned Future can have callbacks added to be run on success or failure of the callable. See
onSuccessandonFailure.- Type Parameters:
T- The return type of the given task- Parameters:
target- The task to rundelay- The time to wait before runningunit- The units of the given delay- Returns:
- A ScheduledFuture giving access to status of task
- See Also:
-
schedule
public static <T> Async.ListeningScheduledFuture<T> schedule(Callable<T> target, long delay, TimeUnit unit, String nameFormat, Object... args) Schedule a task to run after a given delay. The remaining time and status of the task can be checked by the caller using the returned future.Future.get()will return the result of the task after completion.This differs from
schedule(Callable, long, TimeUnit)in that it renames the thread before running the task. The name is created usingString.format(java.lang.String, java.lang.Object...)with the given nameFormat and args.The returned Future can have callbacks added to be run on success or failure of the callable. See
onSuccessandonFailure.- Type Parameters:
T- The return type of the given task- Parameters:
target- The task to rundelay- The time to wait before runningunit- The units of the given delaynameFormat- For the thread used to run this taskargs- Objects to format the name with- Returns:
- A ScheduledFuture giving access to status of task
- See Also:
-
call
Submit a callable object to be run asynchronously. This method is a duplicate ofsubmit(Callable)and only exists to work around a Jython bug where incorrect type resolution is used (Callable python objects are used as Runnables instead so return values are lost).In most cases, Jython functions and methods are callable. If a method needs parameters passed to it, a lambda can be used. eg
>>> def add(a, b): ... sleep(10) ... return a + b ... >>> future = Async.call(lambda: add(1, 2)) >>> future.get() # waits (just under) 10 seconds before returning 3
This can also be used to execute things that can't be cast to Callable directly (eg classes with __call__ method). The returned Future can be used as normal to cancel or get the return value.The returned Future can have callbacks added to be run on success or failure of the callable. See
onSuccessandonFailure.- Parameters:
target- should be callable- Returns:
- Future giving access to PyObject returned by callable. PyNone if void function
- Since:
- 9.8
- See Also:
-