Friday, November 10, 2006

Explain different ways of creating a thread?

Threads can be used by either :

  • Extending the Thread class
  • Implementing the Runnable interface.
The runnable interface is preferred, as it does not require your object to inherit a thread because when you need multiple inheritance, only interfaces can help you. In the above example we had to extend the Base class so implementing runnable interface is an obvious choice. Also note how the threads are started in each of the different cases as shown in the code sample.