- public static void printThreadList() {
- ThreadGroup group = Thread.currentThread().getThreadGroup();
- while (group.getParent() != null) {
- group = group.getParent();
- }
- Thread[] threads = new Thread[group.activeCount()];
- group.enumerate(threads);
- for (Thread thread : threads) {
- if (thread == null) {
- continue;
- }
- try {
- StringBuffer buf = new StringBuffer();
- ThreadGroup tgroup = thread.getThreadGroup();
- String groupName = tgroup == null ? "null" : tgroup.getName();
- buf.append("ThreadGroup:").append(groupName).append(", ");
- buf.append("Id:").append(thread.getId()).append(", ");
- buf.append("Name:").append(thread.getName()).append(", ");
- buf.append("isDaemon:").append(thread.isDaemon()).append(", ");
- buf.append("isAlive:").append(thread.isAlive()).append(", ");
- buf.append("Priority:").append(thread.getPriority());
- System.out.println(buf.toString());
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/231020136590.html
来源: http://www.codesnippet.cn/detail/231020136590.html