Wednesday, 9 December 2015

Anatomy Of Multithreading In Java

What is MultiThreading 

Multithreading is a way by which one single program can perform two or more task simultaneously 
Multi means more than one 
Thread means smallest unit of processing Or Seperate path of execution 
So Multithreading means one or more than one thread is/are executing simultaneously.....

How can we create a thread in java


There is two ways of creating a thread in java..

1- Using Thread classs

2 -Using runnable interface

So we will do each one by one
_________________________________________________________________________________

1-Using Thread class

Using This method ,we have to extends the thread class.....And we know when we extending any class ,our class has become of parent type class..
So Lets create a Thread..

--------------------------------------------------------------------------------------------------------------------------
public class City extends Thread{

public void run(){
System.out.println("my current city is delhi");
                             }
public static void main(String arg[]){

City c=new City();
c.start();
}
}

--------------------------------------------------------------------------------------------------------------------------
So Lets Discuss Each line
In 1st line we are making a class(City) and extends Thread class.So our class also become Thread
In 2nd line we are writing a method called run and write something in body of this method.
run() method is Present in runnable interface and Thread class has implemented that run method So
Here we are overriding that run method

In main Method ,we are creating an object of City class and calling the run() method...............
Lets See What is happening behind the scene....
When we create object of City class ,The Default constractor of City class is called and Inside the of constractor JVM  placed the super  keyword and call the paarent class's(Thread) constractor
Thus Our City class object Became Thread Type Object...

strat() is method of thread class.Itstarts the Thread means it put thread in Runnable State
_________________________________________________________________________________

_________________________________________________________________________________

2-Using runnable interface

Using runnable interface,we can create a thread....
For this we have to implement the runnable interface..runnable interface has only one method called run().
Lets start.....
-------------------------------------------------------------------------------------------------------------------------
public class City implements runnable{

public void run(){

System.out.println("Delhi is  The capital of india");
                           }
public static void main(String arg[]){
Thread t=new Thread();
City ct=new City(t);
ct.start();

}

--------------------------------------------------------------------------------------------------------------------------
Here we are creating a object of Threat class and pass it as parameter in our City class Because Thread class has a contractor which takes parameter of Thread Type




Lifecycle of a thread

Lifecycle of a thread have five states 

New

It is very first state when object of  class is created

Runnable

It is the second state When we invoked the start() method

Non-Runnable(Blocked)

It is the third state.When we call methods like sleep(),wait(),suspend() on running thread

Running

When Thread class executing its run method

Terminated

when run() method exits means execution of run method got over

No comments:

Post a Comment