Java Multithreading - Assign threads to processor cores

10,044

Solution 1

You cannot assign threads to cores.

  1. Java7's fork/join framework addresses exactly the same problem. Automatically though (It will be designed for multi-core processors).

  2. What you can do is to set Thread priority to prioritize your threads, if that's what you want to achieve.

  3. 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

Share:
10,044
KLCoder
Author by

KLCoder

Updated on June 28, 2022

Comments

  • KLCoder
    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
    gd1 almost 13 years
    Maybe with JNI you can tweak it, but it's really pointless. Agree.
  • KLCoder
    KLCoder almost 13 years
    so it means there is no way I can have a control over that?
  • gd1
    gd1 almost 13 years
    Not the JVM, the OS. @KLCoder: maybe with JNI, but you don't need it
  • Bozho
    Bozho almost 13 years
    First the JVM, then the OS :) I clarified
  • zengr
    zengr almost 13 years
    Looking forward to fork/join though.
  • KLCoder
    KLCoder almost 13 years
    another approach can be setting the processor affinity
  • zengr
    zengr almost 13 years
    You cannot play with processor affinity from Java. JVM handles that for you.
  • zengr
    zengr almost 13 years
    There you go, some JNI code which helps you set CPU affinity: blog.toadhead.net/index.php/2011/01/22/…
  • gd1
    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
    KLCoder almost 13 years
    can I implement this behavior using executors?
  • zengr
    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
    Ajhar Shaikh almost 7 years
    @zengr It seems JNI blog doesn't exist there anymore
  • zengr
    zengr almost 7 years
    You can look into this lib, it uses JNI github.com/OpenHFT/Java-Thread-Affinity
  • zengr
    zengr almost 7 years
    Technically 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