rspec/rules/S3014/java/rule.adoc
2022-02-04 16:28:24 +00:00

33 lines
1.1 KiB
Plaintext

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();
----
== See
* 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)
include::message.adoc[]
endif::env-github,rspecator-view[]