Monday, April 6, 2015

Creating a thread in Java; Two ways; Why

To define a class that is a derived from the Thread class which is built into Java. An object of this derived class will be a thread. Thread class has a method called run, which you must override in order to have your thread do whatever you want it to do.

Java does not support multiple inheritance, which means that we can not derive from multiple classes at once. So, there may be a situation in which we want to create a thread, but we also want to derive from a class that is not the Thread class. For this reason, Java provides an alternative way of creating a thread – so that you don’t have to create a class that derives from the Thread class. Java provides an interface called Runnable that our class can implement in order to become a thread. The Runnable interface has only one method heading that we must implement .

No comments: