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());
}
}
}
网友评论