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 trait

Contains factory methods for creating execution contexts.

Attributes

Companion
trait
Source
ExecutionContext.scala
Graph
Supertypes
classObject
traitMatchable
classAny
Self type

Members list

Type members

Classlikes

objectImplicits

Attributes

Source
ExecutionContext.scala
Supertypes
classObject
traitMatchable
classAny
Self type

WARNING: Only ever execute logic which will quickly return control to the caller.

WARNING: Only ever execute logic which will quickly return control to the caller.

ThisExecutionContext steals execution time from other threads by having itsRunnables run on theThread which callsexecute and then yielding back control to the caller after *all* itsRunnables have been executed. Nested invocations ofexecute will be trampolined to prevent uncontrolled stack space growth.

When usingparasitic with abstractions such asFuture it will in many cases be non-deterministic as to whichThread will be executing the logic, as it depends on when/if thatFuture is completed.

Do *not* call any blocking code in theRunnables submitted to thisExecutionContext as it will prevent progress by other enqueuedRunnables and the callingThread.

Symptoms of misuse of thisExecutionContext include, but are not limited to, deadlocks and severe performance problems.

AnyNonFatal orInterruptedExceptions will be reported to thedefaultReporter.

Attributes

Source
ExecutionContext.scala
Supertypes
traitExecutor
classObject
traitMatchable
classAny
Show all
Self type

Value members

Concrete methods

Creates anExecutionContext from the givenExecutor.

Creates anExecutionContext from the givenExecutor.

Value parameters

e

theExecutor to use. Ifnull, a newExecutor is created withdefault configuration.

reporter

a function for error reporting

Attributes

Returns

theExecutionContext using the givenExecutor

Source
ExecutionContext.scala

Creates anExecutionContext from the givenExecutor with thedefault reporter.

Creates anExecutionContext from the givenExecutor with thedefault reporter.

Value parameters

e

theExecutor to use. Ifnull, a newExecutor is created withdefault configuration.

Attributes

Returns

theExecutionContext using the givenExecutor

Source
ExecutionContext.scala

Creates anExecutionContext from the givenExecutorService.

Creates anExecutionContext from the givenExecutorService.

Value parameters

e

theExecutorService to use. Ifnull, a newExecutorService is created withdefault configuration.

reporter

a function for error reporting

Attributes

Returns

theExecutionContext using the givenExecutorService

Source
ExecutionContext.scala

Creates anExecutionContext from the givenExecutorService with thedefault reporter.

Creates anExecutionContext from the givenExecutorService with thedefault reporter.

If it is guaranteed that none of the executed tasks are blocking, a single-threadedExecutorService can be used to create anExecutionContext as follows:

import java.util.concurrent.Executorsval ec = ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor())

Value parameters

e

theExecutorService to use. Ifnull, a newExecutorService is created withdefault configuration.

Attributes

Returns

theExecutionContext using the givenExecutorService

Source
ExecutionContext.scala

Concrete fields

The default reporter simply prints the stack trace of theThrowable toSystem.err.

The default reporter simply prints the stack trace of theThrowable toSystem.err.

Attributes

Returns

the function for error reporting

Source
ExecutionContext.scala

The globalExecutionContext.

The globalExecutionContext. This defaultExecutionContext implementation is backed by a work-stealing thread pool. It can be configured via the following system properties:

  • scala.concurrent.context.minThreads = defaults to "1"

  • scala.concurrent.context.numThreads = defaults to "x1" (i.e. the current number of available processors * 1)

  • scala.concurrent.context.maxThreads = defaults to "x1" (i.e. the current number of available processors * 1)

  • scala.concurrent.context.maxExtraThreads = defaults to "256"

The pool size of threads is thennumThreads bounded byminThreads on the lower end andmaxThreads on the high end.

ThemaxExtraThreads is the maximum number of extra threads to have at any given time to evade deadlock, seescala.concurrent.blocking.

Theglobal execution context can be used explicitly, by defining animplicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global, or by importingExecutionContext.Implicits.global.

Batching short-lived nested tasks

Asynchronous code with short-lived nested tasks is executed more efficiently when usingExecutionContext.opportunistic (continue reading to learn why it isprivate[scala] and how to access it).

ExecutionContext.opportunistic uses the same thread pool asExecutionContext.global. It attempts to batch nested task and execute them on the same thread as the enclosing task. This is ideally suited to execute short-lived tasks as it reduces the overhead of context switching.

WARNING: long-running and/or blocking tasks should be demarcated withinscala.concurrent.blocking-blocks to ensure that any pending tasks in the current batch can be executed by another thread onglobal.

How to use

This field isprivate[scala] to maintain binary compatibility. It was added in 2.13.4, code that references it directly fails to run with a 2.13.0-3 Scala library.

Libraries should not reference this field directly because users of the library might be using an earlier Scala version. In order to use the batchingExecutionContext in a library, the code needs to fall back toglobal in case theopportunistic field is missing (example below). The resultingExecutionContext has batching behavior in all Scala 2.13 versions (global is batching in 2.13.0-3).

implicit val ec: scala.concurrent.ExecutionContext = try { scala.concurrent.ExecutionContext.getClass   .getDeclaredMethod("opportunistic")   .invoke(scala.concurrent.ExecutionContext)   .asInstanceOf[scala.concurrent.ExecutionContext]} catch { case _: NoSuchMethodException =>   scala.concurrent.ExecutionContext.global}

Application authors can safely use the field because the Scala version at run time is the same as at compile time. Options to bypass the access restriction include:

  1. Using a structural type (example below). This uses reflection at run time.

  2. Writing a Scalaobject in thescala package (example below).

  3. Writing a Java source file. This works becauseprivate[scala] is emitted aspublic in Java bytecode.

// Option 1implicit val ec: scala.concurrent.ExecutionContext = (scala.concurrent.ExecutionContext:   {def opportunistic: scala.concurrent.ExecutionContextExecutor} ).opportunistic// Option 2package scala { object OpportunisticEC {   implicit val ec: scala.concurrent.ExecutionContext =     scala.concurrent.ExecutionContext.opportunistic }}

Attributes

Returns

the globalExecutionContext

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