3

I just read the JDK document of the function Thread.stop. It says,

Stopping a thread causes it to unlock all the monitors that it has locked. If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state.Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result.

I just don't quite understand. What does an inconsistent state mean?

And what can the arbitrary behavior be? Can anyone explain them to me please?

(My question is a bit different from the one you thought duplicate. Mine emphasizes on the possible damage, while the former one emphasizes on what method can be used to take the place of Thread.stop.)

roll1987
  • 183
  • 1
  • 7

1 Answers1

7

Consider any type of complex data structure, say a doubly-linked list. A thread locks the structure, looks at it and changes it, and then releases the lock. So consider:

  1. Lock the list.

  2. Adjust the forward pointer.

  3. Adjust the corresponding reverse pointer.

  4. Release the lock.

Imagine if a thread has completed steps 1 and 2 and then you stop it. The doubly-linked list is not valid because a modification to it is half finished. If another thread were to acquire this lock and attempt to walk the list, catastrophe would result.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278