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

The following program reads data (details of students) from a file named students.txt and converts it into e-mail addresses. The results are written to a file named studentemail.txt. students.txt consists of a number of lines, each containing the data of a student in colon delimited format: Last Name:First Name:Student Number Each input record is converted to an e-mail address and written to studentemail.txt in the following format: the first character of the last name + the first character of the first name + the last four digits of the student number + “@myunisa.ac.za” import java.io.*; public class EmailConverter { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new FileReader ("students.txt")); PrintWriter output = new PrintWriter(new FileWriter ("studentemail.txt")); String line = input.readLine(); while (line != null) { // Extract the information for each student String[] items = line.split(":"); // Generate the email address String email = "" + items[0].charAt(0) + items[1].charAt(0) + items[2].substring(4,8) + "@myunisa.ac.za"; email = email.toLowerCase(); // Output output.println(email); line = input.readLine(); } input.close(); output.close(); } } Rewrite the class so that it handles possible errors that may occur. In particular, it should do the following: • It should catch at least three appropriate exceptions that might occur, and display suitable messages. • At this stage, the program will not run correctly if there is an empty line in the input file. Change the program so that if an empty line is encountered, an exception is thrown and the empty line is ignored. This exception should be handled with the display of a suitable error message. • Before the e-mail address is added to the output file, check if the student number has 8 digits. If not, throw an InvalidFormatException (which the program should not handle itself)

0 Answers  


What is Vector?Can you elaborate how Vector is Thread safe?

3 Answers   HCL,


Explain numeric promotion?

0 Answers  


How can we create a object of a class without using new operator.

1 Answers   Infosys, Nokia,


Can a class be subclass of itself?

4 Answers  






Explain aggregation in java?

0 Answers  


Where the CardLayout is used?

1 Answers  


any other way to print the text without using System.out.println() in java?

5 Answers   IBM, Infosys,


What is an immutable object? How do you create one in java?

0 Answers  


What is an example of a keyword?

0 Answers  


why java does not contain pointers?

13 Answers   Infosys, TCS,


Explain 5 features introduced in jdk 1.7?

0 Answers  


Categories