Whether finally runs in the thread

Source: Internet
Author: User
Package COM. partner4java; import Java. util. concurrent. timeunit;/*** when you set the background to run, and the thread does not report an error or perform Cleanup before the running ends, no more finally printed * @ author partner4java **/public class Test2 {public static void main (string [] ARGs) throws interruptedexception {// test1 (); // Test2 (); // test3 (); test4 ();} Private Static void test1 () throws interruptedexception {thread = new thread (New runnable () {@ overridepublic void run () {try {system. out. println ("try"); timeunit. milliseconds. sleep (1, 5000); thread. currentthread (). interrupt (); system. out. println ("Try last");} catch (exception e) {e. printstacktrace ();} finally {system. out. println ("finally") ;}}); thread. setdaemon (true); thread. start (); timeunit. milliseconds. sleep (1, 1000); system. out. println ("hello"); // background print: // try // Hello} Private Static void Test2 () throws interruptedexception {thread = new thread (New runnable () {@ overridepublic void run () {try {system. out. println ("try"); thread. currentthread (). interrupt (); system. out. println ("Try last");} catch (exception e) {e. printstacktrace ();} finally {system. out. println ("finally") ;}}); thread. setdaemon (true); thread. start (); timeunit. milliseconds. sleep (1, 1000); system. out. println ("hello"); // background print: // try last // finally // Hello} Private Static void test3 () {thread = new thread (New runnable () {@ overridepublic void run () {try {system. out. println ("try"); timeunit. milliseconds. sleep (1, 5000); system. out. println ("Try last");} catch (exception e) {e. printstacktrace ();} finally {system. out. println ("finally") ;}}); thread. setdaemon (true); thread. start (); thread. interrupt (); system. out. println ("hello"); // background printing: // Java. lang. interruptedexception: sleep interrupted //..... // Hello // try // finally} Private Static void test4 () {thread = new thread (New runnable () {@ overridepublic void run () {try {system. out. println ("try"); timeunit. milliseconds. sleep (1, 5000); system. out. println ("Try last");} catch (exception e) {e. printstacktrace ();} finally {system. out. println ("finally") ;}}); thread. setdaemon (true); thread. start (); int I = 1/0; system. out. println ("hello"); // background printing: // Java. lang. arithmeticexception:/by zero //... // try }}

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.