HTML Java

Java Thread


Creating a thread by association

You can create thread using by extends or by association or we can say create thread using inheritance and association as well. Which one is better.

Better one is by using association because when we create thread using extends then all methods of Thread comes in our class and which we don’t want that also will come in class so this is complexity. And you can’t extends another class when you extends Thread class. because at a time a class can extends only 1 class .so our class Become thread specific so to solve this problem Runnable interface came into the picture.

Example:

class Runable implements Runnable {
public void run(){
System.out.println("A thread");
}
public static void main(String args[]){
Runable a = new Runable();
Thread t =new Thread(a);
t.start();
}
}

Output

A Thread