What is the life cycle of Thread ?

Answers were Sorted based on User's Feedback



What is the life cycle of Thread ?..

Answer / 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

What is the life cycle of Thread ?..

Answer / venkat

Born state.
Running state.
Idle state.
Dead state.

Is This Answer Correct ?    6 Yes 14 No

Post New Answer

More Core Java Interview Questions

What are the legal operands of the instanceof operator?

0 Answers  


Explain about oops concepts.

0 Answers   Aditi Placement Service,


What is complexity in java?

0 Answers  


What is the difference between a switch statement and an if statement?

0 Answers  


How would you convert bytes to string?

0 Answers  






What are predefined functions?

0 Answers  


What does singleton class mean?

0 Answers  


What exactly is a .class file?

0 Answers  


EDS (Electronic Data Systems India Pvt Ltd) at Chennai on 16-12-2006.

1 Answers   EDS,


Explain what are final variable in java?

0 Answers  


What is an interoperable application in java ?

0 Answers   HCL,


what is synchronization and why is it important? : Java thread

0 Answers  


Categories