前两天翻阅《Effective Java》发现一条提示慎用线程组。ThreadGroup提供的很多功能的实现是有瑕疵的。例如,我们可以调用activeCount获得该组中活动线程的数量,一旦这个数组进行了分配,并用enumerate方法遍历,如果线程数增加了,就有可能忽略掉调用activeCount后新增的线程。关于处理线程组逻辑,可以用线程池的executor代替。
也许ThreadGroup提供的有用的功能之一就是uncaughtException方法了。Java提供了强大的异常处理机制,有些异常可以通过try/catch捕获或者re-throw,这些都是checked exception,比如IOException和ClassNotFoundException,还有一些是不必要捕获处理的,如NumberFormatException.
在Java多线程中提供了一个层次化的机制帮助我们有效的处理uncaught exception. 当一个线程抛出异常时,JVM首先会调用thread里面指定的uncaught exception handler处理异常,如果在thread级没有设置handler,JVM会到当前线程所在的组的exception handler处理异常,如果线程组没有定义uncaughtException方法,JVM会继续寻找上一级的exception handler处理。如果一个handler都没有找到,这默认打出错误栈并推出程序。
让们看一个具体的例子:
import java.lang.Thread.UncaughtExceptionHandler;
public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.printf("Uncaught exception raised and captured in thread %s : \n", t.getName());
}
}
import java.lang.Thread.UncaughtExceptionHandler;
public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.printf("Uncaught exception raised and captured in thread %s : \n", t.getName());
}
}
public class MyThreadGroupWithExceptionHandler extends ThreadGroup {
public MyThreadGroupWithExceptionHandler(String name) {
super(name);
}
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.printf("Uncaught exception caught in thread group's uncaughtException %s.\n",this.getName());
interrupt();
}
}
public class TestUncaughtException {
public static void main(String[] args){
MyTask task1 = new MyTask();
Thread t1 = new Thread(task1,"UncaughtException Task");
t1.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
t1.start();
MyThreadGroupWithExceptionHandler group1 = new MyThreadGroupWithExceptionHandler("mythread");
MyTask task2 = new MyTask();
Thread t2 = new Thread(group1, task2,"Task with group exception handler");
t2.start();
ThreadGroup group2 = new ThreadGroup("mythread");
MyTask task3 = new MyTask();
Thread.setDefaultUncaughtExceptionHandler(new MyDefaultExceptionHandler());
Thread t3 = new Thread(group2, task3, "Task with exception caught by Thread.default");
t3.start();
}
}
输出可见异常分别在Thread实例,ThreadGroup,Thread级别被捕获。
分享到:
相关推荐
前端大厂最新面试题-controlled_Uncontrolled.docx
controlled-uncontrolled-forms-react-源码.rar
含电容滤波的单相不可控整流电路的simulink仿真图
受控和不受控制的React形式该项目是通过引导的。关于回购这个仓库包含了基于类和功能的组件,它们具有在React中使用表单的不同方法。 我使用了带有不同状态管理方法的受控和非受控形式示例。... 打开在浏览器中查看它。...
三相电源的整流。在这个模型中,我们展示了当 Ls=0 时的效果......意味着没有连接电感器。
这是连接电感时的整流三相电源模型。表示电感不为零。
语音识别a complete speech recognition system for the DARPA TIMIT and Resource Management tasks
Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:npm start 在开发模式下运行该应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒...
12 pulse uncontrolled rectifier
response of uncontrolled building
很好地理解使用线性变压器块来降低给定的输入电压。 GOTO 和 FROm 块也用于信号路由,并使用万用表和示波器绘制选定的测量值。
Based on the latest multi-view 3D reconstruction technology, it operates with arbitrary images and is efficient in both controlled and uncontrolled conditions. Photos can be taken from any position, ...
UnControlled包含一个简单的包装程序,该包装程序主要由codemirror本身的内部工作提供codemirror ,而Controlled则需要用户进行状态管理,除非通过value正确处理,否则它会阻止Codemirror进行更改。 后者将提供更多...
Based on the latest multi-view 3D reconstruction technology, it operates with arbitrary images and is efficient in both controlled and uncontrolled conditions. Photos can be taken from any position, ...
20篇英语专四的dictation训练 Dictation 1 Forest Fire森林火灾(150words) Forest fire or wildfire is often used to signify an uncontrolled intense fire/that breaks out ......
that they span test images taken under uncontrolled illumination. In order to evaluate how our algorithms work under practical testing conditions, we have implemented a complete face recognition ...
三种倒立摆系统的代码及simulink... disp('system is uncontrolled.') end Vo=obsv(A,C); ro=rank(Vo); if ro==n disp('system is observable.') elseif ro~=n disp('system is no observable.') end
The AT138RV3-W is miniaturized receiver for infrared remote control systems.... The main benefit is the reliable function even in disturbed ambient and the protection against uncontrolled output pulses.