Saturday, 25 December 2021

Thread life cycle in Java

A thread life cycle denotes the time between the creation of the thread and die of the thread.

1. Newborn state

When a thread is created it is in the new born state i.e, when it has been created and is not at running. In other words, a start() has not been invoked on this thread. In this state, system resources are not at allocated to the thread. where a thread is in a new born state calling any method other than start() causes an IllegalThreadStateException 

2. Runnable State

A thread is in the runnable state is ready for execution but is not being executed currently, once a thread is in the runnable state, it gets all the resources and move on to the running state when CPU calls.

All runnable threads are in Queue waiting for the CPU call.


3.Running Sate

After the runnable state, if thread gets CPU access, it moves into the running state. The thread will be in the running state unless once of the following occurs 

  • It dies
  • It gets blocked to the input/output for some reasons
  • It calls sleep(), wait(), and yiled().
  • It is preempted a thread of high priority 
  • It;s quanta, expires

4. Dead state

A thread goes into the dead state in two ways

1. If its run method is over

2. A stop() is invoked 

The run() exists when it finished execution normally or throws a uncaught exception. The stop() kills the thread. A thread in the dead state can't be executed further.

5. Blocked state

A thread can enter the blocked state when one of the following five conditions occur 

  • When sleep() is called
  • When suspend() is called
  • When wait() is called
  • The thread calls an operations
  • The thread is waiting for monitor.

A thread must have out of the blocked state into the runnable state using the opposite of what ever phenomenon put into the blocked state.

  • If a thread has been put to sleep() the specified sleep time period must expired 
  • If a thread is called wait() them some other thread using the resources for which first thread is waiting must call notify() or notifyAll()
  • If a thread is waiting for the completion of an input or output operation, Then the operations must finish.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top