Versions Compared

Key

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

...

More information about deprecated methods is available in rule MET02-J. Do not use deprecated or obsolete classes or methods. Also, refer to rule ERR09-J. Do not allow untrusted code to terminate the JVM for information on preventing data corruption when the JVM is shut down abruptly.

Noncompliant Code Example (Deprecated Thread.stop())

This noncompliant code example shows a thread that fills a vector with pseudo-random numbers. The thread is forcefully stopped after a given amount of time.

...

Wiki Markup
However, the {{Thread.stop()}} method causes the thread to stop what it is doing and throw a {{ThreadDeath}} exception. All acquired locks are subsequently released \[[API 2006|AA. Bibliography#API 06]\]. If the thread were in the process of adding a new integer to the vector when it was stopped, the vector would become accessible while it is in an inconsistent state. This could result in {{Vector.size()}} returning an incorrect element count, for example, because the element count is incremented after adding the element.

Compliant Solution (volatile flag)

This compliant solution uses a volatile flag to request thread termination. The shutdown() accessor method is used to set the flag to true. The thread's run() method polls the done flag and terminates when it is set.

Code Block
bgColor#ccccff
public final class Container implements Runnable {
  private final Vector<Integer> vector = new Vector<Integer>(1000);
  private volatile boolean done = false;

  public Vector<Integer> getVector() {
    return vector;
  }

  public void shutdown() {
    done = true;
  }

  @Override public synchronized void run() {
    Random number = new Random(123L);
    int i = vector.capacity();
    while (!done && i > 0) {
      vector.add(number.nextInt(100));
      i--;
    }
  }

  public static void main(String[] args) throws InterruptedException {
    Container container = new Container();
    Thread thread = new Thread(container);
    thread.start();
    Thread.sleep(5000);
    container.shutdown();
  }
}

Compliant Solution (Interruptible)

In this compliant solution, the Thread.interrupt() method is called from main() to terminate the thread. Invoking Thread.interrupt() sets an internal interrupt status flag. The thread polls that flag using the Thread.interrupted() method, which both returns true if the current thread has been interrupted and also clears the interrupt status flag.

...

A thread may use interruption for performing tasks other than cancellation and shutdown. Consequently, a thread should be interrupted only when its interruption policy is known in advance. Failure to do so can result in failed interruption requests.

Compliant Solution (Runtime Permission stopThread)

Removing the default permission java.lang.RuntimePermission stopThread permission from the security policy file prevents threads from being stopped using the Thread.stop() method. This approach is discouraged for trusted, custom-developed code that uses that method because the existing design presumably depends upon the ability of the system to perform this action. Furthermore, the system might fail to correctly handle the resulting exception. In these cases, programmers should implement an alternate design corresponding to one of the other compliant solutions described in this rule.

Risk Assessment

Forcing a thread to stop can result in inconsistent object state. Critical resources could also leak if clean-up operations are not carried out as required.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

THI06 THI05-J

low

probable

medium

P4

L3

Related Guidelines

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d53b01ab055d7cc7-b62ed4b0-46584678-ad5d9e68-ab0ec8670faf66da737ceca9"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

Class Thread, method stop, interface ExecutorService

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="89f0ba9e40e0e46e-f865e251-443e4aae-a2d0bc5b-2555437755b4a01729a3915b"><ac:plain-text-body><![CDATA[

[[Sun 1999

AA. Bibliography#Sun 99]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="53082dd10f56f7b3-0ae9de1a-44624479-8b979133-e4501ca30c3dc8cc7fb15682"><ac:plain-text-body><![CDATA[

[[Darwin 2004

AA. Bibliography#Darwin 04]]

24.3 Stopping a Thread

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ed7e6347e220ea52-c6527c04-424c4fa8-865bac2a-31f2272da1d9ba9963c3ca90"><ac:plain-text-body><![CDATA[

[[JDK7 2008

AA. Bibliography#JDK7 08]]

Concurrency Utilities, More information: Java Thread Primitive Deprecation

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0d0b18e8cfde21bc-cfbc8454-4e924bd2-ba159760-27063f1830579486b8290b07"><ac:plain-text-body><![CDATA[

[[JPL 2006

AA. Bibliography#JPL 06]]

14.12.1. Don't stop and 23.3.3. Shutdown Strategies

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="00116e30be449bb6-26927389-49f34575-b9eba15b-6d9d7cc6c761ccc0dc2db3c4"><ac:plain-text-body><![CDATA[

[[JavaThreads 2004

AA. Bibliography#JavaThreads 04]]

2.4 Two Approaches to Stopping a Thread

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f66705597a57ab04-f888c573-494f4d52-9be19b41-b06581c2e544e48f6899e6a4"><ac:plain-text-body><![CDATA[

[[Goetz 2006

AA. Bibliography#Goetz 06]]

Chapter 7: Cancellation and shutdown

]]></ac:plain-text-body></ac:structured-macro>

...

THI02-J. Notify all waiting threads rather than a single thread      09. Thread APIs (THI)      THI06THI05-J. Ensure that threads performing blocking operations can be terminated