Java concurrent Programming (three) background thread (Daemon thread)

Source: Internet
Author: User

Background thread, daemon thread (Daemon thread)

The so-called background thread means that this thread is not part of the program, so when all non-background threads end, the program terminates and kills all the background threads in the process. The thread is set to a background thread by Setdaemon (true).

 Packagecom.csdhsm.concurrent;ImportJava.util.concurrent.TimeUnit;/*** @Title: Simpledaemonthreaddemo.java * @Package: Com.csdhsm.concurrent * @Description to test the Daemon Thread * /c5>@authorHan * @date 2016-4-19 pm 3:56:51 *@versionV1.0*/        Public classSimpledaemonthreaddemoImplementsrunnable{@Override Public voidrun () { while(true) {System.out.println (Thread.CurrentThread ()); Try{TimeUnit.MILLISECONDS.sleep (50); } Catch(interruptedexception e) {System.out.println ("Sleep is intrrupted!"); }        }    }         Public Static voidMain (string[] args) { for(inti = 0; I < 10; i++) {Thread T=NewThread (NewSimpledaemonthreaddemo ()); //set the thread to daemonT.setdaemon (true);        T.start (); }                Try {            //wait other thread to runTimeUnit.MILLISECONDS.sleep (30); } Catch(Interruptedexception e) {} System.out.println ("Main thread is Over"); }}
Results

It is clear to see that when the main thread ends, the other threads are no longer running.

Java concurrent Programming (three) background thread (Daemon 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.