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 accomplishedcomplete, the request has been canceled, or the program or JVM must shut down expeditiously.
...
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 couldmight 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 couldmight resultoccur whenwhile executing {{catch}} or {{finally}} blocks \[[Sun 1999|AA. Bibliography#Sun 99]\]. Consequently, programs must not invoke the {{Thread.stop()}} method. |
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 abruptly.
Noncompliant Code Example (Deprecated Thread.stop()
)
...
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. For example, Thisthis could result in {{Vector.size()}} returning an incorrect element count, for example, because the element count is incremented after adding the element. |
...
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.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d092260a274433c4-07abce2c-43bb4284-b52bbf1b-0225f1ffaaf2232569d590c9"><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="7e7fcb8d69139213-0d62314b-43ed44c1-a9639483-5159dd2d040fdbba0b9e1196"><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="5b0be316553f0178-8c6e6c04-478f413c-920b964b-d1dd6978dfa19abe629c878d"><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="2b298cf18a815719-6c803677-4597490d-8d31a183-17e626cb3f81f5767f2de157"><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="43b0d14a331cc9cd-97a43deb-477646ac-915e86bf-d5da7f1939c24f18e73ad07e"><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="4bbf18fe71004ab1-2cfda6e5-4a414b77-ad478f3d-bb58ceddf594abdc37e441d4"><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="3f84bf05de0f653b-e458fff9-4821439f-ad129cee-732632e62cf2be3f75ff3732"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Chapter 7: Cancellation and shutdown | ]]></ac:plain-text-body></ac:structured-macro> |
...