2

I already know how to use the runtime method to get the number of available processors (in java) but what I need is how to control the number of processors to be used during each round of test or execution in a multicore environment. Put differently, I want to experiment on one node of a multicore computer with 12 processors, but I need to be able to specify at each round of the experiment (execution) how many processors should execute my threaded application. I need to be able to specify the use of 2 cpus, 4 cpus, 6 cpus,8 cpus, 10 cpus and 12 cpus. Any suggestion will be welcome Thanks

ATG
  • 21
  • 2
  • No. Use a threadpool with N threads. Otherwise you need to do it at the OS level. Example for Linux: http://serverfault.com/questions/32322/is-it-possible-to-limit-a-linux-process-so-that-it-can-only-run-on-a-particular – Brian Roach Jun 06 '13 at 13:02
  • It's up to the OS how it will distribute threads to the cores. – NeplatnyUdaj Jun 06 '13 at 13:08
  • Thanks Brain Roach for your reply and the link to the linux example, NeplatnyUdaj thanks for your reply too – ATG Jun 10 '13 at 14:14

2 Answers2

0

Only create as many threads as you want to use the cores (use 1 thread to execute at 1 core).

here is no possibility to limit the number of cores otherwise in Java.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
  • Thanks Uwe Plonus for your response and suggestion, I shall explore the use of threads to take advantage of the cores – ATG Jun 10 '13 at 14:19
0

Use processor affinity for the JVM. Depending on your OS, this may or may not be a trivial task. :-)

For Windows 7, see this: http://www.techrepublic.com/blog/window-on-windows/change-the-processor-affinity-setting-in-windows-7-to-gain-a-performance-edge/5322

For Linux, use this: http://linux.die.net/man/1/taskset

Note: you are setting processor affinity for the JVM, not the Java application!

Also, see this post for a JNI approach and further discussion.

Community
  • 1
  • 1
TFuto
  • 1,361
  • 15
  • 33