开发者

Which way is the best to run a thread in java? [duplicate]

This question already has answers here: 开发者_开发技巧 Closed 12 years ago.

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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜