码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程之捕获异常

时间:2014-05-26 19:41:48      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

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

bubuko.com,布布扣
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");
        }
    }
}
bubuko.com,布布扣

 输出:

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捕获异常

bubuko.com,布布扣
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());
    }
}
bubuko.com,布布扣

输出:

bubuko.com,布布扣
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
bubuko.com,布布扣

 

 

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

bubuko.com,布布扣
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());
    }
}
bubuko.com,布布扣

 输出:

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

 

 

Java多线程之捕获异常,布布扣,bubuko.com

Java多线程之捕获异常

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/zhuawang/p/3751875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!