Difference between Runnable interface & Thread class?Which one to use, though both seems to be same? As per my knowledge,runnable can be implemented in such a case that already it has some hierarchy

-Thanks in advance

Recommended Answers

All 7 Replies

Implement Runnable unless you have some specific reason for extending Thread.

this is the problem which i have encountered many times in case of making GUIs using swings or either making applets for which you have to extend JFrame or Applet.
since java dont support multiple inheritence, you cant extend Threads simultaneously with JFrame or Applet. in that case we have to implement Runnable for using multithreading.

Well, unless you have a specific reason to extend JFrame, don't extend that either. Google "composition".

Implement Runnable unless you have some specific reason for extending Thread.

Thanks masijade.Still,not clear about this.
Say,test is a class which doesn't hav any parents.In such a case,why should i go specifically for runnable implementation instead Thread inherit.Pls,let me explain,the advantage of using runnable.

Thanks masijade.Still,not clear about this.
Say,test is a class which doesn't hav any parents.In such a case,why should i go specifically for runnable implementation instead Thread inherit.Pls,let me explain,the advantage of using runnable.

If there isn't any behavior of Thread that you need to alter by subclassing, why would you extend Thread? That's what they are saying here. If you can't point to an advantage in your code to extend Thread then you most likely have no reason to.

Keep in mind though, Runnable cannot return a value nor throw exceptions. If you need that functionality, use Callable.

Thanks Ezzaral & masijade, your replies makes me clear now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.