Thursday, 12 May 2016

What is thread in java.

Thread:

1) Every Executable programme is called as thread.
2) we can achieve thread by two way.



   A)    By extending Thread class

      eg.
class ThreadDemo extends Thread{
public void run(){
System.out.println("Thread class extended");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}


  B)    By implementing Runnable method.


       class ThreadDemo implements Runnable{

public void run(){
System.out.println("Runnable class Implement");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}

Which is best thread.

If we extends Thread class then we can not extend another class. And if we implement Runnable interface and in future if we want to extend any class then we can extend if we implement Runnable interface.