Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 为什么我的阻塞期货不在单核上运行?_Multithreading_Scala_Future - Fatal编程技术网

Multithreading 为什么我的阻塞期货不在单核上运行?

Multithreading 为什么我的阻塞期货不在单核上运行?,multithreading,scala,future,Multithreading,Scala,Future,当我在只有一个内核的低功耗服务器上运行以下(简化)代码时: implicit val context: ExecutionContextExecutor = scala.concurrent.ExecutionContext.global Future(blocking { while (true) { java.lang.Thread.sleep(1000) println("thread 1") } }) while (true) { ja

当我在只有一个内核的低功耗服务器上运行以下(简化)代码时:

implicit val context: ExecutionContextExecutor = scala.concurrent.ExecutionContext.global

Future(blocking {
  while (true) {
    java.lang.Thread.sleep(1000)
    println("thread 1")
  }
})

while (true) {
  java.lang.Thread.sleep(1000)
  println("main")
}
日志中只显示“main”。如果我增加服务器以拥有更多的内核,那么它就可以工作了。我做错了什么?如何让Scala/Java在内核有限的情况下运行每个线程

我的理解是,运行时应该使用一些逻辑来执行一个线程一段时间,然后切换到另一个线程

scalaVersion := "2.12.12"

经过一番尝试后,我发现如果我对我的EC使用
ExecutionContext.fromExecutor(Executors.newFixedThreadPool(30))
,它就可以工作了。因此,我对全局EC和阻塞的理解一定是错误的。

我无法复制这个问题。使用
ExecutionContext.global
时,即使在单个内核上,也应该至少有一个线程可用,因为默认计算是

numThreads = Runtime.getRuntime.availableProcessors * 1 
诸如此类

Future(blocking {
  while (true) {
    java.lang.Thread.sleep(1000)
    println("thread 1")
  }
})
应该在该线程中执行

while (true) {
  java.lang.Thread.sleep(1000)
  println("main")
}
应该在主线程中执行

注意,即使只有一个线程可用,如果您始终使用
阻塞{}
,那么新线程仍会生成到
maxexthreads

scala.concurrent.context.maxExtraThreads = defaults to "256"
因此,问题可能出在其他地方