0

我愿意运行另一个 jar 文件并获取它的线程,所以我认为以这种方式运行 jar 会在当前 JVM 上运行 jar:

Process p = Runtime.getRuntime().exec("java -jar " + path);

然后得到这样的线程:

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 );
}

但它没有显示我之前运行的新 jar 线程,即使我放了一条Thread.sleep(4000);线等待一点。
这不是假设让所有线程都在 JVM 中运行吗?
代码源

4

0 回答 0