
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
38 lines
1.2 KiB
Plaintext
38 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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
|
|
|
|
[source,java]
|
|
----
|
|
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();
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://wiki.sei.cmu.edu/confluence/x/YzdGBQ[CERT, THI01-J.] - Do not invoke ThreadGroup methods
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this use of "ThreadGroup". Prefer the use of "ThreadPoolExecutor".
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|