Runnable 和 Callable 的区别

区别

  1. 两个都是接口,但是 Runnable 方法中 run() 没有声明异常且没有返回值,而 Callablecall() 声明了异常且有返回值;(意味着使用 Runnable 需要自己处理异常)
  2. 通过 Callable 创建线程需要一个辅助类 FutureTask 实例化对象来接收线程执行完毕后得到的结果,通过 FutureTask 中的 get() 获取结果,如果线程阻塞,那么就会等待。
    Runnable 中如果想要得到结果,那么就需要定义一个属性来被修改与接收,如果线程阻塞或者只有主线程和该线程,那么需要手动等待,即 thread.jion();

源码

  • Callable
@FunctionalInterface
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;
}
  • Runnable
@FunctionalInterface
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();
}

观察源码看到
Runnablerun() 没有声明异常,且没有返回值;
Callablecall() 声明了异常,且有返回值。

因此使用 Runnable 创建线程的时候,需要在 run() 中手动处理异常;
Callable 方法中使用 call() 则不需要

创建线程

以计算 1 到 100 的和为例

  • Runnable
class MyRunnable {
        static int sum = 0;
        public static void main(String[] args) throws InterruptedException {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <= 100; i++) {
                        sum += i;
                    }
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
            thread.join();
            System.out.println(sum);
        }
    }
  • Callable
class MyCallable {
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            Callable<Integer> callable = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int sum = 0;
                    for (int i = 1; i <= 100; i++) {
                        sum += i;
                    }
                    return sum;
                }
            };

            FutureTask<Integer> task = new FutureTask<>(callable);
            Thread thread = new Thread(task);
            thread.start();
            Integer res = task.get();
            System.out.println(res);
        }
    }

可见,创建线程的方式不一样,
Runnable 比较简单

  • 定义静态变量 sum;
  • 重写 run();
  • 将匿名 runnable 对象传入 Thread 即可;
  • 然后启用 start 方法
  • 最后还需要等待线程执行完毕,即调用 jion() 最后再输出结果;

Callable

  • 重写 call();
  • 定义局部变量 sum;
  • 定义 FutureTask 类来接收结果;
  • 启动线程;
  • 输出结果。
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值