1

Possible Duplicate:
Implementing Runnable vs. extending Thread

I am very much confused about the perfect answer in simple language for above question. In which scenario which is better and why. can anyone explain.

Community
  • 1
  • 1
aj983
  • 293
  • 2
  • 5
  • 12

3 Answers3

2

The Executor classes talk in terms of Runnable, and for that reason alone I favour implementing the Runnable interface. You don't carry the baggage of having your code implicitly tied into a thread and frameworks can handle Runnables in whatever way they prefer.

Brian Agnew
  • 261,477
  • 36
  • 323
  • 432
1

Use these simple rules:

1) If the main purpose of a class is to be a thread, then subclass Thread

2) If the main purpose of a class is not to be a thread (e.g.: a GUI component), implement Runnable

3) Use Runnable when the thread class is already sub-classed by another class

hovanessyan
  • 32,578
  • 7
  • 55
  • 79
1

Extending the Thread class will make your class unable to extend other classes, because of the single inheritence feature in JAVA. However, this will give you a simpler code structure. If you implement runnable, you can gain better object-oriented design and consistency and also avoid the single inheritance problems. When developing a thread class, if it don't has any super class then go for Thread class else go for Runnable interface and implement the methods in it. It is the basic thumb rule.

Sumit Singh
  • 24,095
  • 8
  • 74
  • 100