线程的各种异常信息

import java.io.*;

public class ThreadInfo {
	static PrintWriter out = new PrintWriter(System.out, true);
	public static void main(String[] args) {
		Thread[] threads = new Thread[4];
		ThreadGroup group = new ThreadGroup ("MyThreadGroup");
		if(args.length > 0) {
			Thread thread = Thread.currentThread();
			thread.setName(args[0]);	
		}
		for(int i = 0; i< 4; i++) {
			threads[i] = new Thread(group,"MyThread#" + i);
		}
		ThreadInfo.printAllThreadInfo();
	}
	public static void printAllThreadInfo() {
		ThreadGroup parent,root;
		root = parent = Thread.currentThread().getThreadGroup();
		while((parent = parent.getParent()) != null) {
			root = parent;
		}
		out.println();
		printThreadGroupInfo(" ",root);
	}
	public static void printThreadGroupInfo (String indent, ThreadGroup group) {
		final int SAFETY = 5;
		if(group == null) {
			return ;
		}
		out.println(indent + "ThreadGroupt" + group.getName() + "; Max Priority: " +
			  group.getMaxPriority() + (group.isDaemon() ? " [Daemon] " : ""));
		int numThreads = group.activeCount();
		Thread[] threads = new Thread[numThreads + SAFETY];
		numThreads = group.enumerate(threads,false);
		for(int i = 0; i< numThreads; i++) {
			printThreadInfo(indent + " ", threads[i]);
		}
		int numGroups = group.activeGroupCount();
		ThreadGroup[] groups = new ThreadGroup[numGroups + SAFETY];
		numGroups = group.enumerate(groups, false);
		for(int i = 0; i< numGroups; i++) {
			printThreadGroupInfo(indent + "  ", groups[i]);
		}
	}
	public static void printThreadInfo (String indent, Thread thread) {
		if(thread == null) {
			return ;
		}
		out.println(indent + "Thread:" + thread.getName() + "; Priority :"
			  + thread.getPriority() + (thread.isDaemon()? "[Daemon]":" ") 
			  + (thread.isAlive()? "[Alive] " :"[NotAlive]") 
			  + ((Thread.currentThread() == thread) ? " <==current " : "" ));
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值