java面试题-Runnable和Callable有什么区别?
在Java多线程编程中,Runnable
和 Callable
接口都用于创建多线程,但它们之间存在一些关键的区别。本教程将深入研究这两个接口,通过详细的示例代码演示它们的用法,旨在帮助初学者更好地理解和运用这两个重要的多线程接口。
1. 主要区别
1.1 返回值
- Runnable 接口:
run
方法无返回值。 - Callable 接口:
call
方法有返回值,支持泛型。
1.2 异常处理
- Runnable 接口:
run
方法只能抛出运行时异常,且无法捕获处理。 - Callable 接口:
call
方法允许抛出异常,并可以获取异常信息。
2. 示例代码演示
2.1 创建与启动线程的方式
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class TestRunnableAndCallable {
public static void main(String[] args) {
testImplementsRunnable();
testImplementsCallable();
testImplementsCallableWithException();
}
// 测试实现 Runnable 接口的方式创建、启动线程
public static void testImplementsRunnable() {
Thread t1 = new Thread(new CustomRunnable());
t1.setName("CustomRunnable");
t1.start();
}
// 测试实现 Callable 接口的方式创建、启动线程
public static void testImplementsCallable() {
Callable<String> callable = new CustomCallable();
FutureTask<String> futureTask = new FutureTask<>(callable);
Thread t2 = new Thread(futureTask);
t2.setName("CustomCallable");
t2.start();
try {
System.out.println(futureTask.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
// 测试实现 Callable 接口的方式创建、启动线程,抛出异常
public static void testImplementsCallableWithException() {
Callable<String> callable = new CustomCallable2();
FutureTask<String> futureTask = new FutureTask<>(callable);
Thread t3 = new Thread(futureTask);
t3.setName("CustomCallableWithException");
t3.start();
try {
System.out.println(futureTask.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// 实现 Runnable 接口,重写 run 方法
class CustomRunnable implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
// throw new RuntimeException("aaa");
}
}
// 实现 Callable 接口,重写 call 方法
class CustomCallable implements Callable<String> {
public String call() {
System.out.println(Thread.currentThread().getName());
return "Callable Result";
}
}
// 实现 Callable 接口,重写 call 方法,抛出异常
class CustomCallable2 implements Callable<String> {
public String call() throws Exception {
System.out.println(Thread.currentThread().getName());
throw new Exception("I can compute a result");
}
}
2.2 代码解释
testImplementsRunnable()
方法演示了通过实现Runnable
接口的方式创建和启动线程。testImplementsCallable()
方法演示了通过实现Callable
接口的方式创建和启动线程,并获取返回值。testImplementsCallableWithException()
方法演示了Callable
接口抛出异常的情况。
3. 执行结果
执行以上代码,将得到如下输出:
CustomRunnable
CustomCallable
Callable Result
CustomCallableWithException
java.util.concurrent.ExecutionException: java.lang.Exception: I can compute a result
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at Lee.interview.TestRunnableAndCallable.testImplementsCallableWithException(TestRunnableAndCallable.java:46)
at Lee.interview.TestRunnableAndCallable.main(TestRunnableAndCallable.java:12)
Caused by: java.lang.Exception: I can compute a result
at Lee.interview.CustomCallable2.call(TestRunnableAndCallable.java:81)
at Lee.interview.CustomCallable2.call(TestRunnableAndCallable.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
以上结果清晰地展示了通过不同接口创建线程的方式,以及处理返回值和异常的情况。
4. 总结
通过本教程,我们深入了解了 Runnable
和 Callable
接口在Java中的区别,并通过详细的示例代码演示了它们的使用方法。理解这两个接口的不同之处有助于更灵活地处理多线程编程中的各种场景。
正文到此结束
- 本文标签: Java 面试题
- 本文链接: https://www.jietongc.com/article/85
- 版权声明: 本文由大熊科技原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权