Constructor | Description |
---|---|
ThreadGroup(String name) | ThreadGroup(String name), creates a thread group with the given name. This means you can assign a unique name to the thread group for identification and organization purposes. For example, you could make a thread group called "MyThreadGroup" to collect threads that are associated with a particular method or task. |
ThreadGroup(ThreadGroup parent, String name) | ThreadGroup(ThreadGroup parent, String name), allows you to create a thread group with a specified parent group and name. This constructor is useful when you want to create a hierarchical structure of thread groups. The parent group parameter specifies the parent thread group to which the new thread group should belong. By organizing thread groups into a hierarchy, you can effectively manage and control the behavior of threads within the application. |
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 100; i++){ try{ Thread.sleep(100); } catch (InterruptedException e){ e.printStackTrace(); } } } } public class Main{ public static void main(String arg[]){ ThreadGroup dockertpoint = new ThreadGroup("Parent's thread group"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First", dockertpoint); System.out.println("Starting First Thread"); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second", dockertpoint); System.out.println("Starting Second Thread"); System.out.println("N.o of active thread: "+ dockertpoint.activeCount()); } }
Output:
Starting First Thread
Starting Second Thread
N.o of active thread: 2
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); dockertpoint.checkAccess(); System.out.println(dockertpoint.getName() + " has access"); dockertpointChild.checkAccess(); System.out.println(dockertpointChild.getName() + " has access"); } }
Output:
Starting First Thread
Starting Second Thread
Parent thread has access
ThreadGroup child thread has access
First Thread execution finished
Second Thread execution finished
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); System.out.println("N.o of active thread "+dockertpoint.activeGroupCount()); } }
Output:
Starting First Thread
Starting Second Thread
N.o of active thread 1
First Thread execution finished
Second Thread execution finished
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); thread1.join(); thread2.join(); dockertpointChild.destroy(); System.out.println(dockertpointChild.getName() + "child thread destroyed"); dockertpoint.destroy(); System.out.println(dockertpoint.getName() + " parent thread destroyed"); } }
Output:
Starting First Thread
Starting Second Thread
Second Thread execution finished
First Thread execution finished
ThreadGroup child threadchild thread destroyed
ThreadGroup Parent thread parent thread destroyed
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); Thread[] Threadgrp = new Thread[dockertpoint.activeCount()]; int cnt = dockertpoint.enumerate(Threadgrp); for (int i = 0; i < cnt; i++){ System.out.println("Thread " + Threadgrp[i].getName() + " is found"); } } }
Output:
Starting First Thread
Starting Second Thread
Thread First Thread is found
Thread Second Thread is found
First Thread execution finished
Second Thread execution finished
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); System.out.println("The parent's highest priority ThreadGroup = "+ dockertpoint.getMaxPriority()); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); } }
Output:
The parent's highest priority ThreadGroup = 10
Starting First Thread
Starting Second Thread
First Thread execution finished
Second Thread execution finished
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); System.out.println( "The ParentThreadGroup for " + dockertpoint.getName() +" is " + dockertpoint.getParent().getName()); System.out.println( "The ParentThreadGroup for " + dockertpointChild.getName() + " is " + dockertpointChild.getParent().getName()); } }
Output:
Starting First Thread
Starting Second Thread
The ParentThreadGroup for ThreadGroup Parent thread is main
The ParentThreadGroup for ThreadGroup child thread is ThreadGroup Parent thread
First Thread execution finished
Second Thread execution finished
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); dockertpoint.interrupt(); } }
Output:
Starting First Thread
Starting Second Thread
The ParentThreadGroup for ThreadGroup Parent thread is main
The ParentThreadGroup for ThreadGroup child thread is ThreadGroup Parent thread
First Thread execution finished
Second Thread execution finished
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); if (dockertpoint.isDaemon() == true) System.out.println("This is a daemon group."); else System.out.println("This isn't a daemon group."); } }
Output:
Starting First Thread
Starting Second Thread
This isn't a daemon group.
First Thread execution finished
Second Thread execution finished
class ThreadGroupDemo extends Thread{ ThreadGroupDemo(String str, ThreadGroup threadgroup){ super(threadgroup, str); start(); } public void run(){ for (int i = 0; i < 1000; i++){ try{ Thread.sleep(20); } catch (InterruptedException e){ e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() +" execution finished"); } } public class Main{ public static void main(String arg[]) throws InterruptedException,SecurityException{ ThreadGroup dockertpoint = new ThreadGroup("ThreadGroup Parent thread"); ThreadGroup dockertpointChild = new ThreadGroup(dockertpoint, "ThreadGroup child thread"); ThreadGroupDemo thread1 = new ThreadGroupDemo("First Thread", dockertpoint); System.out.println("Starting First Thread"+ ""); ThreadGroupDemo thread2 = new ThreadGroupDemo("Second Thread", dockertpoint); System.out.println("Starting Second Thread"); if (dockertpoint.isDestroyed() == true) System.out.println("This group is now destroyed"); else System.out.println("This group isn't destroyed"); } }
Output:
Starting First Thread
Starting Second Thread
This group isn't destroyed
First Thread execution finished
Second Thread execution finished
Police Colony
Patna, Bihar
India
Email:
Post your comment