SimpleAsyncTaskExecutor class final

A TaskExecutor that runs each task in a later event-loop turn (Timer.run), so the caller goes on first.

Use it for event listeners that should not hold up the publisher, as the executor of a SimpleApplicationEventMulticaster or as the TaskExecutor bean of @Async listeners. It is Spring's SimpleAsyncTaskExecutor, which starts a thread per task; here the tasks share the isolate, so a long synchronous task still blocks everything else.

An error of a task (thrown or from its Future) goes to errorHandler; without one it is logged and suppressed, so a failing task never becomes an uncaught error of the isolate (Spring logs the uncaught exception of the thread). activeCount and whenIdle tell whether tasks are still running, which helps in tests and for a graceful shutdown.

// Send the mail in the background; wait for it before exiting.
final executor = SimpleAsyncTaskExecutor(
  errorHandler: (error, stack) => log.error('task failed', error: error),
);
executor.execute(() async => await sendMail());
await executor.whenIdle;

Spring (Java):

var executor = new SimpleAsyncTaskExecutor();
executor.execute(this::sendMail);
Implemented types

Constructors

SimpleAsyncTaskExecutor({void errorHandler(Object error, StackTrace stackTrace)?})
Creates the executor; without an errorHandler, task errors are logged.

Properties

activeCount → int
The number of tasks submitted and not finished yet.
no setter
errorHandler → void Function(Object error, StackTrace stackTrace)?
Receives the error of a failing task (thrown or from its Future); null logs it at ERROR.
final
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
whenIdle → Future<void>
Completes once no task is pending or running (at once when none is). Await it in a test before checking what the tasks did, or before the program ends.
no setter

Methods

execute(FutureOr<void> task()) → void
Runs task, now or later depending on the implementation; a returned Future counts as the rest of the task. Returns at once for an asynchronous executor.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited