Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java 7 fork/join guarantees executing thread in seperate CPU

Recently, I came to know about the Java 7 fork/join framework - what I learned is that it could be useful for divide-and-conquer like problems.

My question is, does the framework guarantees executing threads on separate CPUs? Or is it event possible to instruct the threads I create using classes of concurrent package to run on separate CPUs available in my server?

like image 766
King J Avatar asked May 07 '13 03:05

King J


2 Answers

It'll be built upon the standard JVM concurrency primitives, in which case they will (eventually) be scheduled onto real OS threads. You cannot guarantee that your OS scheduler is going to schedule threads onto separate CPUS, although it's quite likely in most instances.

Trying to guess what a concurrent scheduler is going to do at runtime is a really bad idea. Just assume that you will be able to make use of no more than as many CPUs as you have active threads, and don't try to second-guess the runtime behaviour unless you're trying to do a particular kind of very low-level optimisation.

like image 170
Gian Avatar answered Nov 13 '22 09:11

Gian


At least it will do its best. The fork/join framework is designed to take advantage of multiple processors. By default ForkJoinPool is created with the number of worker threads equal to the number of processors.

like image 21
Evgeniy Dorofeev Avatar answered Nov 13 '22 11:11

Evgeniy Dorofeev