...
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 | ||
---|---|---|
| ||
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 |
---|---|---|---|---|---|
THI05 THI06-J | low | probable | medium | P4 | L3 |
Related Guidelines
POS47-C. Do not use threads that can be canceled asynchronously | |
CWE ID 705, "Incorrect Control Flow Scoping " |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a035a81ccaf91c87-46cd68f6-4146461e-81c683c6-14193b74cf7e2bd848e55f9c"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class Thread, method | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ed91be43a3572cf2-e534cbef-447d42ff-974786a1-8b8b5a5114c33392b750edab"><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="a2f6d107460bc8b3-a05bb9c0-485342cb-b4ceb5f6-28fe17acfae4b5280631545b"><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="c1b797897c728f67-58ba81a6-4c684714-8d5784b5-76cf87687d31ec8da740d90e"><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="561d1de8fb43c53a-773b1967-4c224677-95399432-cc475da7ac6fa175e6b051f7"><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="1bd18262514ece5b-0bcc9b91-4ddc4e49-97fc92be-502dd86f5d7e9da236929dff"><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="bbd4b8b9304fa427-4a2b506a-455b491e-ac22879a-e9cf5f7f605b2e9933b594e1"><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) THI05THI06-J. Ensure that threads performing blocking operations can be terminated