Eight basic core thread seven (exception handling)

1. primer

In java multi-threaded programming, there are eight basic core. 
Have a look at what the eight basic core of it? They are: 
    1 . The way to create a thread
     2 Thread Start
     3 thread to stop.
     4 threads lifecycle.
     5 thread related methods.
     6 thread-related properties.
     7 thread exception handling.
     8 thread safe. 

Today, we are from the seventh The core foundation began: thread exception handling

2. Test your knowledge

# Antecedent review 
in software development, in addition to the normal process of business process outsourcing, exception handling is what we around the past Hom 

# Quiz 
1 . You know java exception hierarchy do?
2 . Do you know what kind of exception handling way better?
3 . UncaughtExceptionHandler you know how to use it?

3. Case

3.1. Difficult to find abnormal child thread

Brief:

1. In the child thread child-exception-0, the exception is thrown

2. The main thread main still normal execution, the actual project, makes it difficult to detect anomalies in the child thread

Package com.anan.thread.threadexception; 

/ ** 
 * main thread can not find the sub-main thread exception 
 * / 
public  class NotFoundChildThreadException { 

    public  static  void main (String [] args) {
         // Create a thread object 
        the Runnable R1 = new new MyRunnable () ; 
        the thread T1 = new new the thread (R1, "child-exception-0" ); 
        t1.start (); 

        // main loop thread printout, ignoring the sub-thread exception 
        for ( int I = 0; I <. 5; I ++ ) { 
            System.out.println ( "main thread main output: the current index scenery here is fine! [" 
                    + i + "]" );
        } 

    } 

} 

/ ** 
 * implement Runnable, creating a thread 
 * / 
class MyRunnable the implements Runnable {
     public  void RUN () { 
        System.out.println (Thread.currentThread (). GetName () + 
                "to prepare thrown up ... Start " );
         // thrown 
        the throw   new new RuntimeException (" sub-thread throws an exception. " ); 
    } 
}

Results of the:

 

 

 

3.2. Not capture sub-thread exception

Brief:

1. Create three sub-threads: thread-0, thread-1, thread-2

2. Each child thread will throw an exception

3. In the main the main thread, the capture process. If desired thread-0 throws an exception, then the thread-1 / thread-2 thread, do not create execution

/**
 * 不能捕获的子线程异常
 */
public class NotCaughtChildException {

    public static void main(String[] args) {
        // 预期子线程会抛出异常,通过try{}catch(){}捕获处理
        try{
            // 创建子线程0
            Runnable r1 = new MyRunnable1();
            Thread t0 = new Thread(r1,"thread-0");
            t0.start();

            // 创建子线程1
            Thread t1 = new Thread(r1,"thread-1");
            t1.start();

            // 创建子线程2
            Thread t2 = new Thread(r1,"thread-2");
            t2.start();

        }catch (RuntimeException e){
            System.out.println("捕获到了异常.");
        }
    }
}

/**
 * 实现Runnable,创建线程
 */
class MyRunnable1 implements Runnable{
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                "准备抛出异常了...start");
        // 抛出异常
        throw  new RuntimeException("子线程抛出了异常.");
    }
}

执行结果:

 

 

 

3.3.全局异常处理

简述:

1.在3.2.中,不能通过try{}catch(){}捕获子线程异常。因为try{}catch(){}只能捕获当前线程的异常

2.如果要对所有子线程,进行统一异常处理,需要一个全局异常处理器

3.全局异常处理器接口:Thread.UncaughtExceptionHandler

4.实现方式:

4.1.编写全局异常处理器,实现接口:Thread.UncaughtExceptionHandler
4.2.注册使用全局异常处理器

3.3.1.全局异常处理器

package com.anan.thread.threadexception;

/**
 * 自定义全局异常处理器类
 */
public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

    public void uncaughtException(Thread t, Throwable e) {
        System.out.println(t.getName() + "发生了异常,异常消息:" + e.getMessage());
    }
}

3.3.2.注册使用全局异常处理器

package com.anan.thread.threadexception;

/**
 * 注册使用自定义全局异常处理器
 */
public class UseUncaughtExceptionHandler {

    public static void main(String[] args) {

        // 关键代码:设置全局异常处理器
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

        // 创建子线程0
        Runnable r1 = new MyRunnable1();
        Thread t0 = new Thread(r1,"thread-0");
        t0.start();

        // 创建子线程1
        Thread t1 = new Thread(r1,"thread-1");
        t1.start();

        // 创建子线程2
        Thread t2 = new Thread(r1,"thread-2");
        t2.start();
    }
}

/**
 * 实现Runnable,创建线程
 */
class MyRunnable2 implements Runnable{
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                "准备抛出异常了...start");
        // 抛出异常
        throw  new RuntimeException("子线程抛出了异常.");
    }
}

3.3.3.执行结果

 

 

4.讨论分享

#考考你答案
1.你知道java的异常体系吗?
  1.1.java异常体系中,老祖宗是Throwable
  1.2.在Throwable下,有Exception异常体系(日常开发中,见得最多)
  1.3.在Throwable下,有Error错误体系(日常开发中,较少关注)
    
2.你知道哪一种异常处理方式比较好吗?
  2.1.通过案例演示,我们知道不能通过try{}catch(){},跨线程捕获异常。try{}catch(){}只能捕获当前线程自己的异常
  
  2.2.处理方式一:
   2.2.1.可以在当前线程中进行try{}catch(){},捕获处理当前线程的异常
   2.2.2.该种方式处理起来代码量多、繁琐。不推荐使用
   
  2.3.处理方式二:
   2.3.1.通过设置全局异常处理器:UncaughtExceptionHandler
   2.3.2.实现异常全局统一处理。推荐使用
   
3.你知道如何使用UncaughtExceptionHandler吗?
  3.1.编写全局异常处理器,实现接口:
   Thread.UncaughtExceptionHandler
  3.2.注册使用全局异常处理器:
   // 关键代码:设置全局异常处理器
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

java异常体系类图

 

Guess you like

Origin www.cnblogs.com/itall/p/12315722.html