Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

import java.lang.Thread.UncaughtExceptionHandler; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory;

/**

  • @ClassName: ExceptionThread
  • @Description: TODO(这里用一句话描述这个类的作用)
  • @date 2013-10-10 下午12:04:23 */

public class ExceptionThread implements Runnable {

@Override
public void run() {
	throw new RuntimeException("这个线程就干了这么一件事,抛出一个运行时异常");
}

public static void main(String[] args) {
	try {

// Runnable addStarter = new Runnable() { // // public void run() { // // // 在这里调用我们自己的程序的入口函数 // // // MyApplication.main(args); // // } // // }; // // // 把我们自己的程序当作这个线程组的一个线程来运行 // // new Thread(new ApplicationLoader(), addStarter).start();

		ExecutorService exec = Executors.newCachedThreadPool(/**new ThreadFactory(){

			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				t.setUncaughtExceptionHandler(new UncaughtExceptionHandler(){

					@Override
					public void uncaughtException(Thread t, Throwable e) {
						//System.err.println("uncaughtException 哈哈,异常捕获了. id:"+ t.getId() + " name:" + t.getName() + e);
					}
					
				});
				return t;
			}
			
		}*/);
		exec.execute(new ExceptionThread());
		System.out.println("该干嘛干嘛去");
	} catch (RuntimeException e) {
		System.out.println("能不能捕获到异常?");
	}

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment