Versions Compared

Key

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

...

Because the class Vector 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 reflects returns the true correct number of elements in the vector even when an element is added or removed. This is 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 06|AA. Java References#API 06]\]. If the thread is in the process of adding a new integer to the vector when it is stopped, the vector may become accessible while it is in an inconsistent state. For example, {{Vector.size()}} may return two whileeven though the vector contains three elements (as the element count is incremented after adding the element).

Compliant Solution (volatile flag)

This compliant solution stops the thread by using uses a volatile flag to stop the thread. An accessor method shutdown() is used to set the flag to true. The thread's run() method polls the done flag, and terminates when it becomes true.

...

Remove the default permission java.lang.RuntimePermission stopThread from the security policy file to deny the Thread.stop() invoking code, the required privileges.

Risk Assessment

Trying to force thread shutdown Forcing a thread to stop can result in inconsistent object state and corrupt the object. Critical resources may also leak if cleanup clean-up operations are not carried out as required.

...