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

Explain how hashmap works?

600


What is private static in java?

585


What are computer functions?

495


How do you include a string in java?

532


how to open and edit XML file in Weblogic???

1563






Can set contain duplicates?

563


How many bits are in a sentence?

601


How to disable caching on back button of the browser?

537


Why is a string immutable?

558


Should you use singleton pattern?

536


For ease of programming you can consider the maze as a 2D array with colors represented by below integer and characters (in capital letters). • B - Black • W -White • G- Green • R- Red R B W B W W W W W W B W B B W W W W W W W B W B W W W B W W W W B B W W W B W W W B W W B B B B W B W B W W B W W W B W W W B B B W W B W W W B W W B W B W W W B W B W W W W B B W W W W B W W W W W G Shortest Route Problem: • Solution that finds the shortest Route between Red and Green  White will have 1 Weight.  Red and Green carry no weights.  Shortest path is the path with less weight when you add up the weights in the path.

1593


What is static block?

602


Where is the find and replace?

546


What is bitwise complement?

515


What is time complexity java?

549