java 多线程 异常处理

本文介绍如何在Java中处理线程中未被捕获的异常,包括使用Thread.UncaughtExceptionHandler接口来实现异常捕获,以及如何通过自定义ThreadFactory来为每个线程设置异常处理器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

/**
 * 线程抛出异常实例。try catch是无法捕获异常的。
 * 
 * @create @author Henry @date 2016-11-23
 *
 */
public class ExceptionRun extends Thread{
	@Override
	public void run() {
		throw new RuntimeException();
	}
	public static void main(String[] args) {
		try {
			ExecutorService exec=Executors.newCachedThreadPool();
			exec.execute(new ExceptionRun());
		} catch (Exception e) {
			System.out.println();
		}
	}
}


/**
 * 捕获多线程中的异常
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
 * 简单的异常线程类
 * 
 * @create @author Henry @date 2016-11-23
 *
 */
class ExceptionThread2 implements Runnable{
	@Override
	public void run() {
		Thread t=Thread.currentThread();
		System.out.println("run() by "+t);
		System.out.println("eh = "+t.getUncaughtExceptionHandler());;
		throw new RuntimeException();
	}
}
/**
 * Thread.UncaughtExceptionHandler是java SE5 中的新接口,
 * 它允许你在每个Thread对象上都附着一个异常处理器。
 * Thread.UncaughtExceptionHandler.uncaughtException()
 * 会在线程因未捕获的异常而临近死亡时被调用
 * 
 * @create @author Henry @date 2016-11-23
 *
 */
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{

	@Override
	public void uncaughtException(Thread t, Throwable e) {
		System.out.println("caught "+ e);
	}
}
/**
 * 为了使用上述方法,我们创建了一个新类型的ThreadFactory,
 * 它将在每个新创建的Thread对象上附着一个Thread.UncaughtExceptionHandler.
 * 
 * 
 * @create @author Henry @date 2016-11-23
 *
 */
class HandlerThreadFactory implements ThreadFactory{

	@Override
	public Thread newThread(Runnable r) {
		System.out.println(this+" creating new Thread");
		Thread t=new Thread(r);
		System.out.println("created "+ t);
		t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
		System.out.println("eh = "+t.getUncaughtExceptionHandler());
		return t;
	}
	
}
/**
 * 我们将这个工厂传递给Executors创建新的ExecutorService的方法
 * 
 * @create @author Henry @date 2016-11-23
 *
 */
public class CaptureUncaughtException {
	/**
	 * 在程序中添加了额外的跟踪机制,用来验证工厂创建的线程会传递给UncaughtExceptionHandler;
	 * 现在可以看到,未捕获的异常时通过uncaughtException来捕获的。
	 * 
	 * 运行结果:
	 * com.think.no21.HandlerThreadFactory@47b480 creating new Thread
	 * created Thread[Thread-0,5,main]
	 * eh = com.think.no21.MyUncaughtExceptionHandler@10d448
	 * this
	 * run() by Thread[Thread-0,5,main]
	 * eh = com.think.no21.MyUncaughtExceptionHandler@10d448
	 * caught java.lang.RuntimeException
	 * 
	 * @param args
	 */
	public static void main1(String[] args) {
		ExecutorService exec=Executors.newCachedThreadPool(new HandlerThreadFactory());
		exec.execute(new ExceptionThread2());
		System.out.println("this");
		//exec.shutdown();
	}
	
	/**
	 * 上面的示例使得你可以按照具体情况逐个地设置处理器,如果你知道将要在代码中处处
	 * 使用相同的异常处理器,那么更简单的方式是在Thread类中设置一个静态域,并将这个
	 * 处理器设置为默认的未捕获异常处理器。
	 * 这个处理器只有在不存在线程转悠的未捕获异常处理器的情况下才会被调用。
	 * 系统会检查线程专有版本,如果没有发现,则检查线程组是否有其专有的
	 * uncaughtException()方法,如果也没有,再调用defaultUncaughtExceptionHandler
	 * 
	 * 运行结果:
	 * run() by Thread[pool-1-thread-1,5,main]
	 * eh = java.lang.ThreadGroup[name=main,maxpri=10]
	 * caught java.lang.RuntimeException
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
		ExecutorService exec=Executors.newCachedThreadPool();
		exec.execute(new ExceptionThread2());
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值