It represents the execution status of a thread.
The right way to control thread is by using the methods: start(), wait(), notify(), notifyAll(), join().
The wrong way is by using the methods: stop(), suspend(), resume().
The latter have been deprecated.
stop() is unsafe because by stopping a thread all the locks are released and some object data may be in inconsistent state.
Use a flag instead to indicate that the thread should terminate.
suspend() and resume() can cause deadlocks: if the thread suspended has a lock and the thread supposed to call resume() need that lock before calling it, there is a deadlock.
Use wait() and notify() instead and use a flag to indicate the state suspended or active. Call wait() when suspended and an other thread will call notify() to resume.
Use the Thread.State enumeration to deal with all the possible states.
Here's a diagram of the possible states:
start() scheduler wait acquiring lock ---> ---> ---> ----- ------- --------- --------- | NEW | | READY | | RUNNING | | BLOCKED | ----- ------- --------- --------- <--- | | ---> scheduler | | lock acquired | | | | join() or wait() | | ----> --------- | | | WAITING | | | <---- --------- | | notify() or notifyAll() | | | | sleep(), timed join() or timed wait() | | ----> --------------- | | | TIMED_WAITING | | | <---- --------------- | | timeout | | | | End of thread ---------> ------------ | TERMINATED | ------------
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.