简单说说Runnable和Callable

Runnable和Callable这两个接口,是并发编程不可避免要谈的话题,而且总要被放到一起比较一番。太多的人写这两者之间的对比和差异了,在这里就只是随手记录一下自己的理解和想法了。

望文生义

Runnable,即可运行的。接口定义如下:

public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.

     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.

     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

</p>

Runnable接口的设计,本身就是想让一个类,实现这个可运行的接口,并传递给一个Thread去执行。当一个Thread执行run方法时,本质上是执行Runnable的run方法。

Callable,即可调用的,在金融领域,也是可提前兑付的意思。接口设计如下:

public interface Callable<v> {
    /**
     * Computes a result, or throws an exception if unable to do so.

     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

</v>

Callable接口的设计,就是想让一段逻辑被调用后,返回一个结果,并支持抛出异常。这个从JDK1.5开始引入的概念,就是为了补充Runnable产生的。

二者的不同点

本质上,Callable也是借助了同时实现了Runnable接口和Future接口的FutureTask来达到目的的。FutureTask作为一个Runnable,被线程执行。而FutureTask的run()方法,实际上调用了其自身持有的Callable对象的call()方法,并将结果保存在内部变量中。这部分内容会在Future相关文章中详细解读。

所以,我理解Callable是在Runnable的基础上,增强多线程执行任务的能力。

Original: https://www.cnblogs.com/codeflyer/p/16052642.html
Author: 小白码上飞
Title: 简单说说Runnable和Callable

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/714682/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球