foreground and background threads for Java threads

Source: Internet
Author: User

/*
* For Java programs, as long as there is a foreground thread, then the process is not finished
* If there is no foreground thread or all of it is a background thread, then the process ends
* Set a thread to be a background thread setdeamon (true); if a Setdeamon (false)
If False or by default (that is, not set), it means that the thread is a foreground thread
*
*
*
*/


Class Lesson5testdemo1
{
public static void Main (String [] agrs)
{
When our main thread ends, our process is not finished, because we still have a foreground thread that is still executing
New Lesson5testdemo2 (). Start ();
When we set up this thread as the background of the process, then the end of our main thread, then our process is over, because there is no foreground thread
Thread tt = new Lesson5testdemo2 ();
Tt.setdaemon (TRUE);
Tt.start ();
System.out.println ("Lesson5testdemo1:" + thread.currentthread (). GetName ());
}
}

Class Lesson5testdemo2 extends Thread
{
 public void run ()
 {
  while (True)
& nbsp; {
   system.out.println ("Lesson5testdemo2:" + thread.currentthread (). GetName ());
 &NBSP}
 }

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.