Home » Java programming language

Java ThreadGroup isDaemon() method with example

ThreadGroup Class isDaemon() method: Here, we are going to learn about the isDaemon() method of ThreadGroup Class with its syntax and example.
Submitted by Preeti Jain, on December 23, 2019

ThreadGroup Class isDaemon() method

  • isDaemon() method is available in java.lang package.
  • isDaemon() method is used to check whether this thread group is a daemon thread group or not.
  • As we know that daemon thread group is destroyed implicitly when its last thread is destroyed or when its last thread group is destroyed.
  • isDaemon() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • isDaemon() method does not throw an exception at the time of checking the daemon behavior of this thread group.

Syntax:

    public final boolean isDaemon();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Boolean – it returns true when this thread group is a daemon thread group otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean isDaemon()method of ThreadGroup 

public class Daemon implements Runnable {
    public static void main(String[] args) {
        Daemon damn = new Daemon();
        damn.isDaemn();
    }

    public void isDaemn() {
        try {
            // Create two thread group and the named are base
            // and derived

            ThreadGroup base = new ThreadGroup("Base ThreadGroup");
            ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");

            // create two threads
            Thread th1 = new Thread(base, this);
            Thread th2 = new Thread(derived, this);

            // By using getName() method is to retrieve the
            // name of the thread th1
            System.out.println(th1.getName() + " " + "begins.....");

            // By using start() method is to start its execution 
            // of thread th1
            th1.start();

            // By using getName() method is to retrieve the
            // name of the thread th2
            System.out.println(th2.getName() + " " + "begins.....");

            // By using start() method is to start its execution 
            // of thread th2
            th2.start();

            System.out.println();

            // By using isDaemon() method is to check the
            // daemon status of the thread group
            boolean b1 = base.isDaemon();
            boolean b2 = derived.isDaemon();

            System.out.println("Is" + " " + base.getName() + " " + "daemon threadgroup:" + " " + b1);
            System.out.println("Is" + " " + derived.getName() + " " + "daemon threadgroup:" + " " + b2);


            // By using join() method is to wait the current
            // thread till complete execution of another
            // thread

            th1.join();
            th2.join();

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

    }

    // Override run()
    public void run() {

        for (int k = 0; k < 100; ++k)
            ++k;

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

}

Output

Thread-0 begins.....
Thread-0 ends.....
Thread-1 begins.....

Thread-1 ends.....
Is Base ThreadGroup daemon threadgroup: false
Is Derived ThreadGroup daemon threadgroup: false





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.