Java Data Type Tutorial - Java ThreadGroup.setDaemon(boolean daemon)








Syntax

ThreadGroup.setDaemon(boolean daemon) has the following syntax.

public final void setDaemon(boolean daemon)

Example

In the following code shows how to use ThreadGroup.setDaemon(boolean daemon) method.

public class Main {
  public static void main(String[] args) {
    new ThreadGroupDemo();
  }// w w w  .  j  a  va  2 s.  c om
}

class ThreadGroupDemo implements Runnable {
  public ThreadGroupDemo() {

    ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");
    pGroup.setDaemon(true);

    ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");
    cGroup.setDaemon(true);

    Thread t1 = new Thread(pGroup, this);
    System.out.println("Starting " + t1.getName());
    t1.start();

    Thread t2 = new Thread(cGroup, this);
    System.out.println("Starting " + t2.getName());
    t2.start();

    System.out.println("Is " + pGroup.getName() + " a daemon ThreadGroup? "
        + pGroup.isDaemon());
    System.out.println("Is " + cGroup.getName() + " a daemon ThreadGroup? "
        + cGroup.isDaemon());

  
  }

  public void run() {

    System.out.println(Thread.currentThread().getName()
        + " finished executing.");
  }
}

The code above generates the following result.