Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Thread Group in Java

  • In Java, a group of threads is created using the ThreadGroup class. This class provides a practical way to control multiple threads collectively. It proves to be particularly useful when you need to pause or resume a set of interconnected threads. Each thread group, except for the initial thread group, has a parent in the thread group hierarchy.
    • A thread can access data related to its own thread group, but it is prohibited from doing the same for its parent thread group or any other thread groups. By encapsulating threads into thread groups, Java provides a structured approach to managing and organizing threads in a hierarchical manner.
Table Of Content

  • Thread Group in Java
  • Constructors of Thread Group class
  • Methods of Thread Group class







Constructors of Thread Group class


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.

  • In Java, thread groups provide a way to group threads together and manage them collectively. They can be used to set properties and behaviors that apply to all threads within the group. For example, you can set the thread group's priority, daemon status, or exception handler. Thread groups also allow you to enumerate the threads within the group and perform operations on them as a whole.

Methods of Thread Group class


  • 1.int activeCount() : The method's output includes the total number of threads in the group as well as any groups that this thread is the parent of.
  • Syntax: public int activeCount()
  • This method gives a rough estimate of how many active threads are present in this thread group and any other thread groups that have this thread group as an ancestor.
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 

  • 2.void checkAccess(): The security manager conducts a verification process to determine whether the invoking thread possesses the necessary authorization to access and/or modify the group associated with the checkAccess() method invocation.
  • Syntax: final void checkAccess().
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 



  • 3.int activeGroupCount() : This particular method serves the purpose of providing an estimation of the number of active thread groups within the current thread group. It is a valuable function that helps in managing and monitoring threads effectively.
  • Syntax: public int activeGroupCount()
  • The number of groups for which the invoking thread is the parent.
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 

  • 4. void destroy() : Destroys method is used to Destroy the thread group and any child groups that call this method
  • Syntax: public void destroy()



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 

  • 5. int enumerate(Thread group[]): This method adds the threads from the invoking thread group to the group array.
  • Syntax: public int enumerate(Thread group[])



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 

  • 6. int getMaxPriority() : getMaxPriority() returns the group's highest priority setting.
  • Syntax: final int getMaxPriority()
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 

  • 7. ThreadGroup getParent() : If the invoking ThreadGroup object has no parent, getParent() returns null. Otherwise, it returns the invoking object's parent.
  • Syntax: final ThreadGroup getParent()
  • This method returns the thread group's parent.The only thread group is the top-level thread group.
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
 

  • 8. void interrupt() : interrupt() calls all threads in the group's interrupt() methods.
  • Syntax: public final void interrupt()
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
 

  • 9. boolean isDaemon() : isDaemon() determines whether or not this thread group is a daemon thread group. When a daemon thread group's last thread or thread group is destroyed, it is automatically destroyed.
  • Syntax: public final boolean isDaemon()
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 

  • 10. boolean isDestroyed() : isDestroyed() This method determines whether or not this thread group has been destroyed.
  • Syntax: public boolean isDestroyed()
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
 
Shutdown hook in java Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.