【线程四】【翻译】用Runnable还是Thread?

引言

这个问题是上题的后续,大家都知道我们可以通过继承Thread类或者调用Runnable接口来实现线程,问题是,那个方法更好呢?什么情况下使用它?这个问题很容易回答,如果你知道Java不支持类的多重继承,但允许你调用多个接口。所以如果你要继承其他类,当然是调用Runnable接口好了。

正题

Difference between Thread vs Runnable interface in Java

Thread vs Runnable in Java is always been a confusing decision for beginners  in java. Thread in Java seems easy in comparison of Runnable because you just deal with one class java.lang.Thread while in case of using Runnable to implement Thread you need to deal with both Thread and Runnable two classes. though decision of using Runnable or Thread should be taken considering differences between Runnable and Thread and pros and cons of both approaches. This is also a very popular thread interview questions and most of interviewer are really interested to know what is your point of view while choosing Thread vs Runnable or opposite. In this java article we will try to point out some differences between Thread and Runnable in Java which will help you to take an informed decision.

这个区别对初学者一直是迷之存在,Java线程 这篇简单的讨论了下,Runnable比较适合单继承的Java和扩展问题。

此问题也是面试热点,现在就讨论下两者区别,帮你做决定。

Here are some of my thoughts on whether I should use Thread or Runnable for implementing task in Java, though you have another choice as “Callable” for implementing thread which we will discuss later.

讨论前:虽然有另一个选择,使用“Callable” 接口,但是这个属于另外一种,稍后讨论

1) Java doesn’t support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java.

Java不支持多继承,所以如果继承了Thread,就没有机会再继承其它类

2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead.

在OO编程思想中,继承一个类一般是增加新功能、更新或者提高功能作用,如果不是为了做更新,就用接口吧

3) Runnable interface represent a Task which can be executed by either plain (简朴的,清淡的,无糖衣的)Thread or Executors or any other means. so logical separation of Task as Runnable than Thread is good design decision.

接口方法代表一个任务,可以被Thread, Executors或任何其它方式执行。所以逻辑上的任务划分当成Runnable比当成Thread是好的设计。

4) Separating task as Runnable means we can reuse the task and also has liberty(自由,自由权) to execute it from different means. since you can not restart a Thread once it completes. again Runnable vs Thread for task, Runnable is winner.

分解任务作为Runnable意味我们可以复用任务,也可以在不同的方式中自行去执行他。因为之前说过在线程结束后,你不能再启动他(TIP2),但是用Runnable就可以。所以,,又是Runnable咯。

5) Java designer recognizes this and that’s why Executors accept Runnable as Task and they have worker thread which executes those task.

Java设计者认识到了此问题,所以不难理解为什么Executors接受Runnable作为Task,然后他们用worker线程执行task

6) Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable.

These were some of notable difference between Thread and Runnable in Java, if you know any other differences on Thread vs Runnable than please share it via comments. I personally use Runnable over Thread for this scenario and recommends to use Runnable or Callable interface based on your requirement.

这就是两者区别所要记的,如果你还知道其它的,请留言!

我个人使用Runnable超过Thread, 推荐大家基于自己需求上,使用Runnable or Callable接口。

Yol 总结

1 两者区别最一般原因1)是Java是单继承,继承了Thread就没机会继承其它,2)是为了扩展

2 中级原因是OO思想和编程规范,增加新方法才用Thread继承,否则还用Runnable

3 高级原因是3)~6),任务划分做为一个Runnable设计,Thread和Executors都可以使Runable执行,并且Executors接受Runnable作为Task,然后他们用worker线程执行task,实现高级调度

发表评论

电子邮件地址不会被公开。