1.11 Daemon Threads

Source: Internet
Author: User

There are 2 threads in Java, 1 are daemon threads, 1 are non-daemon threads

A daemon thread is a special thread that, when a non-daemon thread is present in the thread, is automatically destroyed by the daemon thread, and the typical daemon thread is the garbage collection thread.

Test as follows

1  PackageCom.cky.daemon;2 3 /**4 * Created by Edison on 2017/12/3.5  */6  Public classMythreadsextendsthread{7     Private intI=0;8 9 @OverrideTen      Public voidrun () { One         Super. Run (); A         Try { -              while(true) { -i++; theSystem.out.println ("i=" +i); -Thread.Sleep (1000); -             } -}Catch(interruptedexception e) { + e.printstacktrace (); -         } +     } A}
 PackageCom.cky.daemon;/*** Created by Edison on 2017/12/3.*/ Public classTesst11 { Public Static voidMain (string[] args) {Try{mythreads mythreads=Newmythreads (); //set it as the daemon thread, default to False, non-daemon threadMythreads.setdaemon (true);            Mythreads.start (); Thread.Sleep (5000); System.out.println ("I'm leaving the thread object and I'm not printing anymore."); } Catch(interruptedexception e) {e.printstacktrace (); }    }}
I=1i=2i=3i=4i=5i=60

1.11 Daemon Threads

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.