Wednesday, January 25, 2012

Difference between Thread vs Runnable interface in Java

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

Difference between Thread vs Runnable in JavaHere 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.

1) Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java.

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 as Runnable 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 it via comments. I personally use Runnable over Thread for this scenario and recommends to use Runnable or Callable interface based on your requirement.

Some more Java Tutorials you may like



19 comments :

Anonymous said...

Why wait(),notify() and notifyAll() are declared in Object class but not in Thread ?

Javin @ convert int to string java said...

Hi Anonymous, I have discussed this question here
Why Wait, notify and notifyAll are defined in Object class .you may find useful.

Rahul Bhardwaj said...

Minor Correction in statement "while in case of using Runnable to implement Thread you need to deal with both Thread and Runnable two classes"

Runnable is an Interface not class.

Javin @ race condition in java said...

@Rahul, Thanks for your comment. Indeed Runnable is an interface and not a class.

Gautam said...

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?

Javin @ List vs Set Java said...

@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.

Keith Yong said...

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.

פאדי חוסיין said...

good article thnx

Anonymous said...

I think most important difference between Thread and Runnable in Java is that Thread is a Class and Runnable is a interface. isn't it ?

Anonymous said...

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!

Anonymous said...

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.

Could you please help me on this.

Javin @ Connection refused exception in Java said...

@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.

In short Runnable is cheap.

SARAL SAXENA said...

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"

Anonymous said...

Javin. A doubt related to interface. Why a data member in an interface implicitly final?
-HareshKannan

Pruthvi Raj said...

another difference of Thread V/S Runnable is

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.

manju said...

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? :)

Anonymous said...

when should we use Runnable interface instead of Thread. Give logic apart from multiple inheritence in java

Anonymous said...

I didn't understand the 4th point in difference..by implementing Runnable interface how u can can reuse the task...how Runnable is winner can u plz elaborate

Saumya said...

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.

Post a Comment