【线程七】【翻译●中英双语】Java中Runnable和Callable有什么不同?

RunnableCallable都代表那些要在不同的线程中执行的任务。RunnableJDK1.0开始就有了,Callable是在JDK1.5增加的。它们的主要区别是Callable call() 方法可以返回值和抛出异常,而Runnablerun()方法没有这些功能。Callable可以返回装载有计算结果的Future对象。

Difference between Callable and Runnable interface in Java is one of the interesting question from my list of Top 15 Java multi-threading questions, and it’s also very popular in various Java Interviews. Callable interface is newer than Runnable interface and added on Java 5 release along with other major changes e.g. GenericsEnumStatic imports and variable argument method. Though both Callable andRunnable interface are designed to represent task, which can be executed by any thread, there is some significant difference between them. In my opinion, major difference between Callable and Runnable interface is that Callable can return result of operation performed inside call() method, which was one of the limitation with Runnable interface. Another significant difference between Runnable and Callableinterface is ability to throw checked exceptionCallable interface can throw checked exception because it’s call method throws Exception. By the way sometime this question is also asked as follow-up question of another classic difference between Runnable and Thread in Java. Commonly FutureTask is used along with Callable to get result of asynchronous computation task performed in call() method.

YOL翻译:

Callable 接口是个有意思的话题,在面试中很常见。Callable接口比Runnable接口新,它是随Java5更新,和 GenericsEnumStatic imports and variable argument method等重要更新一起加进来的,Callable和Runnable接口设计于代表任务,他们可被任何线程执行,他们两者之间差别很大。我觉得,主要区别是Callable接口可以在Call方法里面返回结果,而Runnable并不能。另一个区别是抛checked exception 的能力。Callable接口可以抛这个异常。平常情况下,FutureTask Callable 结合使用,在Call方法中得到异步的计算结果。

As I explained major differences between Callable and Runnable interface in last section. Sometime this question is also asked as difference between call() and run()  method in Java. All the points discussed here is equally related to that question as well.  Let’s see them in point format for better understanding :

 

1) Runnable interface is older than Callable, there from JDK 1.0, while Callable is added on Java 5.0.

2) Runnable interface has run() method to define task while Callable interface uses call() method for task definition.

3) run() method does not return any value, it’s return type is void while call method returns value. Callable interface is a generic parameterized interface and Type of value is provided, when instance of Callable implementation is created.

4) Another difference on run and call method is that run method can not throw checked exception, while call method can throw checked exception in Java.

That’s all on Difference between Callable and Runnable interface in Java or difference between call() and run() method. Both are very useful interface from core Java and good understanding of where to use Runnable and Callable is must for any good Java developer. In next article we will see example of Callable interface along with FutureTask to learn How to use Callable interface in Java.

区别正文

这个问题也可被问成Java中call和run方法区别。

1)Runnable接口比Callable接口老。前者来自JDK1.0,后者来自java5.0.

2)Runnable运行的是run(), 而Callable运行call()。

3)run不返回任何值,call返回值。当创建Callable接口实例后,执行Callable 接口是泛型参数化的接口和提供值类型。

4)另一个区别就是Callable可以抛 checked exception异常,Runnable不抛此异常。

 

这就是两者区别,下篇文章会举例说明如何使用Callable 。

发表评论

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