3

This is a similar question to the one appearing at: How to ensure Java threads run on different cores. However, there might have been a lot of progress in that in Java, and also, I couldn't find the answer I am looking for in that question.

I just finished writing a multithreaded program. The program spawns several threads, but it doesn't seem to be using more than a single core. The program is faster (I am parallelizing something which makes it faster), but it definitely does not use all cores available, judging by running "top".

Any ideas? Is that an expected behavior?

The general code structure is as following:

   for (some values in i)
   {
        start a thread of instantiated as MyThread(i)
        (this thread uses heavily ConcurrentHashMap and arrays and basic arithmetic, no IO)
        add the thread to a list T
   }

   foreach (thread in T)
   {
        do thread.join()
   }
Community
  • 1
  • 1
kloop
  • 4,537
  • 13
  • 42
  • 66
  • How many threads are you running? How many processors do you have, and how many cores on each processor? – Keith Randall Sep 21 '12 at 17:02
  • Do you have multiple core processors? Check with `Runtime.getRuntime().availableProcessors()` – Amit Deshpande Sep 21 '12 at 17:02
  • Yes. Pinning a thread to a specific core is platform-specific and not exposed by the Java SDK. You would have to use JNI to do it in your Java application. – noahlz Sep 21 '12 at 17:03
  • 4
    Is your code actually CPU bound? If you could provide a short but complete example, that would really help... – Jon Skeet Sep 21 '12 at 17:03
  • 1
    No idea without the code. You might have lock contention problems. – Tudor Sep 21 '12 at 17:04
  • thanks for the all the quick response. @Keith: I am using exactly 10 threads on a 16 CPU machine. AmitD, I believe I do have multiple core processors, but I will check using the command you specified. noahz: I see... so basically the decision is left to the JRE, and in this case it decided not to do it? Jon how do I decide if the code is CPU bound? I will edit the question with some more information about the code. – kloop Sep 21 '12 at 17:09
  • no, the decision is not up to the jre, it's up to the operating system. – jtahlborn Sep 21 '12 at 17:15

2 Answers2

4

If its almost exactly 100% of one CPU, it can mean you really have

  • one core thread which is doing all the work and the others are not doing so much.
  • one resource which you are locking on and only one thread has a chance to run.

If you are using approximately one CPU it can mean this is all the work your CPUs have because you are waiting for something such as IO (network and/or disk)

I suggest you look at the state of your threads in VisualVM. It will help you identify which threads are running and give you an ideal of their pattern of behaviour. I also suggest you use a CPU profiler to help find your bottlenecks.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

I think I read in the SCJP book by Katherine Sierra that JVM's ask the underlying OS to create a new OS thread for every Java thread.

So it's up to the underlying Operating System to decide how to balance Java (and any other kind of) threads between the available CPU's.

enTropy
  • 621
  • 4
  • 14
  • and can I have any kind of control over it? – kloop Sep 21 '12 at 17:20
  • I don't think there is any control over threads inside of Java further than priority assignation. Perhaps with an alternative JVM and some argument tunning? – enTropy Sep 21 '12 at 17:29