The source code to check whether a thread group is destroyed or not is given below. The given program is compiled and executed successfully.
// Java program to check whether a thread group
// is destroyed or not
class MyThread extends Thread {
MyThread(String threadname, ThreadGroup tg) {
super(tg, threadname);
start();
}
public void run() {
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + " is finished");
} catch (Exception e) {
System.out.println(e);
}
}
}
public class Main {
public static void main(String[] args) {
try {
ThreadGroup group = new ThreadGroup("Parent thread");
MyThread t1 = new MyThread("Child Thread1", group);
System.out.println(t1.getName() + " is started");
MyThread t2 = new MyThread("Child Thread2", group);
System.out.println(t2.getName() + " is started");
t1.join();
t2.join();
if (group.isDestroyed() == true)
System.out.println("Group is destroyed");
else
System.out.println("Group is not destroyed");
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Child Thread1 is started
Child Thread2 is started
Child Thread1 is finished
Child Thread2 is finished
Group is not destroyed
Explanation:
In the above program, we created two classes MyThread and Main. We created MyThread class by extending the Thread class.
The Main class contains a main() method. The main() method is the entry point for the program. Here, we created an object of ThreadGroup class and added the child thread into the thread group. After that, we checked whether a thread group is destroyed or not using the isDestroyed() method. It returns true when a thread group is destroyed, otherwise, it returns false.
Program/Source Code:
The source code to check whether a thread group is destroyed or not is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two classes MyThread and Main. We created MyThread class by extending the Thread class.
The Main class contains a main() method. The main() method is the entry point for the program. Here, we created an object of ThreadGroup class and added the child thread into the thread group. After that, we checked whether a thread group is destroyed or not using the isDestroyed() method. It returns true when a thread group is destroyed, otherwise, it returns false.