The number of future threads in Scala

Source: Internet
Author: User

Why does the future have a maximum of 4 concurrent threads? The number of threads in a thread pool is determined by ExecutionContext. If you are using the default global, there are only 4 concurrent threads. Import Scala.concurrent.ExecutionContext.Implicits.global

where does the default global ExecutionContext set 4 concurrent threads? Global uses Executioncontextimpl, which has a code like this:
  
 
  1. def createExecutorService: ExecutorService = {
  2. def GetInt ( name : string , f Span class= "pun" >: string => int ): int =
  3. /span>try f ( system getproperty ( name catch { Span class= "PLN" > case e : exception => Span class= "PLN" > runtime getruntime availableprocessors
  4. def range(floor: Int, desired: Int, ceiling: Int): Int =
  5. if (Ceiling< Floor)Range(Ceiling,desired, Floor) ElseScala.Math.min(Scala.Math.Max(desired, Floor),Ceiling)
  6. val desiredParallelism = range(
  7. getInt("scala.concurrent.context.minThreads", _.toInt),
  8. getInt("scala.concurrent.context.numThreads", {
  9. case null | "" => Runtime.getRuntime.availableProcessors
  10. Casesifs.charAt(0) == ' x ' = (Runtime.GetRuntime.availableprocessors*s.substring(1).todouble).Ceil.ToInt
  11. case other => other.toInt
  12. }),
  13. getInt("scala.concurrent.context.maxThreads", _.toInt))
  14. val threadFactory = new DefaultThreadFactory(daemonic = true)
  15. try {
  16. new ForkJoinPool(
  17. desiredParallelism,
  18. threadFactory,
  19. uncaughtExceptionHandler,
  20. true) // Async all the way baby
  21. } catch {
MakersForkjoinpoolwhen you set thedesiredparallelism. You can see how much parallelism the Desiredparallelism function is based on system variables (note the Getint function):scala.concurrent.context.minThreads:minimum number of concurrent threads (INT)
scala.concurrent.context.numThreads:the number of concurrent threads, and if it is int, this value is used, and if it is a string and starts with "X" followed by a double (such as "x1.5"), its value is 1.5 *Runtime.getRuntime.availableProcessors
scala.concurrent.context.maxThreads:Maximum number of concurrent threads(INT)

If these three variables are not set, Getint will take Runtime.getRuntime.availableProcessors, which is the current CPU's number of cores. So there are only 4 concurrent threads running the future on my computer.
How do I change the number of concurrent threads for the future? 1. From the above code analysis it is easy to think that if you are still using global executioncontext, you can modify the system variables:
 
   
  
  1. System.setProperty("scala.concurrent.context.minThreads", "8")
  2. System.setProperty("scala.concurrent.context.maxThreads", "8")

2. A better approach is to rewrite one's own executioncontext.
  
 
  1. import java.util.concurrent.Executors
  2. import scala.concurrent._
  3. implicit val ec = new ExecutionContext {
  4. val threadPool = Executors.newFixedThreadPool(1000);
  5. def execute(runnable: Runnable) {
  6. threadPool.submit(runnable)
  7. }
  8. def reportFailure(t: Throwable) {}
  9. }









From for notes (Wiz)

The number of future threads in Scala

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.