无返回值的 Runnable
| public interface Runnable { |
| public abstract void run(); |
| } |
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| ExecutorService executorService = Executors.newFixedThreadPool(5); |
| |
| |
| Runnable task = new Runnable() { |
| public void run() { |
| System.out.println(Thread.currentThread().getName()); |
| } |
| }; |
| |
| |
| for (int i = 0; i < 10; i++) { |
| executorService.submit(task); |
| } |
| |
| |
| executorService.shutdown(); |
| } |
有返回值的 Callable
| public interface Callable<V> { |
| V call() throws Exception; |
| } |
Callable 一般配合 ExecutorService 接口使用,它是 Java 线程池框架的核心接口,用来异步执行任务。它提供了一些关键方法用来进行线程管理。

- submit 方法既可以传入 Runnable 接口也可以传入 Callable接口
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| ExecutorService executorService = Executors.newFixedThreadPool(5); |
| |
| |
| Callable<String> task = new Callable<String>() { |
| @Override |
| public String call() throws Exception { |
| return Thread.currentThread().getName(); |
| } |
| }; |
| |
| |
| Future[] futures = new Future[10]; |
| for (int i = 0; i < 10; i++) { |
| futures[i] = executorService.submit(task); |
| } |
| |
| |
| for (int i = 0; i < 10; i++) { |
| System.out.println(futures[i].get()); |
| } |
| |
| |
| executorService.shutdown(); |
| } |
异步计算结果 Future 接口
| public interface Future<V> { |
| |
| boolean cancel(boolean mayInterruptIfRunning); |
| |
| boolean isCancelled(); |
| |
| boolean isDone(); |
| |
| V get() throws InterruptedException, ExecutionException; |
| |
| V get(long timeout, TimeUnit unit) |
| throws InterruptedException, ExecutionException, TimeoutException; |
| } |
Future 提供了三种功能:
- 1)判断任务是否完成;
- 2)能够中断任务;
- 3)能够获取任务执行结果。
FutureTask 是 Future 接口的一个唯一实现类,前面的例子中 executorService.submit()
返回的就是 FutureTask
异步计算结果 FutureTask 实现类
| |
| public interface RunnableFuture<V> extends Runnable, Future<V> { |
| void run(); |
| } |
| public class FutureTask<V> implements RunnableFuture<V> { |
| public FutureTask(Callable<V> callable) {} |
| |
| public FutureTask(Runnable runnable, V result) {} |
| ... |
| } |
当需要异步执行一个计算并在稍后的某个时间点获取其结果时,就可以使用 FutureTask
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| ExecutorService executorService = Executors.newFixedThreadPool(3); |
| |
| |
| Callable<Integer>[] tasks = new Callable[5]; |
| for (int i = 0; i < tasks.length; i++) { |
| final int index = i; |
| tasks[i] = new Callable<Integer>() { |
| @Override |
| public Integer call() throws Exception { |
| TimeUnit.SECONDS.sleep(index + 1); |
| return index + 1; |
| } |
| }; |
| } |
| |
| |
| FutureTask<Integer>[] futureTasks = new FutureTask[tasks.length]; |
| for (int i = 0; i < tasks.length; i++) { |
| futureTasks[i] = new FutureTask<>(tasks[i]); |
| executorService.submit(futureTasks[i]); |
| } |
| |
| |
| for (int i = 0; i < futureTasks.length; i++) { |
| System.out.println("Result of task" + (i + 1) + ": " + futureTasks[i].get()); |
| } |
| |
| |
| executorService.shutdown(); |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18310301
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步