libGdx专题

UncaughtExceptionHandler使用

2021-08-23  本文已影响0人  大旺旺的弟弟小旺旺

线程发生了未捕获的异常,可以使用try catch进行捕获,在多线程的环境下就不可以了,会造成直接的崩溃或者错误,而无法回收资源。

public class Demo01 {
    public static void main(String[] args) {
        try{
            int i =  1/0;
        }catch (Exception e){
            System.out.println("error"+e.getMessage());
        }
    }
}

/*
* error/ by zero
* */

案例2:

public class Demo02 {
    public static void main(String[] args) {
        try{
            Thread thread1 = new Thread(new Thread1());
            thread1.start();
        }catch (Exception e){
            System.out.println("error"+e.getMessage());
        }
    }
}

class Thread1 implements Runnable {

    @Override
    public void run() {
        int i = 1/0;
    }
}

/*
*
* Exception in thread "Thread-0" java.lang.ArithmeticException: / by zero
    at Thread1.run(Demo02.java:16)
    at java.lang.Thread.run(Thread.java:748)
* */

案例3

public class Demo03 {
    public static void main(String[] args) {
        try {
            Thread thread1 = new Thread(new Thread2());
            thread1.start();
        } catch (Exception e) {
            System.out.println("error" + e.getMessage());
        }
    }
}

class Thread2 implements Runnable {

    @Override
    public void run() {
        try {
            int i = 1/0;

        }catch (Exception e){
            System.out.println("error"+e.getMessage());
        }
    }
}

/*
* error/ by zero
* */

案例4:

public class Demo04 {
    public static void main(String[] args) {
        try {
            Thread thread1 = new Thread(new Thread3());

            thread1.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
            thread1.start();


            Thread thread2 = new Thread(new Thread3());
            thread2.start();

        } catch (Exception e) {
            System.out.println("error" + e.getMessage());
        }
    }
}

class Thread3 implements Runnable {

    @Override
    public void run() {
        int i = 1/0;
    }
}


class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("error!");
    }
}

/*
* error!
Exception in thread "Thread-1" java.lang.ArithmeticException: / by zero
    at Thread3.run(Demo04.java:23)
    at java.lang.Thread.run(Thread.java:748)
* */

案例5:

public class Demo05 {
    public static void main(String[] args) {
        try {
            Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler5());
            Thread thread1 = new Thread(new Thread5());
            thread1.start();


            Thread thread2 = new Thread(new Thread5());
            thread2.start();

        } catch (Exception e) {
            System.out.println("error" + e.getMessage());
        }
    }
}

class Thread5 implements Runnable {

    @Override
    public void run() {
        int i = 1/0;
    }
}


class MyUncaughtExceptionHandler5 implements Thread.UncaughtExceptionHandler {

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("error!");
    }
}

/**
 *
 * error!
 * error!
* */

案例6:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo06 {
    public static void main(String[] args) {
        try {
            Thread thread1 = new Thread(new Thread6());
            thread1.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler6());
            ExecutorService service = Executors.newCachedThreadPool();
//            service.execute(thread1);
            service.submit(thread1);
            service.shutdown();
        } catch (Exception e) {
            System.out.println("error" + e.getMessage());
        }
    }
}

class Thread6 implements Runnable {

    @Override
    public void run() {
        int i = 1/0;
    }
}


class MyUncaughtExceptionHandler6 implements Thread.UncaughtExceptionHandler {

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("error!");
    }
}

/*
* Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
    at Thread6.run(Demo06.java:21)
    at java.lang.Thread.run(Thread.java:748)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    *
    * 改进就是将其设置到runnable里面
* */

上一篇下一篇

猜你喜欢

热点阅读