Java thread learning notes (2) thread exception capture, java learning notes

Source: Internet
Author: User

Java thread learning notes (2) thread exception capture, java learning notes

Capture thread exceptions:

Normally, we cannot catch thread exceptions in the main () method, for example, the following code:

Public class ExceptionThread implements Runnable {@ Override public void run () {throw new NullPointerException ();} public static void main (String [] args) {ExecutorService executorService = Executors. newCachedThreadPool (); try {System. out. println ("execution thread"); executorService.exe cute (new ExceptionThread ();} catch (Exception e) {e. printStackTrace (); System. out. println ("Capture exceptions ");}}}
The above Code does not capture thread exceptions in the main method. How can we capture thread exceptions?

Let's take a look at this code.

/*** Define the content of the exception Thread */class MyExceptionThread implements Thread. uncaughtExceptionHandler {@ Override public void uncaughtException (Thread t, Throwable e) {// The business processing after capturing exceptions is placed here System. out. println ("the caught exception information is as follows"); System. out. println (e) ;}}/*** defines the abnormal Thread factory */class ExceptionThreadFactory implements ThreadFactory {@ Override public Thread newThread (Runnable r) {Thread thread = new Thread (r); // This is the setting for capturing exceptions. Thread. setUncaughtExceptionHandler (new MyExceptionThread (); return thread ;}/ *** running Thread */class ExceptionThread2 implements Runnable {@ Override public void run () {try {thread. sleep (1000l);} catch (InterruptedException e) {e. printStackTrace () ;}// throw exception throw new NullPointerException ();} public static void main (String [] args) {// use our own ExceptionThreadFactory thread factory, construct the thread pool ExecutorService executorService = Executors. newCachedThreadPool (new ExceptionThreadFactory (); try {System. out. println ("execution thread"); // start three threads: executorService.exe cute (new ExceptionThread2 (); executorService.exe cute (new ExceptionThread2 ()); executorService.exe cute (new ExceptionThread2 ();} catch (Exception e) {e. printStackTrace (); System. out. println ("Capture exceptions ");}}}
The output above is
The exception information captured by the execution thread is as follows: java. lang. NullPointerException: The exception information captured by java. lang. NullPointerException is as follows: java. lang. NullPointerException
The conclusion is: the main () method still does not capture the thread exception. Of course, this setting is reasonable. Each thread now has its own exception capture mechanism. How can this problem be solved? In a word, it should be declared when the thread is created ~

Close!


How does Java Swing automatically start the second thread after the execution of the first thread ends?

Class one {
Public One (){
Start ();

}

Void start (){
Logic ();
New Thread (...). start (); // open a new Thread

}

Void logic (){
// What to do on the first page
......

}

}
Isn't that the case?

Java Exception Handling thread Problems

Use this interface to try java. util. concurrent. ScheduledExecutorService. When the connection fails, start the following task and connect again.
Import static java. util. concurrent. TimeUnit .*;
Class BeeperControl {
Private final ScheduledExecutorService scheduler =
Executors. newScheduledThreadPool (1 );

Public void beepForAnHour (){
Final Runnable beeper = new Runnable (){
Public void run () {System. out. println ("beep ");}
};
Final ScheduledFuture <?> BeeperHandle =
Scheder. scheduleAtFixedRate (beeper, 10, 10, SECONDS );
Schedable. schedule (new Runnable (){
Public void run () {beeperHandle. cancel (true );}
}, 60*60, SECONDS );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.