What are two ways of creating threads in Java and why so?
Threads can be created in the following ways :
-Instantiating a class extending java.lang.Thread class and calling start() method
-Creating a java.lang.Thread and passing a reference of a class implementing Runnable interface.Then calling start() method on this object.
-Instantiating a class extending java.lang.Thread class and calling start() method
-Creating a java.lang.Thread and passing a reference of a class implementing Runnable interface.Then calling start() method on this object.
As mentioned,Java supports these two mechanisms for Thread creations but second option is preferred as in first case there is a possibility of single inheritance making a thread object less flexible in its behaviour while in second case a thread object can be clubbed with multiple features.