Java threads are divided into two types, the daemon thread and the user thread.

Source: Internet
Author: User

There are two types of threads in Java: The user thread and the daemon thread .

Set to User thread by Thread.setdaemon (false);

Set to the daemon thread by Thread.setdaemon (true).

If you do not set the secondary property, the user thread is the default.

Difference: The user thread will continue to run after the main thread ends, the JVM survives, and if there is no user thread at the end of the main thread, the JVM ends.

 Public classMytestextendsThread { Public voidrun () { for(inti=0;; i++){            Try{Thread.Sleep (1000); } Catch(Exection ex) {} System.out.println (i); }    }     Public Static voidmain (String [] args) {Mytest test=NewMytest (); Test.setdaemon (true);        Test.start (); System.out.println ("Isdaemon:" +Test.isdaemon ()); Try {            //Test.setdaemon (TRUE); accepts input, once the user input is received, the main thread ends, the daemon thread ends automatically, and the JVM ends. //Test.setdaemon (FALSE); accepts input, once the user input is received, the main thread ends, the user thread continues, that is, the JVM does not end. System.in.read (); } Catch(IOException ex) {} }}

Java threads are divided into two types, the daemon thread and the user thread.

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.