2

Possible Duplicate:
Java: “implements Runnable” vs. “extends Thread”

Should I use Runnable interface or extends from Thread class? Is there any advantage on one and another?

Thanks in advance.

Community
  • 1
  • 1
Neuquino
  • 11,580
  • 20
  • 62
  • 76

2 Answers2

3
  • you can only extend 1 class so if you have multiple inheritance you can only use an interface. I never extend Thread class, but implement runnable/callable interface.
  • I use executors to help me with thread-management.
Alfred
  • 60,935
  • 33
  • 147
  • 186
  • 1
    +1 always avoid inheritance if you can. It is much cleaner to use an executor with a ThreadFactory to separate the creation and lifecycle management of your threads from the code they will actually run (Runnables and Callables). – Jed Wesley-Smith Nov 30 '10 at 03:23
1

As far as I know it's just a matter of preference.

My own preference is to pass a Runnable, because I don't like the subclassing approach nor the fact that Thread implements Runnable. (Subclassing Thread to implement run() abuses the "is-a" relationship that subclassing is supposed to represent. Making Thread implement Runnable doesn't really add any value, and it allows for nonsensical things like using one thread as the runnable construction parameter of another thread.)

Wyzard
  • 33,849
  • 3
  • 67
  • 87