并发编程实战 1.10. 线程组处理异常 - 重写uncaughtException()
并发编程实战 1.10. 线程组处理异常 - 重写uncaughtException()
火犀牛 发表于2年前
并发编程实战 1.10. 线程组处理异常 - 重写uncaughtException()
  • 发表于 2年前
  • 阅读 11
  • 收藏 0
  • 点赞 2
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: 线程组通过重写uncaughtException(Thread t, Throwable e)方法处理线程组异常

线程组通过重写uncaughtException(Thread t, Throwable e)方法处理线程组异常。

范例: 查询文件(sleep),其中一个抛出异常,threadGroup处理异常后,全部进程都interrupt

自定义的线程组:

package com.rr.concurrent.chapter1.recipe11.test;

/**
 * Created by isaac_gu on 2016/5/11.
 */
public class MyThreadGroup extends ThreadGroup {
    public MyThreadGroup(String name) {
        super(name);
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("******************************** Exception Handler ******************************");

        System.out.printf("1. %s 线程出现了异常!\n", t.getName());
        System.out.printf("2. Exception: %s: %s\n", e.getClass().getName(), e.getMessage());
        System.out.printf("3. Stack Trace: \n");
        e.printStackTrace(System.out);
        System.out.printf("4. Thread status: %s\n", t.getState());
        System.out.println("******************************** Exception Handler END ******************************");

        //打断所有活动的线程
        interrupt();
    }
}

查找线程:

package com.rr.concurrent.chapter1.recipe11.test;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by isaac_gu on 2016/5/11.
 */
public class SearchTask implements Runnable {
    @Override
    public void run() {
        System.out.printf("%s 线程开始查找!\n", Thread.currentThread().getName());
        //2秒后开始查找
        suspend(2);
        while (true) {
            //当除以0 的时候报异常
            int c = 1000 / new Random().nextInt(100);
            if (Thread.currentThread().isInterrupted()) {
                System.out.printf("%s 被打断!\n", Thread.currentThread().getName());
                return;
            }

            //每100微秒查询一次
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                System.out.printf("%s 被打断!\n", Thread.currentThread().getName());
                return;
            }
        }
    }

    private void suspend(int s) {
        try {
            TimeUnit.SECONDS.sleep(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

测试:

package com.rr.concurrent.chapter1.recipe11.test;


/**
 * Created by isaac_gu on 2016/5/11.
 * 范例: 查询文件,其中一个抛出异常,threadGroup处理异常后,全部进程都interrupt
 */
public class Test {
    public static void main(String[] args) {
        MyThreadGroup myThreadGroup = new MyThreadGroup("myThreadGroup");
        SearchTask searchTask = new SearchTask();

        for (int i = 0, len = 10; i < len; i++) {
            new Thread(myThreadGroup, searchTask).start();
        }

        System.out.println("主线程结束!");
    }
}

运行结果:

Thread-0 线程开始查找!
Thread-5 线程开始查找!
Thread-9 线程开始查找!
Thread-1 线程开始查找!
Thread-7 线程开始查找!
Thread-3 线程开始查找!
Thread-8 线程开始查找!
Thread-4 线程开始查找!
Thread-6 线程开始查找!
Thread-2 线程开始查找!
******************************** Exception Handler ******************************
1. Thread-0 线程出现了异常!
2. Exception: java.lang.ArithmeticException: / by zero
3. Stack Trace: 
java.lang.ArithmeticException: / by zero
	at com.rr.concurrent.chapter1.recipe11.test.SearchTask.run(SearchTask.java:17)
	at java.lang.Thread.run(Thread.java:745)
4. Thread status: RUNNABLE
******************************** Exception Handler END ******************************
Thread-6 被打断!
Thread-9 被打断!
Thread-1 被打断!
Thread-3 被打断!
Thread-5 被打断!
Thread-2 被打断!
Thread-4 被打断!
Thread-7 被打断!
Thread-8 被打断!

 

标签: Java ThreadGroup
共有 人打赏支持
粉丝 0
博文 27
码字总数 12180
×
火犀牛
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: