0

I am looking for a way to get the number of threads in a JAVA VM, using JAVA code. Any ideas?

Many thanks in advance

  • You can extrapolate it from this [Get a List of all Threads currently running in Java](http://stackoverflow.com/q/1323408/811865) – Jerome Mar 01 '12 at 09:37

2 Answers2

3

Try this:

Thread.getAllStackTraces().size()
jlemos
  • 526
  • 4
  • 13
  • By doing that you retrieve the stacktrace of every running Thread. That could be annoyingly slow if you had hundreds of threads, see [Get a List of all Threads currently running in Java](http://stackoverflow.com/q/1323408/811865) – Jerome Mar 01 '12 at 09:41
3

I think, you can also get it using JMX using ThreadMXBean management interface. Here is a sample.

Nikunj
  • 3,100
  • 2
  • 20
  • 19