java 线程 捕获异常

java 线程 捕获异常  来自:thinking in java 4 目录20.2.13


package org.rui.thread.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 捕获异常
 * 
 * 下面的任务总是会抛出一个异常,该异常会传播到其run方法的外部,
 * 并且main展示了当你运行它时,所发生的事情
 * @author lenovo
 *
 */
public class ExceptionThread implements Runnable {

	@Override
	public void run() {
		throw new RuntimeException();
		
	}
	
	public static void main(String[] args) {
		/*ExecutorService exec=Executors.newCachedThreadPool();
		exec.execute(new ExceptionThread());
		*/
		try {
			ExecutorService exec=Executors.newCachedThreadPool();
			exec.execute(new ExceptionThread());
		} catch (Exception e) {
			System.out.println("eeeeeeeeeeeeeeee 该语句将不执行!");
		}
		
	
	}

}
/**output: 以上输出结果一样:
 Exception in thread "pool-1-thread-1" java.lang.RuntimeException
	at org.rui.thread.concurrent.ExceptionThread.run(ExceptionThread.java:15)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:722)
 */

package org.rui.thread.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;


/** 
 *  捕获异常
 *  
 * 为了解决这个问题,我们要修改executor产生线程的方式。thread.UncaughtExceptionHandler是javaSE5中的新接口,
 * 它允许你在每个Thread对象上都附着一个异常处理器......
 *
 * @author lenovo
 *
 */
class ExceptionThread2 implements Runnable
{
	@Override
	public void run() {
	Thread t=Thread.currentThread();
	System.out.println("run by : "+t);
	System.out.println(t.getUncaughtExceptionHandler());
	throw new RuntimeException();
	}
	
}
//无知的Exception
class MyUncaughtExecptionHandler implements Thread.UncaughtExceptionHandler
{
	@Override
	public void uncaughtException(Thread t, Throwable e) {
		
		System.out.println("caught "+e);
		
	}
	
}

class handlerThreadFactory implements ThreadFactory
{

	@Override
	public Thread newThread(Runnable r) {
		System.out.println("创建新的线程");
		Thread t=new Thread(r);
		t.setUncaughtExceptionHandler(new MyUncaughtExecptionHandler());
		System.out.println("eh= "+t.getUncaughtExceptionHandler());
		return t;
	}
	
}

public class CaptureUncaughtExecption {
	public static void main(String[] args) {
		ExecutorService exec=Executors.newCachedThreadPool(new handlerThreadFactory()	);
		exec.execute(new ExceptionThread2());
		
		
	}

}

/**
 output:
 创建新的线程
eh= org.rui.thread.concurrent.MyUncaughtExecptionHandler@192c8d9
run by : Thread[Thread-0,5,main]
org.rui.thread.concurrent.MyUncaughtExecptionHandler@192c8d9
创建新的线程
eh= org.rui.thread.concurrent.MyUncaughtExecptionHandler@16f144c
caught java.lang.RuntimeException
 */

package org.rui.thread.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 这个处理器只有在不存在线程专有的末捕获异常处理器的情况下才会被调用。
 * 系统会检查线程专有版 本,如果没有发现,则检查线程组是否有其专有的uncaughtException()方法,
 * 如果也没有,再调用defaultUncaughtExceptionHandler
 * @author lenovo
 *
 */
public class SettingDefaultHandler 
{
	public static void main(String[] args) {
		Thread.setDefaultUncaughtExceptionHandler(
				new MyUncaughtExecptionHandler()
				);
		
		ExecutorService exec=Executors.newCachedThreadPool();
		exec.execute(new ExceptionThread());
	}

}
/**
 * output:
 caught java.lang.RuntimeException

 */


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值