Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

What is Thread ?

Thread is a sequence of code executed independently with other threads of


control with in a single executed program .

for example : Here we are going to calculate the sum of next hundred
numbers from the given input number by the user

LifeCycle of a Thread
Creating a Thread
A thread can be created by two ways :
By extending the thread class
or
By implementing the runnable interface
Thread are created with three pieces of information :
These are
1. Thread Name : It will assign some name to the thread
2. Runnable target : It is the list of code that needs to be executed by the
thread . This information is generally in the run() of the thread .
3. Stack size : Every thread has a stack where it stores its local variables or
temporary variables . Everything related to the size of stack is platform
dependent.Use of stack size in portable programs is highly discouraged

Starting a Thread
We can start the thread by calling the start() method on Thread object .
Although a thread is there once it is created but it is not executing or running .
So it is in waiting state .
Being in waiting state , other threads can interact with this thread ,and
attributes of the waiting thread can be set along with its priority , its daemon
status etc. can also be set by the other threads .
So in short
Even though the thread is waiting , its state can be changed by other threads .
Terminating a Thread

Once the thread finish executing run() method , no matter how much time it
takes to complete or number of calls to other methods, it terminates the thread
. Means the thread dead . Once dead , it can not be restarted.
Pausing,Suspending and Resuming Thread
A thread can be paused by using sleep(time) method . Can be suspended by
using wait method .
Now the question arises what is the difference between wait and sleep
method

Sleep() method will make sure that the thread sleeps atleast for the amount of
time we passed in the sleep method , but there is no such guarantee for the
wait method .
Also , sleep method affects only the thread that executes it , its not possible
to tell another thread to go to sleep .
Thread Cleanup

Although the thread completed its run()method and is terminated . Still ,


Thread object contains some interesting information .As long as some other
active object holds a reference to the terminated
thread object, other threads can execute methods on the terminated thread
and retrieve that information.
If the thread object representing the terminated thread goes out of scope, the
thread object is garbage collected .
Please mention in comments in case you have any doubts regarding threads
or multithreading.

class RunnableDemo implements Runnable {


private Thread t;
private String threadName;

RunnableDemo( String name){


threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}

public void start ()


{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}

public class TestThread {


public static void main(String args[]) {

RunnableDemo R1 = new RunnableDemo( "Thread-1");


R1.start();

RunnableDemo R2 = new RunnableDemo( "Thread-2");


R2.start();
}

You might also like