move code to correct place

add Thread state related code
This commit is contained in:
2025-02-20 14:17:35 +08:00
parent 2a2707c4e7
commit 400981a341
7 changed files with 64 additions and 5 deletions

View File

@@ -0,0 +1,20 @@
package threaddemo;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
public class MultiThreadDemo {
public static void main(String[] args) {
ThreadMXBean threadMXBean = java.lang.management.ManagementFactory.getThreadMXBean();
// 不需要获取同步的 monitor 和 synchronizer 信息,仅获取线程和线程堆栈信息
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
// 遍历线程信息,仅打印线程 ID 和线程名称信息
for (ThreadInfo threadInfo : threadInfos) {
System.out.println(STR."[\{threadInfo.getThreadId()}] \{threadInfo.getThreadName()}; Thread state: \{threadInfo.getThreadState()} ; Is daemon: \{threadInfo.isDaemon()}"
);
}
}
}