multithreading 如何为期货配置微调的线程池?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15285284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure a fine tuned thread pool for futures?
提问by Josh Gao
How large is Scala's thread pool for futures?
Scala 的期货线程池有多大?
My Scala application makes many millions of future {}
s and I wonder if there is anything I can do to optimize them by configuring a thread pool.
我的 Scala 应用程序产生了数百万个future {}
s,我想知道是否可以通过配置线程池来优化它们。
Thank you.
谢谢你。
回答by Bienvenido David
This answer is from monkHyman, a comment from the accepted answer. However, one can miss this great answer so I'm reposting it here.
这个答案来自monkHyman,来自已接受答案的评论。然而,人们可能会错过这个很好的答案,所以我在这里重新发布。
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))
If you just need to change the thread pool count, just use the global executor and pass the following system properties.
如果只需要更改线程池计数,只需使用全局执行器并传递以下系统属性即可。
-Dscala.concurrent.context.numThreads=8 -Dscala.concurrent.context.maxThreads=8
回答by Josh Gao
You can specify your own ExecutionContext that your futures will run in, instead of importing the global implicit ExecutionContext.
您可以指定自己的期货将在其中运行的 ExecutionContext,而不是导入全局隐式 ExecutionContext。
import java.util.concurrent.Executors
import scala.concurrent._
implicit val ec = new ExecutionContext {
val threadPool = Executors.newFixedThreadPool(1000)
def execute(runnable: Runnable) {
threadPool.submit(runnable)
}
def reportFailure(t: Throwable) {}
}
回答by Neeraj Bansal
best way to specify threadpool in scala futures:
在 Scala 期货中指定线程池的最佳方法:
implicit val ec = new ExecutionContext {
val threadPool = Executors.newFixedThreadPool(conf.getInt("5"));
override def reportFailure(cause: Throwable): Unit = {};
override def execute(runnable: Runnable): Unit = threadPool.submit(runnable);
def shutdown() = threadPool.shutdown();
}
回答by VAIBHAV GOUR
class ThreadPoolExecutionContext(val executionContext: ExecutionContext)
object ThreadPoolExecutionContext {
val executionContextProvider: ThreadPoolExecutionContext = {
try {
val executionContextExecutor: ExecutionContextExecutor = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(25))
new ThreadPoolExecutionContext(executionContextExecutor)
} catch {
case exception: Exception => {
Log.error("Failed to create thread pool", exception)
throw exception
}
}
}
}