What is Thread life cycle in Java Programming?
Thread life cycle consists five states:
New, Ready, Running, Inactive and Finished
When you create a Thread, it is in the new state; the only method you can use with a new Thread is the method to start it. When you call the Thread’s start() method, the Thread enters the ready state. A ready Thread is runnable, which means that it can run. However, a runnable Thread might not be in the running state because the CPU might be busy elsewhere. Just as your runnable automobile cannot pass through an intersection until the traffic officer waves you on, a runnable Thread cannot actually run until the CPU allocates some time to it.
Methods of the Thread class:
start() – Starts the Thread, causing the run() method to execute
stop() – Stops the Thread
supended() – Suspends the Thread until you use the resume() method
resume() – Resumes the Thread you suspended
isAlive() – Returns true or false to indicate whether the Thread is currently running
setPriorit() – Lets you set a priority from 1 to 10 for the Thread by passing in an integer.
When a Thread begins to execute, it is in the running state. A Thread runs until it becomes inactive or finishes. A
Thread enters the inactive state when you call the Thread’s sleep() or suspended() method, or it might become
inactive if it must wait for another Thread to finish and for the CPU to have available time. When a Thread
complete the execution of its run() method, it is in the finished or dead state. A Thread can also enter the finished state before its run() method completes if you call the Thread’s stop() method. You can use the isAlive() method to determine whether a Thread is currently alive, which means that it has started but has not stopped. The isAlive() method returns false if a Thread is new or finished; otherwise it returns true.