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.

1 Comments:

At 7:48 PM, Blogger Saifuddin.Merchant said...

There is also a logical differentiation between extending the thread class and implementing runnable.

You extend a class only in case there is a IS-A relation. The only reason to extend the tread class is then to create your implementation of a Thread class. Mostly you should not be doing that!!

Sam - http://java-twisters.blogspot.com/ -- Java quiz with a twist

 

Post a Comment

<< Home