Skip to content

Commit

Permalink
Introduces ExecutionContext.opportunistic as a private[scala] instanc…
Browse files Browse the repository at this point in the history
…e as an escape-hatch for getting the original 2.13.0 batching global EC
  • Loading branch information
viktorklang committed Oct 29, 2020
1 parent 200c122 commit 48bd47a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/library/scala/concurrent/ExecutionContext.scala
Expand Up @@ -166,6 +166,25 @@ object ExecutionContext {
override final def reportFailure(t: Throwable): Unit = defaultReporter(t)
}

/**
* This `ExecutionContext` is ideally suited to execute short-lived tasks on the `global` `ExecutionContext` as it
* attempts to locally batch the execution of nested tasks.
*
* WARNING: long-running and/or blocking tasks should be demarcated within `scala.concurrent.blocking`-blocks,
* to ensure that any pending tasks in the current batch can be executed by another thread on `global`.
*/
private[scala] object opportunistic extends ExecutionContextExecutor with BatchingExecutor {
final override def submitForExecution(runnable: Runnable): Unit = global.execute(runnable)

final override def execute(runnable: Runnable): Unit =
if ((!runnable.isInstanceOf[impl.Promise.Transformation[_,_]] || runnable.asInstanceOf[impl.Promise.Transformation[_,_]].benefitsFromBatching) && runnable.isInstanceOf[Batchable])
submitAsyncBatched(runnable)
else
submitForExecution(runnable)

override final def reportFailure(t: Throwable): Unit = global.reportFailure(t)
}

object Implicits {
/**
* The implicit global `ExecutionContext`. Import `global` when you want to provide the global
Expand Down

0 comments on commit 48bd47a

Please sign in to comment.