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.
public class ThreadExample
public static void main (String args)
int n= Integer.parseInt(args);
System.out.println("Number is : " +n);
while(nexthundred > n)
sum = sum+(nexthundred--);
System.out.println("Sum of next hundred numbers is " + sum);
So at machine level following instructions are executing :
1. Convert args to integer
2. Store the integer to the n memory location
3. Print the number
4. Stote 0 in the location known as sum
5. Stote n+100 in the location known as nexthundred
6. Now Test if nexthundred is greater than n
7. Again print the sum as final result
The above steps happened at the machine level.
So we learn that The execution path of the above steps is called as Thread .
* Each computer program has atleast one thread that is the thread which starts the application
and we normally call this thread as main thread
If the program is break into different parts , then it is easier to write code for the algorithm as separate tasks or threads .Please note that , using thread in the program does not make the program to execute faster . But it sorts of create an illusion .
LifeCycle of a Thread
Creating a Thread
A thread can be created by two ways :
By extending the thread class
By implementing the runnable interface
Thread are created with three pieces of information :
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 .
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 .
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.