1. Traditional exception handling
We deal with exceptions in a single thread by throwing them up, or by try ing In the multithreaded environment, the main thread can't handle the exception thrown by the child thread
1-1. Child thread throwing up exception
Through the following code verification, we know that the child thread throws an exception, the parent thread does not interrupt, and continues to complete its own tasks
public class ThreadException implements Runnable{ @Override public void run() { throw new RuntimeException("Run err..."); } public static void main(String[] args) { new Thread(new ThreadException()).start(); for (int i = 0; i < 10; i++) { System.out.println(i); } } } // Operation result Exception in thread "Thread-0" java.lang.RuntimeException: Run err... 0 at com.polar.concurrence.thread.ThreadException.run(ThreadException.java:10) 1 at java.lang.Thread.run(Thread.java:748) 2 3 4 5 6 7 8 9
1-2.try... Catch catch exception
Through the following code experiments, we find that try Catch does not catch exceptions from child threads
public class ThreadException implements Runnable{ @Override public void run() { throw new RuntimeException("Run err..."); } public static void main(String[] args) { try { new Thread(new ThreadException()).start(); Thread.sleep(1000); new Thread(new ThreadException()).start(); Thread.sleep(1000); new Thread(new ThreadException()).start(); Thread.sleep(1000); } catch (Exception e) { System.out.println("err catch..."); } } } // Operation result Exception in thread "Thread-0" java.lang.RuntimeException: Run err... at com.polar.concurrence.thread.ThreadException.run(ThreadException.java:10) at java.lang.Thread.run(Thread.java:748) Exception in thread "Thread-1" java.lang.RuntimeException: Run err... at com.polar.concurrence.thread.ThreadException.run(ThreadException.java:10) at java.lang.Thread.run(Thread.java:748) Exception in thread "Thread-2" java.lang.RuntimeException: Run err... at com.polar.concurrence.thread.ThreadException.run(ThreadException.java:10) at java.lang.Thread.run(Thread.java:748)
2. Two strategies of multithreading exception handling
2-1. Try catch manually in the child thread (not recommended)
public class ThreadException implements Runnable{ @Override public void run() { try { throw new RuntimeException("Run err..."); } catch (Exception e) { System.out.println("Thread err warning...."); } } public static void main(String[] args) throws InterruptedException { new Thread(new ThreadException()).start(); Thread.sleep(1000); new Thread(new ThreadException()).start(); Thread.sleep(1000); } } // Operation result Thread err warning.... Thread err warning....
2-2. UncaughtExceptionHandler exception handling provided in thread class
This exception handler can be designed for different dimensions
- Design an exception handler for the program
- Each thread is designed separately
- Design a line pool
// Define an exception handler public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { private String handlerName; public MyUncaughtExceptionHandler(String handlerName) { this.handlerName = handlerName; } @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("Exception caught, processor:" + this.handlerName); System.out.println("Abnormal information: threadName:" + t.getName() + " e:" + e); } } // Use exception handler public class ThreadException implements Runnable{ @Override public void run() { throw new RuntimeException("Run err..."); } public static void main(String[] args) throws InterruptedException { Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler("Exception handler A")); new Thread(new ThreadException()).start(); } } // Operation result //Exception caught, processor:Exception handler A //Abnormal information: threadName:Thread-0 e:java.lang.RuntimeException: Run err...