5

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KLCoder
  • 89
  • 2
  • 3
  • 11

7 Answers7

9

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

zengr
  • 38,346
  • 37
  • 130
  • 192
  • Looking forward to fork/join though. – zengr May 04 '11 at 06:33
  • another approach can be setting the processor affinity – KLCoder May 04 '11 at 06:35
  • You cannot play with processor affinity from Java. JVM handles that for you. – zengr May 04 '11 at 06:43
  • 2
    There you go, some JNI code which helps you set CPU affinity: http://blog.toadhead.net/index.php/2011/01/22/cputhread-affinity-in-java/ – zengr May 04 '11 at 06:44
  • @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. – gd1 May 04 '11 at 06:54
  • can I implement this behavior using executors? – KLCoder May 04 '11 at 07:00
  • @Giacomo I agree. This responsibility should be with the JVM. @KLCoder Executors are just ways to lunch new tasks. This "behavior" cannot use executors. – zengr May 04 '11 at 07:08
  • @zengr It seems JNI blog doesn't exist there anymore – Ajhar Shaikh May 23 '17 at 18:22
  • You can look into this lib, it uses JNI https://github.com/OpenHFT/Java-Thread-Affinity – zengr May 23 '17 at 20:02
5

I think the simple answer here would be you just can't.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • 1
    *Maybe* with JNI you can tweak it, but it's really pointless. Agree. – gd1 May 04 '11 at 06:26
  • Technically incorrect, you can. It's just that you shouldn't unless you exactly know what you are doing. Example: https://github.com/OpenHFT/Java-Thread-Affinity – zengr May 23 '17 at 20:05
3

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.
}
rgrebski
  • 2,354
  • 20
  • 29
2

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.

gd1
  • 11,300
  • 7
  • 49
  • 88
2

You can't. See this article. The JVM will delegate this to the OS, which will handle it for you.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
2

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

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
1

As said, the JVM won't let you. But first, you should ask yourself why you're thinking about assigning threads to cores. This is probably not what you want to do.

The Executor class is used to schedule medium-sized "grains" of computation without incurring the overhead of creating too many threads. You may also want to try using parallel branches for a much more fine-grained scheduling, here are some code samples: http://www.ateji.com/px/codesamples.html

Patrick
  • 31
  • 1