Avoid ThreadGroup
ID |
java.avoid_threadgroup |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Java |
Tags |
best-practice, concurrency |
Description
Reports usage of java.lang.ThreadGroup in field declarations, local variables, parameters, and return types. ThreadGroup is a legacy concurrency primitive that has been effectively superseded by the java.util.concurrent framework.
Rationale
ThreadGroup was part of the original Java threading model but has significant design flaws. Several of its methods (destroy(), suspend(), resume(), stop()) are deprecated, and the class offers little value over modern alternatives. Its exception-handling semantics via uncaughtException() are better served by Thread.UncaughtExceptionHandler, and its grouping semantics are better served by ExecutorService or ForkJoinPool.
// Legacy pattern -- fragile and limited
ThreadGroup group = new ThreadGroup("workers");
Thread t = new Thread(group, task, "worker-1");
Remediation
Use ExecutorService, ForkJoinPool, or virtual threads (Java 21+) for thread management:
// Modern pattern -- robust and flexible
ExecutorService executor = Executors.newFixedThreadPool(4);
executor.submit(task);
executor.shutdown();