Wednesday, January 25, 2012

Thread vs Runnable in Java is always been a confusing decision for beginners in java. Thread in Java seems easy in comparison of Runnable because you just deal with one class java.lang.Thread while in case of using Runnable to implement Thread you need to deal with both Thread and Runnable two classes. though decision of using Runnable or Thread should be taken considering differences between Runnable and Thread and pros and cons of both approaches. This is also a very popular thread interview questions and most of interviewer are really interested to know what is your point of view while choosing Thread vs Runnable or opposite. In this java article we will try to point out some differences between Thread and Runnable in Java which will help you to take an informed decision.

Difference between Thread and Runnable interface in Java

Thread vs Runnable in Java

Here are some of my thoughts on whether I should use Thread or Runnable for implementing task in Java, though you have another choice as "Callable" for implementing thread which we will discuss later.

2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead.

3) Runnable interface represent a Task which can be executed by either plain Thread or Executors or any other means. so logical separation of Task asRunnable than Thread is good design decision.

4) Separating task as Runnable means we can reuse the task and also has liberty to execute it from different means. since you can not restart a Thread once it completes. again Runnable vs Thread for task, Runnable is winner.

5) Java designer recognizes this and that's why Executors accept Runnable as Task and they have worker thread which executes those task.

6) Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable.

These were some of notable difference between Thread and Runnable in Java, if you know any other differences on Thread vs Runnable than please share itvia comments. I personally use Runnable over Thread for this scenario and recommends to use Runnable or Callable interface based on your requirement.

whenever I create Thread, I always face issue whether to extend Thread class or implement Runnable interface. I was not aware of difference in extending Thread class and implementing Runnable interface. After reading this article I know much more about Runnable interface and when should I use Runnable interface or Thread class. Can you also provide an example of implementing Runnable and extending Thread class? some one also told me that don't use either Runnable or Thread instead use Callable, what do you say?

@Gautam and @Anonymous, Thanks for your comment and glad to hear that you like difference between Runnable and Thread and this article helps you to decide when to use Runnable interface and when to extend Thread class while creating Thread in Java.

Its not that difficult to choose between Runnable and Thread than Callable and Runnable for most of Java programmer given Callable is new interface introduced in Java 5. My suggestion is to use Callable when you are doing computation inside Thread as call() method returns FutureTask object which can be used to return result. Avoid Runnable if you are coding in Java 5 even if you don't return any result, just in case you may require in future.

Hi Javin,Generally, I read stuff from blogs and don't bother to comment on any. But your topics are so well explained, I couldn't ignore commenting on how nice your articles are..You did a very good job!

Thanks for nice article @Javin, still I have some doubts regarding overhead we face when extends thread

While extend thread class your class will have all functionalists of thread, same way when you creating thread using runnable interface, you need to pass this object to thread class.So that object again have all functionalists of thread.

@Anonymous, There is big difference, When you extend java.lang.Thread to run your task (code inside run() method) in another thread, you actually create a full Thread class, while implementing Runnable is just a wrapper around your task, so that it can be executed by individual threads or worker threads inside a Thread pool.

Hi Javin...nice article few things that I want to add in this is ..Yes,implements Runnable is the preferred way to do it, as You're not really specialising the thread's behaviour. You're just giving it something to run. That means composition is the philosophically "purer" way to go.

In general, I would recommend using something like Runnable rather than Thread because it allows you to keep your work only loosely coupled with your choice of concurrency. For example, if you use a Runnable and decide later on that this doesn't in fact require it's own Thread, you can just call threadA.run().

Moral of the story : Inherit only if you want to override some behavior.or rather it should be read as "Inherit less, interface more"

Use Runnable interface when you want to access the same resource from the group of threads. Avoid using Thread class here, because multiple objects creation consumes more memory and it becomes a big performance overhead.

Everybody talks about Thread vs Runnable stuff and suggests the coders to use Runnable interface for obvious reasons but I'm really interested about the reason behind allowing the Thread class to be extended. for most of the applications its very unlikely that we change the behavior of thread by extending the Thread class. They could have made the Thread class final just like how they did for String class or at least I want to know the kind of applications that forces the way Thread behaves in java. Could somebody shed light on it? :)

Hello Anonymous, By using Runnable, you can pass same task to multiple thread as different instance of Runnable, this way you can reuse the code, If you were to do the same, you have to create different instances of thread, which would be costly. In earlier case, you can reuse those threads.