Java Multithreading - Assign threads to processor cores
Solution 1
You cannot assign threads to cores.
Java7's fork/join framework addresses exactly the same problem. Automatically though (It will be designed for multi-core processors).
What you can do is to set Thread priority to prioritize your threads, if that's what you want to achieve.
JNI might be another direction to export, but an overkill I guess. You can look at Peter Lawrey's Java-Thread-Affinity which uses JNI (I haven't used it).
Solution 2
I think the simple answer here would be you just can't.
Solution 3
I don't think it's easy for you to explicitly assign threads to cores. Maybe you can use native instructions, but I doubt it can be done.
And unless you're doing some special benchmark, you really don't need to. Java threads are backed by native threads, and if the O.S. is modern enough, its kernel will dynamically assign (and re-assign) threads to cores in a way that is certainly better than yours, because it (hopefully) does load balancing.
Start some threads that perform long computations and then see processor usage from a task manager. You'll see many cores being used.
Solution 4
See Java Thread Affinity project.
Lock to CPU
try (AffinityLock al = AffinityLock.acquireLock()) {
// do some work while locked to a CPU.
}
Lock core:
try (AffinityLock al = AffinityLock.acquireCore()) {
// do some work while locked to a CPU.
}
Solution 5
The OS manages what threads are processed on what core. You will need to assign the threads to a single core in the OS.
For instance. On windows, open task manager, go to the processes tab and right click on the java processes... then assign them to a specific core.
That is the best you are going to get.
you can assign thread priority as per your requirement
KLCoder
Updated on June 28, 2022Comments
-
KLCoder almost 2 years
I am writing an application in java which involves parallel computing. My question is how can I explicitly assign threads to cores? What is the programming logic for it?
Can anyone tell why class Executor is used? Thanks
-
gd1 almost 13 yearsMaybe with JNI you can tweak it, but it's really pointless. Agree.
-
KLCoder almost 13 yearsso it means there is no way I can have a control over that?
-
gd1 almost 13 yearsNot the JVM, the OS. @KLCoder: maybe with JNI, but you don't need it
-
Bozho almost 13 yearsFirst the JVM, then the OS :) I clarified
-
zengr almost 13 yearsLooking forward to fork/join though.
-
KLCoder almost 13 yearsanother approach can be setting the processor affinity
-
zengr almost 13 yearsYou cannot play with processor affinity from Java. JVM handles that for you.
-
zengr almost 13 yearsThere you go, some JNI code which helps you set CPU affinity: blog.toadhead.net/index.php/2011/01/22/…
-
gd1 almost 13 years@zengr +1. But KLCoder don't do that. It means you'll have to maintain native code, which compiles different under each platform/architecture combination. It is crazy for doing something you don't need (and should) do.
-
KLCoder almost 13 yearscan I implement this behavior using executors?
-
zengr almost 13 years@Giacomo I agree. This responsibility should be with the JVM. @KLCoder Executors are just ways to lunch new tasks. This "behavior" cannot use executors.
-
Ajhar Shaikh almost 7 years@zengr It seems JNI blog doesn't exist there anymore
-
zengr almost 7 yearsYou can look into this lib, it uses JNI github.com/OpenHFT/Java-Thread-Affinity
-
zengr almost 7 yearsTechnically incorrect, you can. It's just that you shouldn't unless you exactly know what you are doing. Example: github.com/OpenHFT/Java-Thread-Affinity