6. daemon thread

 Java, there are two threads, one is the user thread, the other is the guardian of the thread.

 When the process does not exist or stop the main thread, the thread daemon will be stopped.

 Use setDaemon (true) method to set a daemon thread

 

 

**

 *

 * What is a daemon thread?-Threaded daemon process thread (the main thread hung up) daemon threads will be automatically destroyed.

 *

 * @ClassDesc : Functional Description :( daemon thread)

 */

public class DaemonThread {

 

  public static void main(String[] args) {

    Thread thread = new Thread(new Runnable() {

      @Override

      public void run() {

        while (true) {

          try {

            Thread.sleep(100);

          } catch (Exception e) {

            // TODO: handle exception

          }

          . System OUT .println ( "I am a child thread ...");

        }

      }

    });

    thread.setDaemon(true);

    thread.start();

    for (int i = 0; i < 10; i++) {

      try {

        Thread.sleep(100);

      } catch (Exception e) {

 

      }

      . System OUT .println ( "I am the main thread");

    }

    . System OUT .println ( "main thread is finished!");

  }

Guess you like

Origin www.cnblogs.com/goldlong/p/10953903.html