|
|
Threads - Thread Execution
Alive or Dead?
- the Thread class includes an isAlive() method which returns true if a thread has been started and not stopped
- a thread stops when its run() method finishes executing
- the isAlive() method returns false if the thread is new or dead
- there is no way to detect if a thread is not alive because it was never started or because it is dead
- there is also no way to detect if a live thread is Runnable or Not Runnable
- neither can a thread identify which thread started it
Why a thread might not be executing (BB pg 270)
- the thread does not have the highest priority and can't get CPU time
Example: LowPriority
- the thread has been put to sleep via sleep() method
Example: Sleeping
- there is more than one thread with the same priority and the JVM is switching between the threads
Example: SamePriority
- the thread's wait() method has been invoked
Example: Waiting
- the thread's yield() method has been invoked
Example: Yielding
- the suspend() method has been invoked (this is a deprecated method and should no longer be used)
|