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
ThreadGroup
Class? - Common Methods
- Examples of Using the
ThreadGroup
Class - 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.
Comments
Post a Comment
Leave Comment