ThreadGroup method isDaemon() in Java
The ThreadGroup method isDaemon() used to determine whether a thread group is a daemon thread group. It returns a boolean value true, if thread group is daemon group. A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.
Note : 'isDaemon()' is deprecated and marked for removal, from version 16.
Syntax
 1 public final boolean isDaemon()
return : It returns a boolean value that specifies whether thread group is daemon thread group or not.
Thread class
 1 package com.java;
 2 
 3 class CustomThread extends Thread {
 4 
 5     public void run() {
 6         System.out.println("Starting thread : " + this.getName());
 7     }
 8 }
A CustomThread class is created by extending a Thread class and overriding a run() method. Once a thread instance method start() called, it executes run() method.
ThreadGroup method isDaemon()
 1 package com.java;
 2 
 3 public class Example extends Thread {
 4 
 5     public static void main(String[] args) {
 6 
 7         CustomThread ct = new CustomThread();
 8         ThreadGroup tg = new ThreadGroup("TG1");
 9 
 10         // assigning thread group to thread
 11         Thread t1 = new Thread(tg, ct, "T1");
 12         t1.start();
 13 
 14         System.out.println("Is ThreadGroup isDaemon : " + tg.isDaemon());
 15     }
 16 }
In the above example, a CustomThread and ThreadGroup objects are created. An instance of Thread class is created by passing a thread group, thread object and thread name. A thread starts by calling start() method that executes CustomThread run() method. A ThreadGroup method isDaemon() is called that returns false, as it is not a daemon thread group.
Output
 1 Starting thread : Thread-0
 2 Is ThreadGroup isDaemon : false

Example 2 with internal thread class

Example 2 with internal thread class
 1 package com.java;
 2 
 3 class CustomThread extends Thread {
 4 
 5     public void run() {
 6         System.out.println("CustomThread thread started...");
 7     }
 8 }
 9 
 10 public class Example {
 11 
 12     public static void main(String[] args) {
 13         CustomThread ct = new CustomThread();
 14         ThreadGroup tg = new ThreadGroup("TG1");
 15         tg.setDaemon(true);
 16         // assigning thread group to thread
 17         Thread t1 = new Thread(tg, ct, "T1");
 18         t1.start();
 19 
 20         System.out.println("Is ThreadGroup isDaemon : " + tg.isDaemon());
 21     }
 22 }
In the above example, a CustomThread class is created by extending a Thread class. A Thread class is created by passing a CustomThread class and ThreadGroup class. A ThreadGroup method setDaemon() method called by passing a boolean value true. A ThreadGroup method isDaemon() called that returns boolean value true.
Output
 1 CustomThread thread started...
 2 Is ThreadGroup isDaemon : true
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us