I am willing to run another jar file and get its threads, so I thought running the jar this way will run the jar on the current JVM:
Process p = Runtime.getRuntime().exec("java -jar " + path);
And then to get the threads like this:
ThreadGroup rootThreadGroup = null;
ThreadGroup getRootThreadGroup( ) {
if ( rootThreadGroup != null )
return rootThreadGroup;
ThreadGroup tg = Thread.currentThread( ).getThreadGroup( );
ThreadGroup ptg;
while ( (ptg = tg.getParent( )) != null )
tg = ptg;
return tg;
}
Thread[] getAllThreads( ) {
final ThreadGroup root = getRootThreadGroup( );
final ThreadMXBean thbean = ManagementFactory.getThreadMXBean( );
int nAlloc = thbean.getThreadCount( );
int n = 0;
Thread[] threads;
do {
nAlloc *= 2;
threads = new Thread[ nAlloc ];
n = root.enumerate( threads, true );
} while ( n == nAlloc );
return java.util.Arrays.copyOf( threads, n );
}
But it is not showing the new jar threads I ran before, even though I put a Thread.sleep(4000);
line to wait a little bit.
Isn't that suppose to get all the threads running in JVM?
code source