1.ThreadGroup
System
.out
.println("hello");
ThreadGroup group
= Thread
.currentThread().getThreadGroup();
ThreadGroup topGroup
= group
;
while (group
!= null
) {
topGroup
= group
;
group
= group
.getParent();
}
int nowThreads
= topGroup
.activeCount();
Thread
[] lstThreads
= new Thread[nowThreads
];
topGroup
.enumerate(lstThreads
);
for (int i
= 0; i
< nowThreads
; i
++) {
System
.out
.println("线程number:" + i
+ " = " + lstThreads
[i
].getName());
}
topGroup.enumerate(lstThreads); enumerate方法用来将ThreadGroup线程组中的active线程全部复制到Thread类型的数组中,并且返回数组中元素个数,即线程组中active线程数量。 enumerate方法详解
2.MXBean方式
System
.out
.println("below is thread info:");
ThreadMXBean threadMXBean
= ManagementFactory
.getThreadMXBean();
long[] threadIds
= threadMXBean
.getAllThreadIds();
ThreadInfo
[] threadInfos
= threadMXBean
.getThreadInfo(threadIds
);
for (ThreadInfo threadInfo
: threadInfos
) {
System
.out
.println(threadInfo
.getThreadId()+": "+threadInfo
.getThreadName());
}
3.打断点方式查看