rspec/rules/S2122/java/rule.adoc
2023-08-11 16:49:04 +02:00

76 lines
2.7 KiB
Plaintext

== Why is this an issue?
`ThreadPoolExecutor` is an object that efficiently manages and controls the execution of multiple tasks in a thread pool.
A thread pool is a collection of pre-initialized threads ready to execute tasks.
Instead of creating a new thread for each task, which can be costly in terms of system resources, a thread pool reuses existing threads.
`java.util.concurrent.ScheduledThreadPoolExecutor` is an extension of `ThreadPoolExecutor` that can additionally schedule commands to run
after a given delay or to execute periodically.
`ScheduledThreadPoolExecutor` 's pool is sized with `corePoolSize`, so setting `corePoolSize` to zero means the executor will have no
threads and run nothing. `corePoolSize` should have a value greater than zero and valid for your tasks.
This rule detects instances where `corePoolSize` is set to zero via its setter or the object constructor.
=== Noncompliant code example
[source,java]
----
public void do(){
int poolSize = 5; // value greater than 0
ScheduledThreadPoolExecutor threadPool1 = new ScheduledThreadPoolExecutor(0); // Noncompliant
ScheduledThreadPoolExecutor threadPool2 = new ScheduledThreadPoolExecutor(poolSize);
threadPool2.setCorePoolSize(0); // Noncompliant
}
----
== Resources
=== Documentation
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/concurrent/ScheduledThreadPoolExecutor.html[Oracle Java SE - ScheduledThreadPoolExecutor]
=== Articles & blog posts
* https://engineering.zalando.com/posts/2019/04/how-to-set-an-ideal-thread-pool-size.html[Zalando - How to set an ideal thread pool size]
* https://www.baeldung.com/java-threadpooltaskexecutor-core-vs-max-poolsize[Baeldung - ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Increase the "corePoolSize".
'''
== Comments And Links
(visible only on this page)
=== on 8 Oct 2014, 19:12:56 Ann Campbell wrote:
\[~nicolas.peru] after writing this rule up, I noticed that its analog is commented-out:
/**
* Since you can change the number of core threads for a scheduled
* thread pool executor, disabling this for now
*
So... what do you think of a smarter implementation (I'd have to update the description) that detects when it's set to zero & left that way?
=== on 21 Oct 2014, 15:47:15 Nicolas Peru wrote:
This is way harder. The "left that way" part can concern a lot of code not under analysis scope.
=== on 21 Oct 2014, 20:01:32 Ann Campbell wrote:
Do you feel the rule has value as-is or should we scrap it as was done in FB?
=== on 4 Feb 2015, 08:59:04 Nicolas Peru wrote:
Let's reimplement it as what was done in FB. Scheduled for 3.0.
endif::env-github,rspecator-view[]