Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Remove this use of "ThreadGroup". Prefer the use of "ThreadPoolExecutor".
-
Default Severity:Blocker
-
Impact:High
-
Likelihood:High
-
Default Quality Profiles:Sonar way
-
Covered Languages:Java
-
Remediation Function:Constant/Issue
-
Constant Cost:45min
-
CERT:THI01-J.
-
PMD:AvoidThreadGroup
Description
There is little valid reason to use the methods of the ThreadGroup class. Some are deprecated (allowThreadSuspension(), resume(), stop(), and suspend()), some are obsolete, others aren't thread-safe, and still others are insecure (activeCount(), enumerate()) . For these reasons, any use of ThreadGroup is suspicious and should be avoided.
Compliant Solution
ThreadFactory threadFactory = Executors.defaultThreadFactory(); ThreadPoolExecutor executorPool = new ThreadPoolExecutor(3, 10, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), threadFactory); for (int i = 0; i < 10; i++) { executorPool.execute(new JobThread("Job: " + i)); } System.out.println(executorPool.getActiveCount()); // Compliant executorPool.shutdown();
See
- CERT, THI01-J. - Do not invoke ThreadGroup methods
Attachments
Issue Links
- is implemented by
-
SONARJAVA-2709 Rule S3014: "ThreadGroup" should not be used
-
- Closed
-