美文网首页
Java 获取所有线程

Java 获取所有线程

作者: 西贝巴巴 | 来源:发表于2021-03-17 08:47 被阅读0次
package com.company;

/*
使用 getName() 方法获取所有正在运行的线程
*/
public class GetNameTest extends Thread {
    public static void main(String[] args) {
        GetNameTest g = new GetNameTest();
        g.setName("thread1");
        g.start();

        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);
        for (int i = 0; i < noThreads; i++) {
            System.out.println("线程号:" + i + " = " + lstThreads[i].getName());
        }
    }
}

相关文章

网友评论

      本文标题:Java 获取所有线程

      本文链接:https://www.haomeiwen.com/subject/xtlcqltx.html