Java concurrency, background threads

Source: Internet
Author: User

1  Packagecom.xt.thinks21_2;2 3 ImportJava.util.concurrent.TimeUnit;4 5 /**6 * Background Threading Test7  * 8  * @authorAdministrator9  *Ten  */ One  Public classSimpledaemontestImplementsRunnable { A  - @Override -      Public voidrun () { the         //TODO auto-generated Method Stub -          while(true) { -             Try { -TimeUnit.MILLISECONDS.sleep (100); +System.out.println (Thread.CurrentThread () + ":" + This); -}Catch(interruptedexception e) { +                 //TODO auto-generated Catch block A e.printstacktrace (); at             } -         } -     } -  -      Public Static voidMain (string[] args) { -          for(inti = 0; I < 10; i++) { inThread T =NewThread (Newsimpledaemontest ()); -T.setdaemon (true);//set to Background thread, if not set as non-background thread, will print thread information wirelessly to T.start (); +         } -System.out.println ("All Deamon is start!"); the         Try { *TimeUnit.MILLISECONDS.sleep (175);//175>100, every thread information is printed once $}Catch(interruptedexception e) {Panax Notoginseng             //TODO auto-generated Catch block - e.printstacktrace (); the         } +     } A  the}

A background thread differs from a method that is not a background thread: The background thread is actively setting Thread.setdeamon (TRUE)

When a non-background thread ends, the background thread forces the end

Java concurrency, background 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.