One: Why do you want to talk about multi -threaded abnormal capture alone?
First look at an example:
Public Class ThreadException Implements Runnable {@Override Public Void Run () {Throw New Runtimeexception ();}: The console printed out an abnormal information for a period of time. blic static void main (string [] args) { // Even if the thread execution statement is placed in the TRY-CATCH block, there is no help. h (Runtimeexception E) {System.out.println ("Exception Has Been Handled!");}}}A runtime abnormality was thrown manually in RUN. Starting threads in main, capture abnormalities in the Catch statement block and printed a sentence. The running result is as shown below:
It was found that the abnormalities were thrown into the console, and the statements in the CATCH block were not printed.
Conclusion: Multi -threaded operation cannot handle abnormalities in the process of executing abnormalities in order. The abnormalities will be directly thrown out to the console (due to the nature of the thread, you can not capture the abnormalities of escaping from the thread. Once abnormal escapes, the task is out of the task. The RUN method will spread out to the console, unless you use special forms to capture this abnormality), which will make you a headache and cannot capture the problems caused by abnormalities.
So, we must think about how to capture abnormalities in multi -threads?
2. Capture abnormalities in multi -threaded
Let's follow the steps below to complete the experiment:
1. Define the exception processor
Requires to implement the UNCAUGHTEXCEPTION method of Thread.UncaultExceptionHandler, as follows: as follows:
/ * * 1st step: define the "abnormal processor" that complies with the thread abnormal processor specification * real.UncauGHTEXCEPTIONHANDLER specifications */CLASS MyuncauGeptionHandler Implements Handler { / * * Thread.uncauGhtexceptionhandler.uncaughtexception () will be threaded The capture of the abnormality is called when it is nearly dead*/ @Override Public Void UncaUGHTEXCEPTION (Thread T, Throwable E) {System.out.println ("CAUGHT"+E);}}}2. Define the thread factory using this abnormal processor
/** Step 2: Define the thread factory* thread factory to attach the task to the thread, and bind a thread to an abnormal processor*/Class HanldErthreadFactory Implements ThreadFactory {@Override Public ThreadRead (RUN nable r) {system. Out.println (this+"creating new thread"); Thread T = New Thread (R); System.out.println ("Created"+T); htexceptionhandler ()); // Set thread factory System.out.println ("EH ="+T.GetuncaUGHTEXCEGEPTIONLER ()); Return T;}}3. Define a task to let it throw an exception
/ * * Step 3: Our task may throw an exception. ad (); System.out. Println ("run () by"+t); System.out.println ("eh ="+t.getuncaughtexceptionhandler ()); Throwing New RuntimeException ();}}4. Call the experiment
/** Step 4: Use a thread factory to create a thread pool, and call its Execute method*/Public Class ThreadExceptionUncauGHTEXCEPTIONHANDLER {Public Static Void Main (String [] ARGS) {Executorser VICE Exec = Executors.newcacheDthreadPool (New HanldrtyReadFactory ()); exec .execute (New ExceptionThread ());}}The running result is as shown below:
3. Conclusion
To capture the abnormalities generated by multi -threaded threads in Java, you need to customize an abnormal processor and set it to the corresponding thread factory (that is, step first and second steps).
Fourth, expand
If you know that the same abnormal processor will be used everywhere in the code, the simpler way is to set a static domain in the Thread class and set this processor to the default unsatisfactory processor.
This processor is called only without the proper unsatisfactory processor without threads.
Public Static Void Main (String [] args) {Thread.SetDefaultuncaUGHTEXCEPTIONHANDLER (New MyuncauGexceptionhandler ()); ExecutorService Exec cutors.newcachedthreadPool (); Exec.execute (new exceptionthread ());}The above is all the contents of the abnormal capture of the multi -threaded multi -threaded multi -threading of Java. If you have a wrong place in this article, you are welcome to criticize and correct it.