Thread Group in Java
A thread group in Java is a way to group multiple threads into a single unit. It allows you to manage threads collectively, such as starting, interrupting, or checking the status of all threads in the group. Thread groups are part of the java.lang package and are represented by the ThreadGroup class.
Creating a Thread Group
We can create a thread group using the ThreadGroup class. Threads can be added to a group by specifying the group in the thread's constructor.
Example:
public class ThreadGroupExample {
public static void main(String[] args) {
// Create a thread group
ThreadGroup group = new ThreadGroup("MyThreadGroup");
// Create threads and add them to the group
Thread t1 = new Thread(group, () -> System.out.println("Thread 1 is running"), "Thread-1");
Thread t2 = new Thread(group, () -> System.out.println("Thread 2 is running"), "Thread-2");
t1.start();
t2.start();
// Display information about the thread group
System.out.println("Thread Group Name: " + group.getName());
group.list(); // Lists all threads in the group
}
}
Output:
Thread 1 is running
Thread 2 is running
Thread Group Name: MyThreadGroup
java.lang.ThreadGroup[name=MyThreadGroup,maxpri=10] Thread[Thread-1,5,MyThreadGroup] Thread[Thread-2,5,MyThreadGroup]
Key Methods of the ThreadGroup Class
- getName():
- Returns the name of the thread group.
- activeCount():
- Returns the number of active threads in the group.
- list():
- Prints information about the thread group and its threads.
- interrupt():
- Interrupts all threads in the group.
- setMaxPriority(int priority):
- Sets the maximum priority for threads in the group.
Example: Managing Threads with a Thread Group
public class ThreadGroupManagement {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("WorkerGroup");
Runnable task = () -> {
System.out.println(Thread.currentThread().getName() + " is running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " was interrupted");
}
};
Thread t1 = new Thread(group, task, "Worker-1");
Thread t2 = new Thread(group, task, "Worker-2");
Thread t3 = new Thread(group, task, "Worker-3");
t1.start();
t2.start();
t3.start();
System.out.println("Active threads in group: " + group.activeCount());
// Interrupt all threads in the group
group.interrupt();
}
}
Output:
Worker-1 is running
Worker-2 is running
Worker-3 is running
Active threads in group: 3
Worker-1 was interrupted
Worker-2 was interrupted
Worker-3 was interrupted
Advantages of Thread Groups
- Simplified Management: Thread groups allow you to manage multiple threads collectively.
- Centralized Control: You can interrupt or set priorities for all threads in a group at once.
- Organization: Thread groups help organize threads logically, making the program easier to understand.
Limitations of Thread Groups
- Deprecated Features: Some features of thread groups, such as stopping threads, are deprecated due to safety concerns.
- Limited Use: Modern applications often use the
ExecutorServicefrom thejava.util.concurrentpackage for thread management.
