0

If I am not wrong, JVM relies on underlying architecture of OS for scheduling and spreading threads to other cores (when a thread is spawned). However, in my application, I don't see it happening. Each and every thread is running on 0th core and rest N-1 cores are in idle states. Any specific reason why this is happening and, more over, how to solve this issue so that I can utilize rest of my cores ?

I am aware of setting process affinity but that is, in a way, hardcoding a process to a specific core. Since a thread can be spawned at any given point, it should idly be processed by a core sitting idle.

Code Snippet e.g.

public static void main() {

R1 r1 = new R1();
R2 r2 = new R2();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);

t1.start();
t2.start();
}

Any suggestion ?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • 2
    Hi! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) We can't help you with code we can't see. :-) Please update your question with a [mcve] demonstrating the problem of using only a single core. (The most likely explanation -- all due respect! -- is that you're not starting the threads correctly.) Please also include details of your configuration (OS, etc.). – T.J. Crowder Sep 04 '18 at 08:03
  • 1
    Hi, would be nice to see the code you are using. The following post could be useful: https://stackoverflow.com/questions/8579657/whats-the-difference-between-thread-start-and-runnable-run – Ametzaga Sep 04 '18 at 08:12
  • Hard to answer without given code. Just a quick guess: Are you by chance using `Thread.run()` instead `Thread.start()`? – Markus Bauer Sep 04 '18 at 08:13
  • @Ametzaga,@MarkusBauer: it is Thread.start(). – Ashish Goel Sep 04 '18 at 09:14
  • how do you see which core is busy and which are idle? – Alexei Kaigorodov Sep 04 '18 at 10:11
  • who told you java will run your threads on separate cores? – ACV Sep 04 '18 at 11:43
  • @AlexeiKaigorodov used `top` and `sysstat` commands. – Ashish Goel Sep 04 '18 at 12:46
  • @ACV Doesn't it? :) – Ashish Goel Sep 04 '18 at 12:47
  • the java language specification doesn't enforce it. Also remember a thread is not a process. – ACV Sep 04 '18 at 13:17
  • @AVC Yes I am aware of it. The essence of the problem is that thread should be running on separate core which is idle but it doesn't happen with my application. – Ashish Goel Sep 04 '18 at 14:25

1 Answers1

1

There is almost certainly something about your application that is causing the JVM not to be able to run more than one application thread at a time.

The problem will be in the R1 and R2 classes, or something that they depend on. Since you have decided not to show them to us, we can only guess what it might be. However a common explanation is that one of the threads (running your code) is holding a lock that the other thread is trying to acquire. That can be sufficient to make your application effectively single threaded.

Advice:

  • Use the jstack utility to get a thread dump of the application, and look at the stacktraces to see what the threads are doing, and why one is blocked.

  • Run the application using a debugger, etcetera.

  • Don't waste your time on investigating the scheduler, thread affinity or stuff like that. They are unlikely to help you to solve the problem.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for the input. Most of them are asynchronous calls/threads running in parallel (not necessarily dependent on each other or on one specific resource). But even so, they are running on only one core. This baffles me. I am sure I am missing something at configuration level and it could be a silly mistake. Regarding class structure, it is pretty much the same (I have written a blue print). I will try to debug again and will also explore `jstack` utility but is there any more possibility for such weird behavior ? Kindly bump this question to get more views from others. – Ashish Goel Sep 04 '18 at 12:42
  • The most likely cause of your problems is your code. Without seeing it (or an MCVE that reproduces the behavior), I can't speculate beyond what I have already said. I don't think anyone else will be able to help you either. – Stephen C Sep 05 '18 at 00:28