Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/scala.concurrent/ExecutionContext

ExecutionContext

scala.concurrent.ExecutionContext
See theExecutionContext companion object

AnExecutionContext can execute program logic asynchronously, typically but not necessarily on a thread pool.

A general purposeExecutionContext must be asynchronous in executing anyRunnable that is passed into itsexecute-method. A special purposeExecutionContext may be synchronous but must only be passed to code that is explicitly safe to be run using a synchronously executingExecutionContext.

APIs such asFuture.onComplete require you to provide a callback and an implicitExecutionContext. The implicitExecutionContext will be used to execute the callback.

While it is possible to simply importscala.concurrent.ExecutionContext.Implicits.global to obtain an implicitExecutionContext, application developers should carefully consider where they want to define the execution policy; ideally, one place per application — or per logically related section of code — will make a decision about whichExecutionContext to use. That is, you will mostly want to avoid hardcoding, especially via an import,scala.concurrent.ExecutionContext.Implicits.global. The recommended approach is to add(implicit ec: ExecutionContext) to methods, or class constructor parameters, which need anExecutionContext.

Then locally import a specificExecutionContext in one place for the entire application or module, passing it implicitly to individual methods. Alternatively define a local implicit val with the requiredExecutionContext.

A customExecutionContext may be appropriate to execute code which blocks on IO or performs long-running computations.ExecutionContext.fromExecutorService andExecutionContext.fromExecutor are good ways to create a customExecutionContext.

The intent ofExecutionContext is 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.

Attributes

Companion
object
Source
ExecutionContext.scala
Graph
Supertypes
classObject
traitMatchable
classAny
Known subtypes

Members list

Value members

Abstract methods

defexecute(runnable:Runnable):Unit

Runs a block of code on this execution context.

Runs a block of code on this execution context.

Value parameters

runnable

the task to execute

Attributes

Source
ExecutionContext.scala

Reports that an asynchronous computation failed.

Reports that an asynchronous computation failed.

Value parameters

cause

the cause of the failure

Attributes

Source
ExecutionContext.scala

Deprecated methods

Prepares for the execution of a task.

Prepares for the execution of a task. Returns the prepared execution context. The recommended implementation ofprepare is to returnthis.

This method should no longer be overridden or called. It was originally expected thatprepare would be called by all libraries that consume ExecutionContexts, in order to capture thread local context. However, this usage has proven difficult to implement in practice and instead it is now better to avoid usingprepare entirely.

Instead, if anExecutionContext needs to capture thread local context, it should capture that context when it is constructed, so that it doesn't need any additional preparation later.

Attributes

Deprecated
[Since version 2.12.0]preparation of ExecutionContexts will be removed
Source
ExecutionContext.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp