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
Thread allocation is managed by the operating system. Threads are created using OS system calls and, if the process happens to run on a multi-core processor, the OS automatically tries to allocate / schedule different threads on different cores. Thread allocation is managed by the programming language implementation.
Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores.
you can use GOMP_CPU_AFFINITY variable and set which thread to use which CPU core. For example, if you have 16 cores, and use only 4 threads, you can set to execute to cores 0-3, or 0, 4, 8, 12.
A core provides one or more threads. However, if a CPU core exposes multiple threads (typically only 2), the expectation is that the threads do contend with each other for execution resources. The idea with multi-threaded cores (commonly called hyperthreading) is that cores have lots of execution resources.
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).
I think the simple answer here would be you just can't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With