An Item's Life Cycle (Thread States)
A thread can be in any of the states mentioned above at any given time in Java. They are as follows:
- I'm blocked or waiting
- Temporary Waiting
Justification of various Threads States
Threads are always in a new state when they are first formed. The code has not yet been executed and, as a result, has not started for a thread in the new state.
When a thread calls the start() method, it transitions from the new state to the active state. Running and runnable are two of the two states that make up the active state.
- Runnable: After a thread has reached the runnable state, it is prepared to begin execution. The thread is currently in a runnable condition.The software allows a set amount of time for each unique thread when using multithreading. Every thread executes for a brief period, after which it voluntarily transfers control of the CPU to another thread so that the latter line may complete its portion of the time allotted for execution. Whenever such an event occurs, all threads that are ready to run and awaiting their chance to do so are in the runnable state. The threads are stored in a queue while they are in the runnable state.
- Running: The thread transitions from the runnable state to the running state when it receives the CPU. The most frequent transformation of a thread's state is from runnable to running.Blocked or Waiting: Whenever a thread is inactive for an extended period (but not permanently), it is either blocked or waiting.
As an illustration, a thread (let's call it A) might desire to print some data to the printer. But concurrently, the other line (let's call it B) prints some data on the printer. Since thread B must use the printer first, thread A must wait. As a result, thread A is blocked. Because it cannot execute anything, a thread in the blocked state never uses a Central Processing Unit cycle (CPU). Therefore, we may argue that thread A is still inactive.Until thread A, which is in the waiting or blocked state, is reactivated by the thread scheduler.
The main thread is considered waiting when it calls the join() method. After that, the main thread waits for the subordinate threads to finish their jobs. A notification is issued to the main line when the child threads finish their work, which switches the thread from waiting to the active state.
The task of the thread scheduler is to choose which threads to accept and which to refuse when there are many threads in the waiting or blocked state. The selected thread is then given a chance to run.
- Timed Waiting: Occasionally, awaiting leadsto hunger. For instance, a thread (whose name is A) has entered a code's vital area and is unable to leave it. In this case, another thread (whose name is B) is forced to wait forever, which causes it to starve. A timed waiting state is provided to thread B in order to prevent this occurrence. Therefore, thread is only in the waiting state for a specific time, not permanently. When we use the sleep() method on a particular thread, we are actually engaging in timed waiting. The thread enters the timed wait state using the sleep() function. The thread awakens when the allotted time has passed and resumes execution where it left off.
- Terminated: A thread enters the terminating state for a reason stated below:
- A thread exists or generally ends once it has completed its task.
- A segmentation issue or unintended exception are examples of uncommon events that can cause an abnormal termination.
- A thread that has been ended means it is no longer in the system. In other words, the thread is inactive and cannot be revived (made active again after being killed).