Thread eight basic core seven (exception handling)

Keywords: Programming Java less

1. Introduction

There are eight basic cores in java multithreaded concurrent programming.
What are the eight core foundations?They are:
	1. How threads are created
	2. Thread Start
	3. Thread Stop
	4. Thread life cycle
	5. Thread-related methods
	6. Thread-related properties
	7. Thread exception handling
	8. Thread Security

Today we start with the seventh fundamental core: thread exception handling

2. Examine you

#Previous Review
In software project development, exception handling is also a barrier that we can't get around in addition to normal business processes.

#Examine you
1. Do you know the exception system of java?
2. Do you know which exception handling method is better?
3. Do you know how to use UncaughtExceptionHandler?

3. Cases

3.1. Difficult to discover subthread exceptions

Sketch:

1. Throw an exception in child-exception-0

2. The main thread mains are still running properly, which in the actual project will make it difficult to find exceptions for the sub-threads

package com.anan.thread.threadexception;

/**
 * Main thread main cannot find child thread exception
 */
public class NotFoundChildThreadException {

    public static void main(String[] args) {
        // Create Thread Object
        Runnable r1 = new MyRunnable();
        Thread t1 = new Thread(r1,"child-exception-0");
        t1.start();

        // Main Thread Loops Print Output, Ignoring Subthread Exceptions
        for (int i = 0; i < 5; i++) {
            System.out.println("Main Thread main Output: The view is unique here!Current Index ["
                    + i + "]");
        }

    }

}

/**
 * Implement Runnable, create thread
 */
class MyRunnable implements Runnable{
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                "Ready to throw an exception...start");
        // throw
        throw  new RuntimeException("Subthread threaded an exception.");
    }
}

3.2. Subthread exceptions that cannot be caught

Sketch:

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

2. Every child thread throws an exception

3. Capture processing in the main thread.Expect thread-1/thread-2 threads not to create execution if thread-0 throws an exception

/**
 * Uncaught Subthread Exception
 */
public class NotCaughtChildException {

    public static void main(String[] args) {
        // Expected child threads to throw exceptions, handled by try{}catch(){}
        try{
            // Create Subthread 0
            Runnable r1 = new MyRunnable1();
            Thread t0 = new Thread(r1,"thread-0");
            t0.start();

            // Create Subthread 1
            Thread t1 = new Thread(r1,"thread-1");
            t1.start();

            // Create Subthread 2
            Thread t2 = new Thread(r1,"thread-2");
            t2.start();

        }catch (RuntimeException e){
            System.out.println("Exception caught.");
        }
    }
}

/**
 * Implement Runnable, create thread
 */
class MyRunnable1 implements Runnable{
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                "Ready to throw an exception...start");
        // throw
        throw  new RuntimeException("Subthread threaded an exception.");
    }
}

3.3. Global exception handling

Sketch:

1.In 3.2. child thread exceptions cannot be caught by try {}catch(){}.Because try{}catch(){} can only catch exceptions for the current thread

2. A global exception handler is required if uniform exception handling is to be performed on all subthreads

3. Global exception handler interface: Thread.UncaughtExceptionHandler

4. Implementation:

4.1. Write a global exception handler to implement the interface: Thread.UncaughtExceptionHandler
4.2. Register to use Global Exception Handler

3.3.1. Global exception handler

package com.anan.thread.threadexception;

/**
 * Custom Global Exception Handler Class
 */
public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

    public void uncaughtException(Thread t, Throwable e) {
        System.out.println(t.getName() + "An exception has occurred, exception message:" + e.getMessage());
    }
}

3.3.2. Register to use Global Exception Handler

package com.anan.thread.threadexception;

/**
 * Register to use custom global exception handlers
 */
public class UseUncaughtExceptionHandler {

    public static void main(String[] args) {

        // Key Code: Set up a global exception handler
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

        // Create Subthread 0
        Runnable r1 = new MyRunnable1();
        Thread t0 = new Thread(r1,"thread-0");
        t0.start();

        // Create Subthread 1
        Thread t1 = new Thread(r1,"thread-1");
        t1.start();

        // Create Subthread 2
        Thread t2 = new Thread(r1,"thread-2");
        t2.start();
    }
}

/**
 * Implement Runnable, create thread
 */
class MyRunnable2 implements Runnable{
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                "Ready to throw an exception...start");
        // throw
        throw  new RuntimeException("Subthread threaded an exception.");
    }
}

3.3.3. Execution results

4. Discuss and share

#Examine your answers
1.You know, java Abnormal system?
  1.1.java In the abnormal system, the ancestors were Throwable
  1.2.stay Throwable Next, there are Exception Abnormal systems (most seen in daily development)
  1.3.stay Throwable Next, there are Error Error system (less attention in daily development)
	
2.Do you know which exception handling method is better?
  2.1.Through a case study, we know we can't pass try{}catch(){},Catch exceptions across threads. try{}catch(){}Only the current thread's own exception can be caught
  
  2.2.Processing method 1:
   2.2.1.Can be done in the current thread try{}catch(){},Capture exceptions that handle the current thread
   2.2.2.This way of handling is a lot of code and tedious.Not recommended
   
  2.3.Processing mode two:
   2.3.1.By setting up a global exception handler: UncaughtExceptionHandler
   2.3.2.Implement global uniform exception handling.Recommended Use
   
3.You know how to use it UncaughtExceptionHandler Are you?
  3.1.Write a global exception handler to implement the interface:
   Thread.UncaughtExceptionHandler
  3.2.Register to use Global Exception Handler:
   // Key Code: Set up a global exception handler
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

java exception system class diagram:

Posted by PHPThorsten on Sat, 15 Feb 2020 18:02:23 -0800