标签:
为什么Future最大只有4个并发线程?
def createExecutorService: ExecutorService = {
def getInt(name: String, f: String => Int): Int =
try f(System.getProperty(name)) catch { case e: Exception => Runtime.getRuntime.availableProcessors }
def range(floor: Int, desired: Int, ceiling: Int): Int =
if (ceiling < floor) range(ceiling, desired, floor) else scala.math.min(scala.math.max(desired, floor), ceiling)
val desiredParallelism = range(
getInt("scala.concurrent.context.minThreads", _.toInt),
getInt("scala.concurrent.context.numThreads", {
case null | "" => Runtime.getRuntime.availableProcessors
case s if s.charAt(0) == ‘x‘ => (Runtime.getRuntime.availableProcessors * s.substring(1).toDouble).ceil.toInt
case other => other.toInt
}),
getInt("scala.concurrent.context.maxThreads", _.toInt))
val threadFactory = new DefaultThreadFactory(daemonic = true)
try {
new ForkJoinPool(
desiredParallelism,
threadFactory,
uncaughtExceptionHandler,
true) // Async all the way baby
} catch {
System.setProperty("scala.concurrent.context.minThreads", "8")
System.setProperty("scala.concurrent.context.maxThreads", "8")
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) {}
}
标签:
原文地址:http://my.oschina.net/guanxun/blog/482089