Is there any maximum limit to number of running threads in Linux? Is it possible to count total number of threads from all classes in java?
Asked
Active
Viewed 244 times
-1
-
2do you mean: http://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux ? – Gavriel Feb 08 '16 at 11:27
-
Some nice answers already provided [here](http://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support). – dstronczak Feb 08 '16 at 11:34
1 Answers
1
Technically you should use 2 X numberOfPhysicalCores threads for optimal performance. This post describes the maximum number of threads (Thanks to @gavriel for finding it :P)
Next, to get the number of running threads in the current JVM:
public static void main(String[] args) throws IOException, InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(4);
service.execute(new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
System.out.println(Thread.getAllStackTraces().size());
}
O/P : 5

Community
- 1
- 1

TheLostMind
- 35,966
- 12
- 68
- 104
-
1While the maximum number is much higher, the practical limit is once you have enough threads to keep all your cores busy, adding more threads is likely to just add overhead and slow down your application. +1 – Peter Lawrey Feb 08 '16 at 13:37
-
1@PeterLawrey - I would rather have your answer here than your vote :).. Its a pity this question has been closed – TheLostMind Feb 08 '16 at 13:39
-
I am not sure how the Thread.getAllStackTraces() relates through. – Peter Lawrey Feb 08 '16 at 13:43
-
A shorter example of your code. `IntStream.range(0, 4).parallel().forEach(i -> LockSupport.parkNanos(1_000_000_000));` – Peter Lawrey Feb 08 '16 at 13:44
-
1@PeterLawrey - The OP was asking about getting all the threads running in the JVM. Doesn't `Thread.getAllStackTraces()` do that? – TheLostMind Feb 08 '16 at 14:52
-
1You are right. I missed that there was two questions so you answer makes sense. Do you really only have 5 threads including GC/JVM system threads? – Peter Lawrey Feb 08 '16 at 15:35
-
-
@PeterLawrey - I am curious. Can you please explain the code you have added in your comment?.. And how it is related to the question ? :) – TheLostMind Feb 08 '16 at 15:39
-
I see, I didn't realize this method only gives you Java threads with a stack trace. It doesn't show you all the threads as `jstack` does. – Peter Lawrey Feb 08 '16 at 15:39
-
I was making the point that the code does the same thing as your ExecutorService + loop. It's a shorter version. It doesn't give you the number of threads. – Peter Lawrey Feb 08 '16 at 15:41
-
`System.out.println(Thread.getAllStackTraces().keySet());` alone for me prints `[Thread[Monitor Ctrl-Break,5,main], Thread[Reference Handler,10,system], Thread[Attach Listener,5,system], Thread[Signal Dispatcher,9,system], Thread[Finalizer,8,system], Thread[main,5,main]] ` which is why I was surprised you only get 5. – Peter Lawrey Feb 08 '16 at 15:41
-
@PeterLawrey - Oh I see. Interestingly I distinctly remember seeing the same number of threads a few days back.. I think the numbe rof threads depends on the state of the JVM (what code it is running / it was running) – TheLostMind Feb 08 '16 at 15:43
-
-
-
@PeterLawrey - BTW.. long time no see on SO?.. Busy with life or work? :) – TheLostMind Feb 08 '16 at 15:53
-
I have have been busy with work. I am also working in a US timezone a lot of the time lately. ;) – Peter Lawrey Feb 08 '16 at 15:55
-
1