...
Because the Vector
class is thread-safe, operations performed by multiple threads on its shared instance are expected to leave it in a consistent state. For instance, the Vector.size()
method always returns the correct number of elements in the vector, even in the face of after concurrent changes to the vector, because the vector instance uses its own intrinsic lock to prevent other threads from accessing it while its state is temporarily inconsistent.
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, this could result in {{Vector.size()}} returning an incorrect element count 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.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="600d30b56e61570b-5490434d-41a64225-bd318a52-b8721e32a7c7db2441547eca"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="191715f873b0832b-b9eab8d0-44e248e2-9b8ba95e-742b7165232197b12c14c0d0"><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="4d4bbb017cb77c22-1fedcd03-45144776-84378323-135c3e9d7ae9aefe3a6c1081"><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="568430bf475f7610-8ecdff5e-44824cad-a62cb386-75f785fdb1698f2ba5b31f2a"><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="9ebd734e0df1376b-036cd0f9-43294dd4-8c8188a5-77c08235b75267ea42200840"><ac:plain-text-body><![CDATA[ | [[JPL 2006 | AA. Bibliography#JPL 06]] | 14.12.1, Don't 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="7cd3b55cb8e9d8be-dc9fc964-4c3e40f9-9eed9476-08bf3059089fa23fb349e1c4"><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="f18178c710aafecc-9bdab042-4e4c4415-b1ec9a19-b898967fed4f415232015630"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Chapter 7, Cancellation and Shutdown | ]]></ac:plain-text-body></ac:structured-macro> |
...