Java Essentials - Life cycle of a thread | Java
20K views
Oct 24, 2024
Java Essentials - Life cycle of a thread
View Video Transcript
0:00
In this video we are discussing a very important concept that is the life cycle of a thread
0:07
We shall explain this concept with one diagram. So here is a diagram we are getting
0:13
Here we are finding that we are having certain states that there is a new, runable, waiting
0:18
timed waiting, terminated and blocked. This terminated state can also be called as a dead state
0:26
So whenever a new thread will be originated, then it will be. be found in the new state also known as the born state
0:34
Now when the thread will start its execution, then it will be found in this runable state
0:41
And in this runable state, the thread is supposed to perform its task
0:45
There is an intended task to be performed which will be executed when the thread will
0:50
be found in the runable state. When the trade will be found in the runable state, the thread may get suspended due to the
0:57
presence of some higher priority threat in the system so it will be found in the
1:01
waiting state through this weight and when that respective constraint will be
1:06
waived then through notify or notify all that state transition will take place for the thread from moving from this waiting state to this runable state Sometimes this waiting may be for a certain duration only
1:22
In that case, the thread will be found in the timed waiting and then there is a wait or slip
1:28
So using this, the thread will have the timed waiting. That means the thread will not be waiting at this particular state for indefinite period of time
1:37
but it will be waiting here for certain duration only. So when the interval expires through notify or notify all, this timed waiting state
1:47
to this runable state, the state transition will take place. And when this runable state, when the thread will be found and after its completion
1:57
of its task, then the task completes will take place and it will be found in the terminated
2:03
state or the dead state and the thread will be, will be able to be. expiring here now due to some I.O request a thread might be found in the block state or
2:13
enter synchronized statement the thread might be found in the block state and when this this acquired
2:20
lock interrupt or when the IO completion will be obtained then the thread will be found in the runable state back again so let us go for this respective states into more declaration and with a more definition in the next slide
2:37
What is the life cycle of a thread? A thread goes through various stages in its life cycle
2:45
The different states are new, runable, running, running, waiting and terminated. At first way, discussing
2:54
this new. A new thread begins its lifecycle in the new state and it remains in this state
3:02
until the program starts the thread and it is also referred to as the bond thread
3:08
Next one is the runable. So after a newly born trade is started, the thread becomes runnable
3:15
and the thread in this state is considered to be executing the respective task what was
3:22
supposed to get executed by this thread. So in the runnable state, actually, the thread is
3:27
executing its own task. Next one, we are having this waiting. Sometimes a threat transitions
3:35
to the waiting state while the thread waits for another thread to perform a certain task So some higher priority thread may come into the system So this particular thread may be found having a transition from the runable state to the waiting state
3:52
A threat conditions back to the runable state only when another threat signals the waiting
3:57
thread to continue its execution. Next one is the timed waiting. A runable thread can enter the time waiting state for a specified interval of time
4:10
A thread in this state transitions back to the runnable state when the time interval expires
4:16
or when the event it is waiting for occurs. So whenever the time will expire or the event for which it is waiting, when it will occur
4:25
then it will go back to the runable state back again from the timed waiting state
4:30
Next one is the terminated, also known as the dead state. A runable thread enters the terminated state when it completes its task or other
4:39
otherwise terminates. So that is a terminated state or dead state. So we have explained all this concept using the diagram also
4:50
Now we are discussing priorities of a thread. Every Java thread has a priority that helps that operating system determine the order in which
#Computer Education
#Java (Programming Language)
#Programming