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.)
精彩评论