Ticker

6/recent/ticker-posts

Thread Life Cycle




 Thread Life Cycle

Threads provide multiple paths in a program to execute different tasks simultaneously.To understand how a thread works, it is important to understand the life cycle of a thread.The life cycle of a thread is a series of states that a thread undergoes to perform a task.Following are the various states that the life cycle of a thread undergoes:

* Creating a Thread: It is the state when an instance of a thread is created. At this point, the thread is not alive. There are two ways to create new threads. One way to create a thread is to declare a class to be a subclass of the Thread class. When a new instance of a thread class is created, no system resources are allocated for it. To allocate system resources to a new instance of a thread, the start ( ) method is used. Calling any other method than the start () method, when the thread is in this state (when the thread is newly created and not alive), raises the illegal Thread state Exception at runtime.

* Starting a Thread: It is the start () method that creates the system resources necessary to run a thread, schedules the thread to run, and calls the run Once a call to the run () method is made, the thread starts running

* Non-Runnable state of a thread: A running thread can enter to a non-runnable state. The non-runnable state is the state when a thread stops executing because it is either sleeping or waiting for resources that are held by another thread.

* Blocked: A thread can enter in this state when it has to wait for the I/0 resources that are held by another thread.

* Sleeping: In this state, the thread is still alive but it is not runnable.It might return to runnable state later, if a particular event occurs. In this state, a thread sleeps for a specified period of time. You can use the sleep ( ) method to stop the running state of a thread.

* Waiting for Notification: In this state, a thread waits for notification from another thread.

* Terminating a thread: A thread can be considered dead when its run ( ) method has completed. If a thread comes to this state, then it means that it cannot ever run again.

 A thread does not start executing until an object of the thread is created. Let’s now understand the mechanism involved to create and run a thread.

 Creating and Running a Thread

 When a lava program starts, it has one thread called main thread. You can also start other threads by calling the start ( ) method of the thread object and place the code, you want to execute, in the run() method. A thread in lava can be created by:

*    implementing the runnable (Java.lang.Runnable) interface

  Extending the Thread (Java .Thread) class Let's understand these ways one     by one.

Implementing the Runnable Interface

A thread can be created by declaring a class that implements the Runnable interface.The Runnable interface is available in the Java.This interface has only one method—the run () method— which can be defined in the class implementing the Runnable interface.

  

For more understanding you can also read below mentioned topic from online:

thread life cycle in java

Post a Comment

0 Comments