Runs a block of code on this execution context.
Runs a block of code on this execution context.
the task to execute
Reports that an asynchronous computation failed.
Reports that an asynchronous computation failed.
the cause of the failure
Returns string formatted according to given format string.
Returns string formatted according to given format string.
Format strings are as for String.format
(@see java.lang.String.format).
Prepares for the execution of a task.
Prepares for the execution of a task. Returns the prepared execution context.
prepare should be called at the site where an ExecutionContext is received (for
example, through an implicit method parameter). The returned execution context may
then be used to execute tasks. The role of prepare is to save any context relevant
to an execution's call site, so that this context may be restored at the
execution site. (These are often different: for example, execution may be
suspended through a Promise's future until the Promise is completed, which may
be done in another thread, on another stack.)
Note: a valid implementation of prepare is one that simply returns this.
the prepared execution context
An
ExecutionContextcan execute program logic asynchronously, typically but not necessarily on a thread pool.A general purpose
ExecutionContextmust be asynchronous in executing anyRunnablethat is passed into itsexecute-method. A special purposeExecutionContextmay be synchronous but must only be passed to code that is explicitly safe to be run using a synchronously executingExecutionContext.APIs such as
Future.onCompleterequire you to provide a callback and an implicitExecutionContext. The implicitExecutionContextwill be used to execute the callback.It is possible to simply import
scala.concurrent.ExecutionContext.Implicits.globalto obtain an implicitExecutionContext. This global context is a reasonable default thread pool.However, application developers should carefully consider where they want to set policy; ideally, one place per application (or per logically-related section of code) will make a decision about which
ExecutionContextto use. That is, you might want to avoid hardcodingscala.concurrent.ExecutionContext.Implicits.globalall over the place in your code. One approach is to add(implicit ec: ExecutionContext)to methods which need anExecutionContext. Then import a specific context in one place for the entire application or module, passing it implicitly to individual methods.A custom
ExecutionContextmay be appropriate to execute code which blocks on IO or performs long-running computations.ExecutionContext.fromExecutorServiceandExecutionContext.fromExecutorare good ways to create a customExecutionContext.The intent of
ExecutionContextis to lexically scope code execution. That is, each method, class, file, package, or application determines how to run its own code. This avoids issues such as running application callbacks on a thread pool belonging to a networking library. The size of a networking library's thread pool can be safely configured, knowing that only that library's network operations will be affected. Application callback execution can be configured separately.