In ForkJoinPool for Scala's “Future”, why is the id for the worker always an odd number?

Here are the codes:

import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.concurrent.duration._


val is = 1 to 100 toList
def db = s"${Thread.currentThread}"
def f(i: Int) = Future { println(db) ; 2 * i }

val theFuture = Future.traverse(is)(f _)

Await.result(theFuture, 10.seconds)

I ran it many times and the result is as follows:

Thread[ForkJoinPool-1-worker-3,5,main]
Thread[ForkJoinPool-1-worker-3,5,main]
Thread[ForkJoinPool-1-worker-3,5,main]
Thread[ForkJoinPool-1-worker-3,5,main]
Thread[ForkJoinPool-1-worker-3,5,main]
Thread[ForkJoinPool-1-worker-1,5,main]
Thread[ForkJoinPool-1-worker-5,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-7,5,main]
Thread[ForkJoinPool-1-worker-3,5,main]

The sample is always "Thread [ForkJoinPool-1-worker -" $ {AnOddNumber} ", 5, main]". Does anyone have any ideas on why an id for a worker is always an odd number, not an even number?

+4
source share
1 answer

You are using the execution context ExecutionContext.Implicits.global. Under the hood is used ForkJoinPoolto handle work flows. This one has ForkJoinPoolbeen branched by the developers of the Scala library to modify it according to their needs. You can find it here .

. registerWorker. ( workerNamePrefix, "ForkJoinPool-${POOL_ID}-worker-") , (. 1712). , , . , , - ( . -, ).

, 1, 3, 5 7 , , , 4- . , , , . :

def f(i: Int) = Future { println(db); Thread.sleep(100); 2 * i }

, !

+2

All Articles