Thursday 12 May 2016

What is thread in java.

       Every executable program is called as thread. and there are two ways to create a thread :
1. By extending Thread class.
2. By implementing Runnable method.

Example of By extending Thread class :

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();
}
}

Example of 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.







No comments:

Post a Comment