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()
   }
7
  • How many threads are you running? How many processors do you have, and how many cores on each processor? Commented Sep 21, 2012 at 17:02
  • Do you have multiple core processors? Check with Runtime.getRuntime().availableProcessors() Commented Sep 21, 2012 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. Commented Sep 21, 2012 at 17:03
  • 4
    Is your code actually CPU bound? If you could provide a short but complete example, that would really help... Commented Sep 21, 2012 at 17:03
  • 1
    No idea without the code. You might have lock contention problems. Commented Sep 21, 2012 at 17:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

and can I have any kind of control over it?
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.