What is the life cycle of Thread ?

Answer Posted / ar.g

The Life Cycle of a Thread
Now that we've seen how to give a thread something to do, we
can review some details that were glossed over in the
previous section. In particular, we look at the life cycle
of a thread: how to create and start a thread, some of the
special things it can do while it's running, and how to stop
it.
The following diagram shows the states that a Java thread
can be in during its life. It also illustrates which method
calls cause a transition to another state. This figure is
not a complete finite state diagram, but rather an overview
of the more interesting and common facets of a thread's life.

A thread goes through various stages in its life cycle. For
example, a thread is born, started, runs, and then dies.
Above mentioned stages are explained here:
New: Creation of a Thread
A new thread begins its life cycle in the new state. It
remains in this state until the program starts the thread.
It is also referred to as a born thread.
Thread t;
t = new Thread(this, "Demo Thread"); //thread consturctor
t.start(); // Start the thread

After the bold statement has been executed, t is in the
New Thread state. When a thread is a New Thread, it is
merely an empty Thread object; no system resources have been
allocated for it yet. When a thread is in this state, you
can only start the thread. Calling any method besides start
when a thread is in this state makes no sense and causes an
IllegalThreadStateException).
Runnable: Thread Started ….Running state
After a newly born thread is started, the thread becomes
runnable. A thread in this state is considered to be
executing its task.
Thread t;
t = new Thread(this, "Demo Thread"); //thread consturctor
t.start(); // Start the thread
The start method creates the system resources necessary to
run the thread, schedules the thread to run, and calls the
thread's run method.
After the start method has returned, the thread is
"running". Yet, it's somewhat more complex than that. As the
previous figure shows, a thread that has been started is
actually in the Runnable state. Many computers have a single
processor, thus making it impossible to run all "running"
threads at the same time. The Java runtime system must
implement a scheduling scheme that shares the processor
between all "running" threads.
So at any given time, a "running" thread actually may be
waiting for its turn in the CPU.
Not Runnable state
A thread becomes Not Runnable when it is in the following
states:
Waiting: Sometimes a thread transitions to the waiting
state while the thread waits for another thread to perform a
task. A thread transitions back to the runnable state only
when another thread signals the waiting thread to continue
executing.
Timed waiting: A runnable thread can enter the timed
waiting state for a specified interval of time. A thread in
this state transitions back to the runnable state when that
time interval expires or when the event it is waiting for
occurs.
o Its sleep method is invoked.
 Thread.sleep(1000);
 The specified number of milliseconds must elapse before
the thread becomes Runnable again
o The thread calls the wait method to wait for a specific
condition to be satisifed.
o If a thread is blocked on I/O, then the I/O must complete
Terminated: Stopping a Thread
A runnable thread enters the terminated state when it
completes its task or otherwise terminates.
A program doesn't stop a thread like it stops an applet (by
calling a method). Rather, a thread arranges for its own
death by having a run method that terminates naturally. For
example, the for loop in this run method is a finite loop--
it will iterate 1 times and then exit:
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
// Let the thread sleep for a while.
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
A thread with this run method dies naturally when the loop
completes and the run method exits.

So, the lifecycle of a thread extends from it creation,
running, stopping and finally its death.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why constructor has no return type?

638


When to use runnable interface vs thread class in java?

525


Can inner class have constructor?

550


What is the protected method modifier?

563


Why wait and notify methods are declared in object class?

599






What are the two types of java programming?

539


Is hashset ordered?

551


What is the purpose of garbage collection in java, and when is it used?

564


Similarity and difference between static block and static method ?

536


Can the garbage collection be forced by any means?

534


What is meant by bytecode?

577


What are the access modifiers in java?

588


What is the difference between an array and an array list?

503


Is arraylist a class in java?

536


Write a program to solve producer consumer problem in java?

571