Thread vs Runnable in Java is always been a confusing decision not just for beginners in Java but also for developers who have done a couple of projects in Java. Thread in Java seems easy in comparison to 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 the decision of using Runnable or Thread should be taken considering differences between Runnable and Thread and the pros and cons of both approaches.
This is also a very popular thread interview questions and most of the interviewer is really interested to know what is your point of view while choosing Thread vs Runnable or the opposite.
In this java article, we will try to point out some differences between Thread and Runnable in Java which will help you to make an informed decision.
Btw, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at these Java Multithreading courses.
This is also a very popular thread interview questions and most of the interviewer is really interested to know what is your point of view while choosing Thread vs Runnable or the opposite.
In this java article, we will try to point out some differences between Thread and Runnable in Java which will help you to make an informed decision.
Btw, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at these Java Multithreading courses.
This list contains advanced courses to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance.
Difference between Thread and Runnable interface in Java
Here are some of my thoughts on whether I should use Thread or Runnable for implementing tasks in Java, though you have another choice as "Callable" for implementing thread which we will discuss later.
1. Java doesn't support multiple inheritances, which means you can only extend one class in Java so once you extended the 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 modifications on Thread then use the Runnable interface instead.
3. The Runnable interface represents a Task that can be executed by either plain Thread or Executors or any other means. so logical separation of Task as Runnable than Thread is a good design decision.
4. Separating task as Runnable means we can reuse the task and also has the liberty to execute it from different means. since you can not restart a Thread once it completes. again Runnable vs Thread for a task, Runnable is the 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 an additional overhead just for representing a Task that can be done easily with Runnable.
And, if you like to see the difference in tabular format, here is a nice table which shows the main differences between Thread class and Runnable interface in Java:
These were some of the notable differences between Thread and Runnable in Java if you know any other differences on Thread vs Runnable than please share them via comments.
I personally use Runnable over Thread for this scenario and recommend using the Runnable or Callable interface based on your requirement.
Though, its also a concurrency best practice in Java to not use Thread directly, instead you should leverage a Thread pools like Executors or ForkJoinPool where threads are created and managed by library.
If you use thread pool then its not only easy to execute as you only need to submit your task but also its easier to scale as you can increase the number of threads if your data processing increases or you can let the system do if you don't use fixed thread pool.
Some more Java Tutorials you may like
28 comments :
Why wait(),notify() and notifyAll() are declared in Object class but not in Thread ?
Hi Anonymous, I have discussed this question here
Why Wait, notify and notifyAll are defined in Object class .you may find useful.
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.
@Rahul, Thanks for your comment. Indeed Runnable is an interface and not a class.
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.
good article thnx
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 ?
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.
Could you please help me on this.
@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.
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"
Javin. A doubt related to interface. Why a data member in an interface implicitly final?
-HareshKannan
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.
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? :)
when should we use Runnable interface instead of Thread. Give logic apart from multiple inheritence in java
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
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.
For creating Thread why we have two ways of creation. Implementing Runnable is best then why do we need "Thread" class ??
please show some coding examples on how we can extend another class while implementing Runnable.
@Deepak
class GUI extends Canvas implements Runnable{
// do whatever you want to do
}
Hi plz assist in which scenario thread class using is better then runnable
why we create object of thread class inside main when we created thread using implementing runnable. plz explain.
I always used to save this Links of your articles from facebook it self
Thanks for the all articles as well as your efforts to teach us a better java ..
Request you to open a questions and answers form...when we can ask any doubt if we face .
Thanks in advance
@Anonymous, thanks, I'll think about it btw you can post question as comments for now
Can you explain 4th point with example please?
Hello Divya, this means you can create many object of same Runnable class and reuse them to run with multiple threads like a thread pool. A good example is RecursiveTask on ForkJoinPoool API.
Post a Comment