Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Noncompliant Code Example (shutting down thread pools)

Wiki Markup
According to the Java API \[[API 06|AA. Java References#API 06]\], interface {{java.util.concurrent.ExecutorService}}, method {{shutdownNow()}} documentation:

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.

Consequently, care must be taken so that only interruptible tasks are submitted to the thread pool.

Noncompliant Code Example (shutting down thread pools)

This noncompliant code example uses the SocketReader class defined earlier in Compliant Solution (close socket connection) of the guideline CON24-J. Ensure that threads and tasks performing blocking operations can be terminated and submits it as a task to a thread pool defined in class PoolService.

...

Tasks that do not support interruption using Thread.interrupt() should not be submitted to a thread pool. This compliant solution submits the interruptible version of SocketReader discussed in Compliant Solution (interruptible channel) of the guideline CON24-J. Ensure that threads and tasks performing blocking operations can be terminated, to the thread pool.

Code Block
bgColor#ccccff
class PoolService {
  // ...
}

class SocketReader implements Runnable {
  private final SocketChannel sc;
  // ...
}

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

CON35 CON36- J

low

probable

medium

P4

L3

Automated Detection

...