1.主线程不能捕获到子线程的异常

package Thread.Exection;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ExeceptionThread implements Runnable {
@Override
public void run() {
throw new RuntimeException();
} public static void main(String[] args) {
try {
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new ExeceptionThread());
} catch (Exception e) {
System.out.println("exeception has handled");
}
}
}

输出:

Exception in thread "pool-1-thread-1" java.lang.RuntimeException
at Thread.Exection.ExeceptionThread.run(ExeceptionThread.java:10)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

2.通过设置HandlerThreadFactory捕获异常

package Thread.Exection;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; class ExceptionThread2 implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.println("run() by " + t);
System.out.println("eh=" + t.getUncaughtExceptionHandler());
throw new RuntimeException();
}
} class MyUncaughtExceptionHandler 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(this + " creating new Thread");
Thread t = new Thread(r);
System.out.println("created " + t + " ID:" + t.getId());
t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
System.out.println("eh=" + t.getUncaughtExceptionHandler());
return t;
}
} public class CaptureUncaughtException {
public static void main(String[] args) {
ExecutorService exec = Executors
.newCachedThreadPool(new HandlerThreadFactory());
exec.execute(new ExceptionThread2());
}
}

输出:

Thread.Exection.HandlerThreadFactory@4e25154f creating new Thread
created Thread[Thread-0,5,main] ID:9
eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4e
run() by Thread[Thread-0,5,main]
eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4e
Thread.Exection.HandlerThreadFactory@4e25154f creating new Thread
created Thread[Thread-1,5,main] ID:11
eh=Thread.Exection.MyUncaughtExceptionHandler@193fa958
caught java.lang.RuntimeException

3.通过设置默认异常捕获类捕获异常

package Thread.Exection;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class SettingDefaultHandler {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(
new MyUncaughtExceptionHandler()); ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new ExceptionThread2());
}
}

输出:

run() by Thread[pool-1-thread-1,5,main]
eh=java.lang.ThreadGroup[name=main,maxpri=10]
caught java.lang.RuntimeException

Java多线程之捕获异常的更多相关文章

  1. java 多线程为何会出现无法捕获异常的现象?

    提出问题: 很多Java初学者在初学java 多线程的时候可能会看到如下代码: public class ExceptionThread implements Runnable{ @Override ...

  2. JAVA多线程和并发基础面试问答(转载)

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  3. Java多线程学习(转载)

    Java多线程学习(转载) 时间:2015-03-14 13:53:14      阅读:137413      评论:4      收藏:3      [点我收藏+] 转载 :http://blog ...

  4. [转] JAVA多线程和并发基础面试问答

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  5. JAVA多线程和并发基础面试问答

    转载: JAVA多线程和并发基础面试问答 多线程和并发问题是Java技术面试中面试官比较喜欢问的问题之一.在这里,从面试的角度列出了大部分重要的问题,但是你仍然应该牢固的掌握Java多线程基础知识来对 ...

  6. 【多线程】JAVA多线程和并发基础面试问答(转载)

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  7. 50个Java多线程面试题

    不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java 语言一个重要的特点就是内置了对并发的支持,让 Java 大受企业和程序员的欢迎.大多数待遇丰厚的 Java 开发职位都要求开发者 ...

  8. (转)JAVA多线程和并发基础面试问答

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  9. Java多线程(二) —— 线程安全、线程同步、线程间通信(含面试题集)

    一.线程安全 多个线程在执行同一段代码的时候,每次的执行结果和单线程执行的结果都是一样的,不存在执行结果的二义性,就可以称作是线程安全的. 讲到线程安全问题,其实是指多线程环境下对共享资源的访问可能会 ...

随机推荐

  1. acess the C\c++ from the Java

    https://en.wikipedia.org/wiki/Java_Native_Interface http://docs.oracle.com/javase/7/docs/technotes/g ...

  2. java .net compartion

    1, http://www-01.ibm.com/software/smb/na/J2EE_vs_NET_History_and_Comparison.pdf http://stackoverflow ...

  3. pyzmq missing when running ipython notebook

    Q: I can run iPython, but when I try to initiate a notebook I get the following error: ~ ipython not ...

  4. adaptive hash index

    An optimization for InnoDB tables that can speed up lookups using = and IN operators, by constructin ...

  5. WCF Data Service 使用小结 —— 了解OData(一)

    最近做了一个小项目,其中用到了 WCF Data Service,之前是叫 ADO.NET Data Service 的.关于WCF Data Service,博客园里的介绍并不多,但它确实是个很好的 ...

  6. IIS6下PHP环境的资源未找到(404)问题

    故障现象: 无法找到该页, 404错误

  7. 使用Path语法取得对象的值

    借鉴了http://stackoverflow.com/questions/4473928/c-sharp-dynamic-string-property-path     public class ...

  8. OSPF(Open Shortest Path First开放式最短路径优先 -链路状态路由协议

    OSPF分为OSPFv2和OSPFv3两个版本,其中OSPFv2用在IPv4网络,OSPFv3用在IPv6网络 思科OSPF的协议管理距离(AD)是110,华为OSPF的协议管理距离是10 通告网络接 ...

  9. Amazon关键词抓取

    亚马逊的网址构造很简单,几乎算是静态的网页,花费3小时完美收工,不要在意细节! 在python3下利用xpath就可以完美解决 xpath的使用方法请见: python之lxml(xpath) 入口图 ...

  10. React Native知识点

    1. Live Reload和Hot Reloading的区别: 相同点:都是你只要保存一下原文件,那么App就会实时刷新. 区别:Live Reload是全局刷新,而Hot Reloading是局部 ...