前两天翻阅《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-forms-react-源码.rar
前端大厂最新面试题-controlled_Uncontrolled.docx
Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:npm start 在开发模式下运行该应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒...
含电容滤波的单相不可控整流电路的simulink仿真图
材料UI芯片输入 ...// uncontrolled input < ChipInput xss=removed xss=removed xss=removed> handleChange ( chips ) } /> // controlled input < ChipInput xss=removed xss=removed xss=removed> handle
这是连接电感时的整流三相电源模型。表示电感不为零。
受控和不受控制的React形式该项目是通过引导的。关于回购这个仓库包含了基于类和功能的组件,它们具有在React中使用表单的不同方法。 我使用了带有不同状态管理方法的受控和非受控形式示例。... 打开在浏览器中查看它。...
三相电源的整流。在这个模型中,我们展示了当 Ls=0 时的效果......意味着没有连接电感器。
语音识别a complete speech recognition system for the DARPA TIMIT and Resource Management tasks
Imitation Learning • Also known as learning...• Hand-crafted rewards can lead to uncontrolled behavior • Two approaches: • Behavior Cloning • Inverse Reinforcement Learning (inverse optimal control)
Uncontrolled trials Meta-analysis and systematic review Meta-analysis Systematic Review Week 2: Describing your data The spectrum of data types Definitions Descriptive statistics Inferential ...
The Logic of Uncontrolled Growth How Much Consumption? Making Space for a New Set of Voices Solving the Growth Dilemma Notes 11 Putting Poverty in Museums A Better World Starts with Imagination...
很好地理解使用线性变压器块来降低给定的输入电压。 GOTO 和 FROm 块也用于信号路由,并使用万用表和示波器绘制选定的测量值。
However, due to uncontrolled vocabularies, social tags are usually redundant, sparse, and ambiguous. In this paper, we propose a deep neural network approach to solve this problem by mapping both the...
UnControlled包含一个简单的包装程序,该包装程序主要由codemirror本身的内部工作提供codemirror ,而Controlled则需要用户进行状态管理,除非通过value正确处理,否则它会阻止Codemirror进行更改。 后者将提供更多...
--- 因子 factors 可控因子和非可控因子 进程 x1 x2 x3 u1 u2 u3 y1 y2 y3 进程的模型 照应变量response 非可控因子〔噪声因子〕 Uncontrolled factor/noise factor 因子 factor 实验设计基本概念引见 DOE培训资料...
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, ...
12 pulse uncontrolled rectifier
response of uncontrolled building
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.