Total Pageviews

Friday, October 19, 2012

Java Threads via Extending Thread class, implementing Runnable interface and Callable interface


If anybody asks - What are the two methods of using Threads in Java? The answer is on our tip of tongue- TWO, but further if it is asked what is the difference between threads implemented via extending Thread class and implementing runnable interface? we feel dificulty in explaining. Here some of my thoughts are presented to draw a line among them.
  1.  As we all know Java doesn't support multiple inheritance. If you want to extend the Thread class then it will make your class unable to extend other classes as java is having single inheritance feature whereas If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems.
  2. In OOP, we extend a class for adding some new behaviour. If we are not making any modification on Thread than its a good OOP design to use Runnable interface.
  3. When we extend a thread class, all the Properties and Behaviours of Thread class and inherited by the subclass, its kind of overhead if we are not going to use it and the same can be achieved on ease with Runnable.
  4. Runnable interface represent Task and the same gives flexibility to get them executed by either Executors or Thread or any other means. So it draws a line between Task as Runnable than Thread as good design decision. However Executors accept Runnable as Task and they have worker thread which executes those tasks, but that's a totally different point. 
  5. While we are differentiating task as Runnable exhibits it can be reused and we are also provided with liberty to execute it from different means. As we know, a thread cannot be restarted once it's complete so again Runnable seems more promising. 


A Short Note on Callable and Runnable:
Both the interfaces are implemented by those classes who want to execute a thread of execution, but few notable differences are as follows:
  1. A Thread implemented via Callable interface may return a value, whereas its not possible if we are using a Runnable interface.
  2. A Thread implemented via Callable interface may throw checked exceptions, whereas its not possible if we are using a Runnable interface.


What I feel, the developers of Java felt a need of enhancing the capabilities of the Runnable interface, without changing the usage of the Runnable interface and that is why probably they've developed Callable interface.

I hope this will clear some of the doubt clouds about threads.

5 comments:

  1. If There is already Concept of 'Implementing Runnable' then why to use 'extending Thread'.
    (why Extending Thread over Implementing Runnable)

    ReplyDelete
    Replies
    1. Thread is a class, and when we say .start() we create a thread of execution which is attached to an Instance of Thread Class. The run() method of Runnable is called making it execute the task on to the thread of execution, and the start() method returns quickly. Runnable is the task that is assigned to the newly created thread of execution. So without the Thread class, you canNot run your Runnable.

      Delete
  2. one significant difference between implementing Runnable and extending Thread is that
    by extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same object instance.

    ReplyDelete
    Replies
    1. Good point Vivek... Thanks for sharing...

      Delete