🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The ThreadGroup class in Java is used to group multiple threads into a single unit, allowing for easier management and control of thread collections.
Table of Contents
- What is the
ThreadGroupClass? - Common Methods
- Examples of Using the
ThreadGroupClass - Conclusion
1. What is the ThreadGroup Class?
The ThreadGroup class provides a mechanism to group threads, making it easier to manage multiple threads simultaneously. It allows operations to be performed on all threads within the group.
2. Common Methods
ThreadGroup(String name): Creates a new thread group with a specified name.activeCount(): Returns the number of active threads in the thread group.activeGroupCount(): Returns the number of active groups in the thread group.enumerate(Thread[] list): Copies references to every active thread in the group into the specified array.getName(): Returns the name of the thread group.interrupt(): Interrupts all threads in the thread group.list(): Prints information about the thread group to the standard output.parentOf(ThreadGroup g): Tests if this thread group is a parent of the specified thread group.
3. Examples of Using the ThreadGroup Class
Example 1: Creating a ThreadGroup
This example demonstrates how to create a thread group and add threads to it.
public class ThreadGroupExample {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("ExampleGroup");
Thread thread1 = new Thread(group, () -> {
System.out.println(Thread.currentThread().getName() + " is running.");
}, "Thread-1");
Thread thread2 = new Thread(group, () -> {
System.out.println(Thread.currentThread().getName() + " is running.");
}, "Thread-2");
thread1.start();
thread2.start();
}
}
Output:
Thread-1 is running.
Thread-2 is running.
Example 2: Getting Active Thread Count
This example shows how to retrieve the number of active threads in a thread group.
public class ActiveCountExample {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("ActiveCountGroup");
new Thread(group, () -> {}).start();
new Thread(group, () -> {}).start();
System.out.println("Active threads in group: " + group.activeCount());
}
}
Output:
Active threads in group: 1
Example 3: Interrupting Threads in a Group
This example demonstrates interrupting all threads in a thread group.
public class InterruptExample {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("InterruptGroup");
Thread thread = new Thread(group, () -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " was interrupted.");
}
});
thread.start();
group.interrupt();
}
}
Output:
Thread-0 was interrupted.
Example 4: Listing Threads in a Group
This example shows how to list all threads in a thread group.
public class ListExample {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("ListGroup");
new Thread(group, () -> {}).start();
new Thread(group, () -> {}).start();
group.list(); // Prints information about the group
}
}
Output:
java.lang.ThreadGroup[name=ListGroup,maxpri=10]
Thread[#21,Thread-1,5,]
Example 5: Checking Parent of a ThreadGroup
This example checks if one thread group is a parent of another.
public class ParentOfExample {
public static void main(String[] args) {
ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
ThreadGroup childGroup = new ThreadGroup(parentGroup, "ChildGroup");
System.out.println("Is ParentGroup parent of ChildGroup? " + parentGroup.parentOf(childGroup));
}
}
Output:
Is ParentGroup parent of ChildGroup? true
4. Conclusion
The ThreadGroup class in Java provides a convenient way to manage multiple threads together. By grouping threads, developers can perform operations on entire groups, simplifying thread management and control in Java applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment