callable和runnable区别(3大区别详解)

callable和runnable区别(3大区别详解)-mikechen

Callable和Runnable 都是用于在多线程环境中执行任务的接口,它们之间的主要区别有以下3点@mikechen

返回值的区别

Callable

Callable 是一个泛型接口,它要求实现类必须实现 call() 方法,该方法可以返回一个结果,即具有返回值。

如下所示:

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        // 执行任务并返回结果
        return 42;
    }
}

// 使用Callable
Callable<Integer> callable = new MyCallable();
FutureTask<Integer> futureTask = new FutureTask<>(callable);
Thread thread = new Thread(futureTask);
thread.start();
Integer result = futureTask.get();

call() 方法的返回值的类型由泛型参数指定。

Runnable

Runnable 也是一个接口,但它的 run() 方法没有返回值。

如下所示:

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        // 执行任务
    }
}

// 使用Runnable
Runnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();

因此,Runnable 用于执行无需返回结果的任务。

 

异常处理的区别

Callable

call() 方法可以声明抛出受检异常,因此你可以更灵活地处理异常情况。

如下所示:

import java.util.concurrent.Callable;

public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        if (someCondition) {
            throw new MyCheckedException("An error occurred");
        }
        return 42;
    }
}

Runnable

run() 方法只能抛出未受检异常(unchecked exception),在异常处理方面不如 Callable 灵活。

如下所示:

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        try {
            if (someCondition) {
                throw new MyUncheckedException("An error occurred");
            }
        } catch (MyUncheckedException e) {
            // 处理异常或将其传递给线程的未捕获异常处理程序
        }
    }
}

 

使用方式的区别

Callable

Callable通常与 ExecutorService 结合使用,通过 submit(Callable) 方法提交任务,并可以获得表示任务结果的 Future 对象,以便获取任务的返回值。

如下所示:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        // 执行任务并返回结果
        return 42;
    }

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<Integer> future = executorService.submit(new MyCallable());
        Integer result = future.get(); // 阻塞等待任务完成并获取结果
        System.out.println("Result: " +

 

Runnable

Runnable通常用于创建 Thread 对象,然后通过 start() 方法启动线程执行任务。

如下所示:

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        // 执行任务
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable());
        thread.start(); // 启动线程执行任务
    }
}

Runnable 不直接提供返回值,你可能需要使用其他方式来传递结果或状态信息。

评论交流
    说说你的看法