gpt4 book ai didi

scala 全局 ExecutionContext 与 ExecutionContextExecutorService

转载 作者:行者123 更新时间:2023-12-03 19:35:31 24 4
gpt4 key购买 nike

我正在使用具有默认全局上下文和我自己的 Scala Futures ExecutionContext .

我很好奇如何global context在所有执行后关闭。因为如果我创建自己的 ExecutionContext我必须手动关机。

例子,

1) 使用全局 executionContext,

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}

object ParallelTasksWithGlobalExecutionContext {

private val data: Iterable[Input] = Iterable("data1", "data2", "data3")

def main(args: Array[String]): Unit = {

val f: Future[Unit] = Future.traverse(data) { d =>

println(s"[${Thread.currentThread().getName}]-Firing $d")
processData(d)

} map { processed =>
processed.foreach(p => println(s"""[${Thread.currentThread().getName}]-$p"""))
}

Await.result(f, Duration.Inf)
}

type Input = String
type Output = String

def processData: (Input => Future[Output]) = data => {
Future {
Thread.sleep(5000)
s"[Thread-${Thread.currentThread().getName}] data $data is processed."
}
}
}

输出
$ sbt "runMain ParallelTasksWithGlobalExecutionContext"
[info] Running ParallelTasksWithGlobalExecutionContext
[run-main-0]-Firing data1
[run-main-0]-Firing data2
[run-main-0]-Firing data3
[scala-execution-context-global-59]-[Thread-scala-execution-context-global-58] data data1 is processed.
[scala-execution-context-global-59]-[Thread-scala-execution-context-global-59] data data2 is processed.
[scala-execution-context-global-59]-[Thread-scala-execution-context-global-60] data data3 is processed.
[success] Total time: 6 s, completed Apr 1, 2018 12:44:36 AM

执行完成后,应用程序终止。

2) 使用自己的 ExecutionContext - 在我手动完成所有执行后,应用程序不会终止 .shutdown .
import java.util.concurrent.Executors

import scala.concurrent.{ExecutionContext, ExecutionContextExecutorService, Future}
import scala.util.{Failure, Success}

object ParallelTasksWithCustomExecutionContext {

private val data: Iterable[Input] = Iterable("data1", "data2", "data3")
implicit val singleThreadContext: ExecutionContextExecutorService = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(3))

def main(args: Array[String]): Unit = {

Future.traverse(data) { d =>

println(s"[${Thread.currentThread().getName}]-Firing $d")
processData(d)

} onComplete {
case Success(processed) =>
processed.foreach(p => println(s"""[${Thread.currentThread().getName}]-$p"""))
//singleThreadContext.shutdown()
case Failure(f) =>
f.printStackTrace()
//singleThreadContext.shutdown()
}

}

type Input = String
type Output = String

def processData: (Input => Future[Output]) = data => {
Future {
Thread.sleep(5000)
s"[Thread-${Thread.currentThread().getName}] data $data is processed."
}
}
}

输出
$ sbt "runMain ParallelTasksWithCustomExecutionContext"
[info] Running ParallelTasksWithCustomExecutionContext
[run-main-0]-Firing data1
[run-main-0]-Firing data2
[run-main-0]-Firing data3
[pool-7-thread-1]-[Thread-pool-7-thread-1] data data1 is processed.
[pool-7-thread-1]-[Thread-pool-7-thread-2] data data2 is processed.
[pool-7-thread-1]-[Thread-pool-7-thread-3] data data3 is processed.
<hangs>

这是 JVisualVM 线程监视器,

jvisual-vm

我的问题是 scala 的全局上下文如何在不询问客户端的情况下自动终止?

最佳答案

斯卡拉 global上下文是使用 Thread 创建的制造工厂Threads守护进程,因此一旦所有用户线程完成执行,它们(线程)将不会阻止 JVM 退出。

查询 ExecutionContextImpl方法 def createDefaultExecutorService(reporter: Throwable => Unit): ExecutorService .

关于scala 全局 ExecutionContext 与 ExecutionContextExecutorService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com