Exception handling method based on Java sub thread (general)
In ordinary single threaded programs, you only need to catch exceptions through try catch ... finally ... Just code blocks. Then, in the case of concurrency, such as starting a child thread in the parent thread, how to catch exceptions from the child thread in the parent thread and handle them accordingly?
Common errors
Some people may think that it is very simple to try directly where the parent thread starts the child thread Just catch one. In fact, this is wrong.
Cause analysis
Let's recall the complete signature of the run method of the runnable interface. Because the throws statement is not identified, the method will not throw a checked exception. As for unchecked exceptions such as runtimeException, because the new thread is scheduled and executed by the JVM, if an exception occurs, the parent thread will not be notified.
public abstract void run()
terms of settlement
So, how to catch exceptions from child threads in the parent thread? The landlord thought of three common methods to share with you.
Method 1: try in the child thread catch...
The simplest and most effective way is to try where exceptions may occur in the sub thread method catch ... Statements. Sub thread code:
Method 2: set the exception handler uncaughtexceptionhandler for the thread
Set the exception handler for the thread. The specific methods can be as follows:
(1) Thread.setuncaughtexceptionhandler sets the exception handler for the current thread
(2) Thread.setdefaultuncaughtexceptionhandler sets the default exception handler for the entire program. If the current thread has an exception handler (no by default), the uncaughtexceptionhandler class is preferred; otherwise, if the thread group to which the current thread belongs has an exception handler, the exceptionhandler of the thread group is used; otherwise, the global default defaultuncaughtexceptionhandler is used; if none, the child thread will exit.
Note: if an exception occurs in the child thread, if there is no class to handle it, it will exit directly without leaving any logs. Therefore, if you do nothing, there will be a "strange" phenomenon that the sub thread task is neither executed nor prompted by any log.
Set the exception handler for the current thread:
Alternatively, set the default exception handler for all threads
Command line output: do something 1
handle exception in child thread. java. lang.RuntimeException: ChildThread exception
Method 3: catch exceptions through the get method of future
Use the thread pool to submit a method that can get the return information, that is, executorservice Submit (callable) can obtain the future object of the execution result of a thread after submit. If an exception occurs in the child thread, use future When get () gets the return value, it can catch the executionexception exception, so as to know that an exception has occurred in the child thread.
Sub thread code:
Parent thread code:
Command line output: do something 1
handle exception in child thread. java. util. concurrent. ExecutionException: java. lang.RuntimeException: ChildThread1 exception
summary
The above are three common Java sub thread exception handling methods. In fact, the landlord also thought of solutions for several other specific scenarios. I'll analyze them another day. Thank you for your support~
The exception handling method (general) based on Java sub thread is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.