Versions Compared

Key

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

Threads preserve class invariants when they are allowed to exit normally. Programmers often attempt to terminate threads abruptly when they believe that the task is complete, the request has been canceled, or the program or JVM must shut down expeditiously.

Certain thread APIs were introduced to facilitate thread suspension, resumption, and termination but were later deprecated because of inherent design weaknesses. For example, the Thread.stop() method causes the thread to immediately throw a ThreadDeath exception, which usually stops the thread. More information about deprecated methods is available in rule MET02-J. Do not use deprecated or obsolete classes or methods.

Wiki Markup
Invoking {{Thread.stop()}} results in the release of all locks a thread has acquired, potentially exposing the objects protected by those locks when those objects are in an inconsistent state. The thread might catch the {{ThreadDeath}} exception and use a {{finally}} block in an attempt to repair the inconsistent object or objects. However, this requires careful inspection of all synchronized methods and blocks because a {{ThreadDeath}} exception can be thrown at any point during the thread's execution. Furthermore, code must be protected from {{ThreadDeath}} exceptions that might occur while executing {{catch}} or {{finally}} blocks \[[Sun 1999|AA. Bibliography#Sun 99]\]. Consequently, programs must not invoke {{Thread.stop()}}.

Removing the java.lang.RuntimePermission stopThread permission from the security policy file prevents threads from being stopped using the Thread.stop() method. Although this approach guarantees that the program cannot use the Thread.stop() method, it is nevertheless strongly discouraged. Existing trusted, custom-developed code that uses the Thread.stop() method presumably depends on the ability of the system to perform this action. Furthermore, the system might fail to correctly handle the resulting security exception. Additionally, third-party libraries may also depend on use of the Thread.stop() method.

Refer 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 abruptly shut down.

...

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

...

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 security 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 cleanup operations are not carried out as required.

...

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b11768e89492438a-1f3f13b9-4bd84c64-8e468faf-912e79d783cf3434283534d7"><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="66aff032bc195128-30f028a1-47874256-ae90b412-fce9f7d5ed295fd821ee5d45"><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="b2423681980cabc6-85025bc7-47754956-ad2ebe31-2c77118a0dfbcce260fd0c58"><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="7ce06c0cf6f4bcc5-51424a50-4714436b-8853a634-d8e0201e4a34ecf33ef93f91"><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="6e8fcf489ed0bf2b-b682cfbb-417343ed-b63ea273-01e946ad1e9f1d8f0e5e3887"><ac:plain-text-body><![CDATA[

[[JPL 2006

AA. Bibliography#JPL 06]]

14.12.1. , Don't stop and Stop; 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="3a28231639abe382-1af474c0-4b9344c3-83698e07-436631a7e034df7e718d6092"><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="d01b698ed465470a-2f632412-44eb4012-bdc29005-705c6d77a943ea446adf65d3"><ac:plain-text-body><![CDATA[

[[Goetz 2006

AA. Bibliography#Goetz 06]]

Chapter 7: , Cancellation and shutdown Shutdown

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

...