在Java并发编程中,Runnable、Callable、Future、RunnableFuture 和 FutureTask 这些接口和类都是为了支持异步任务执行和结果获取而设计的。下面分别说明它们的设计原理并提供使用范例。
- Runnable Interface
Runnable 是 Java 中最基本的线程任务接口,它只包含一个 run() 方法,用于定义线程需要执行的任务。
public interface Runnable {
void run();
}
// 使用示例
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Running a task in a thread...");
}
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start();
}
}
- Callable Interface
Callable 接口扩展了 Runnable 的功能,它提供了有返回值的任务,并且可以抛出异常。通过实现 call() 方法,我们可以创建一个能返回结果的任务。
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class MyCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int result = someComputation(); // 假设这是个计算方法
return result;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable callable = new MyCallable();
FutureTask<Integer> futureTask = new FutureTask<>(callable);
Thread t = new Thread(futureTask);
t.start();
// 获取线程执行完成后的结果
Integer computedResult = futureTask.get();
System.out.println("Computed result: " + computedResult);
}
}
- Future Interface
Future 接口代表了一个异步计算的结果,提供了检查计算是否完成、阻塞等待计算结果以及获取计算结果的方法。
import java.util.concurrent.Future;
// 通常不直接实现 Future 接口,而是由其他类如 FutureTask 实现
public class FutureExample {
public void executeTaskWithFuture() throws ExecutionException, InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> future = executor.submit(new MyCallable());
// 可以做其他事情,然后...
// 当需要结果时,调用 get() 方法会阻塞直到结果准备好
Integer result = future.get();
System.out.println("Result from Future: " + result);
// 关闭线程池
executor.shutdown();
}
}
- RunnableFuture Interface
RunnableFuture 同时继承了 Runnable 和 Future 接口,这意味着它是一个可运行的任务,同时也能作为 Future 来获取结果。
import java.util.concurrent.RunnableFuture;
// 不直接实现 RunnableFuture,而是使用 FutureTask 等已实现它的类
class MyRunnableFuture extends FutureTask<Integer> {
public MyRunnableFuture(Callable<Integer> callable) {
super(callable);
}
public void customMethod() {
// 可以添加额外的自定义方法
}
}
- FutureTask Class
FutureTask 类实现了 RunnableFuture 接口,因此它可以被提交给 Executor 执行,同时又可以作为 Future 来查询结果或取消任务。
import java.util.concurrent.FutureTask;
public class FutureTaskExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 创建一个 FutureTask,传入 Callable 对象
FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
// 创建一个线程来执行这个任务
Thread thread = new Thread(futureTask);
thread.start();
// 或者将 FutureTask 提交到 ExecutorService
// ExecutorService executor = Executors.newSingleThreadExecutor();
// executor.execute(futureTask);
// 获取结果
Integer result = futureTask.get();
System.out.println("Result from FutureTask: " + result);
}
}
总结来说,当需要在线程中执行带有返回值的任务时,通常会选择 Callable 接口配合 FutureTask 类或者直接将 Callable 任务提交给 ExecutorService。FutureTask 能够方便地将 Runnable 或 Callable 的任务与 Future 结果机制相结合,使得主线程能够获取到异步计算的结果。