并发基础之Callable与Future获取异步执行结果
创始人
2025-05-31 14:41:27
0

目录

    • 前言
    • 浅析Callale接口
      • Callale源码分析
      • Callale简单演示
    • 浅析Future接口
      • Future源码分析
      • Future简单演示
    • 结语

前言

在多线程编程中,我们一般会根据实际业务场景来创建线程实现功能。当然创建线程的方式也有很多,比如继承Thread类、实现Runable接口或直接根据Executors类创建线程池获取线程。但是继承Thread类和实现Runable并不能获取线程执行结果,如果我们有需求要获取执行结果怎么处理呢?答案就是今天的主角——Callale与Future接口。

浅析Callale接口

Callale源码分析

进入JUC 包下找到 Callable 接口

@FunctionalInterface
public interface Callable {/*** 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;
}

如上源码所示,Callable 接口内部有个call() 方法,该方法可以返回结果,如果有异常情况会抛出异常。call() 方法是 Callable 接口唯一且核心方法,我们在实际开发场景中,在call() 方法中写入具体逻辑代码并可以返回我们需要的结果。

我们继续查看 Callable 接口实现类
在这里插入图片描述

如图所示我们有很多工具类实现了 Callable接口,比如比较常见的 RunableAdapter、TaskCallable等等工具类,在实际业务场景我们可以任意选用我们喜欢的方式实现自己的多线程功能。

Callale简单演示

比如我们现在需要在业务场景中使用多线程执行,并且需要返回执行中的一个结果。那么,我们直接实现Callable即可:

/*** TestCallable* @author senfel* @date 2023/3/20 11:06 * @return void*/
@Test
public void TestCallable() throws Exception{Callable callable = new Callable() {@Overridepublic String call() throws Exception {//具体的业务场景省略 //......//模拟返回执行结果return "测试1";}};//获取执行结果String call = callable.call();//打印结果在控制台System.err.println(call);
}

这里我们直接是简便的编码方式,当前不怕麻烦的同学可以单独写一个类实现Callable接口,然后在实例化自身的线程类并调用。

浅析Future接口

Future源码分析

上文我们讲到了实现Callable接口创建线程并获取执行接口,下面我们看看实现Future 接口方式有何异同。我们先来看源码:

package java.util.concurrent;/**
* Future接口
*/
public interface Future {/*** 取消*/boolean cancel(boolean mayInterruptIfRunning);/*** 获取是否取消*/boolean isCancelled();/*** 任务是否完成*/boolean isDone();/*** 循环等待执行结果*/V get() throws InterruptedException, ExecutionException;/*** 超时等待执行结果*/V get(long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException;
}

如上源码所示,我们发现 Future 接口内部提供了很多很多的操作方法。比如 cancel() 取消执行、isCancelled () 获取是否取消状态、isDone() 任务是否完成,当然还有我们最重要的 get() 获取执行结果的方法。

看到这里我们不难发现,Future 接口比 Callable接口多了很多很多的操作线程的方法。这些操作方式可以使得线程操作更加灵活,也能够让开发人员适用跟过的业务场景编码。我想说,看到Future 接口我就不想用 Callable了呢。

继续查看Futrue实现类,我们看看有哪些
在这里插入图片描述

如图所示,有很多的实现类。其中有大家不陌生并且经常使用到的ForkJoinTask、FutureTask,这里两个实现类应该算是在多线程开发中经常使用的吧。

我们选一个实现类进入查看Future具体的实现方式吧,我们就用 FutureTask 开探究为啥 Future要比Callable多那么多的操作方法。

进入 JUC FutureTask 查看源码

public interface RunnableFuture extends Runnable, Future {/*** Sets this Future to the result of its computation* unless it has been cancelled.*/void run();
}
public class FutureTask implements RunnableFuture {/** 执行线态* NEW -> COMPLETING -> NORMAL* NEW -> COMPLETING -> EXCEPTIONAL* NEW -> CANCELLED* NEW -> INTERRUPTING -> INTERRUPTED*/private volatile int state;private static final int NEW          = 0;private static final int COMPLETING   = 1;private static final int NORMAL       = 2;private static final int EXCEPTIONAL  = 3;private static final int CANCELLED    = 4;private static final int INTERRUPTING = 5;private static final int INTERRUPTED  = 6;/** The underlying callable; nulled out after running */private Callable callable;/** The result to return or exception to throw from get() */private Object outcome; // non-volatile, protected by state reads/writes/** The thread running the callable; CASed during run() */private volatile Thread runner;/** Treiber stack of waiting threads */private volatile WaitNode waiters;/*** Returns result or throws exception for completed task.** @param s completed state value*/@SuppressWarnings("unchecked")private V report(int s) throws ExecutionException {Object x = outcome;if (s == NORMAL)return (V)x;if (s >= CANCELLED)throw new CancellationException();throw new ExecutionException((Throwable)x);}/*** Creates a {@code FutureTask} that will, upon running, execute the* given {@code Callable}.** @param  callable the callable task* @throws NullPointerException if the callable is null*/public FutureTask(Callable callable) {if (callable == null)throw new NullPointerException();this.callable = callable;this.state = NEW;       // ensure visibility of callable}/*** Creates a {@code FutureTask} that will, upon running, execute the* given {@code Runnable}, and arrange that {@code get} will return the* given result on successful completion.** @param runnable the runnable task* @param result the result to return on successful completion. If* you don't need a particular result, consider using* constructions of the form:* {@code Future f = new FutureTask(runnable, null)}* @throws NullPointerException if the runnable is null*/public FutureTask(Runnable runnable, V result) {this.callable = Executors.callable(runnable, result);this.state = NEW;       // ensure visibility of callable}public boolean isCancelled() {return state >= CANCELLED;}public boolean isDone() {return state != NEW;}public boolean cancel(boolean mayInterruptIfRunning) {if (!(state == NEW &&UNSAFE.compareAndSwapInt(this, stateOffset, NEW,mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))return false;try {    // in case call to interrupt throws exceptionif (mayInterruptIfRunning) {try {Thread t = runner;if (t != null)t.interrupt();} finally { // final stateUNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);}}} finally {finishCompletion();}return true;}}

如上源码 FutureTask 实现了 RunnableFuture 接口,RunnableFuture 又是实现了Runable和Future,所以FutureTask自然拥有了Future的功能。但是我们也看到FutureTask 构造方法需要传入实例化线程,这里可以是 Callable 实现的线程。分析到这里我们就明白了什么Future 要比 Callable多那么多的方法?原因其实就是Future是对线程操作的升华,不仅仅创建执行线程,更多的是对创建线程执行状态的维护。

Future简单演示

既然上文我们分析了FutureTask ,这里就用FutureTask 简单演示吧

    /*** TestFutureTask* @author senfel* @date 2023/3/20 11:34* @return void*/@Testpublic void TestFutureTask() throws Exception {//实例化FutureTask,并传入一个用Callable实现的线程FutureTask task = new FutureTask(new Callable() {@Overridepublic String call() throws Exception {return "TestFutureTask";}});//运行任务task.run();//获取执行结果String s1 = task.get();//结果打印System.err.println(s1);}
}

这里我们直接也是简便的编码方式,当前不怕麻烦的同学可以单独写一个类实现Callable接口,然后在实例化FutrueTask时传入。

当然,在使用Futrue过程中我们推荐线程池获取Futrue的方式

/*** TestCallableByPool** @author senfel* @date 2023/3/20 11:43 * @return void*/
@Test
public void TestCallableByPool() throws Exception{//创建一个定长线程池ExecutorService executorService = Executors.newFixedThreadPool(10);//获取FutureFuture future = executorService.submit(new Callable() {@Overridepublic String call() throws Exception {//具体的业务场景省略//......//模拟返回执行结果return "TestCallableByPool";}});String s = future.get();System.err.println(s);
}

结语

在多线程编码需要获取线程执行结果的情况下,我们推荐使用实现Futrue 接口的方式,它比Callable方式多了很多的操作执行线程的方法,比如 cancel() 取消执行、isCancelled () 获取是否取消状态、isDone() 任务是否完成等等。在实际的开发中,我们也推荐使用多线程来创建Futrue获取执行结果。

相关内容

热门资讯

计算机组成原理实验1---运算...     本实验为哈尔滨工业大学计算机组成原理实验,实验内容均为个人完成,...
3 ROS1通讯编程提高(1) 3 ROS1通讯编程提高3.1 使用VS Code编译ROS13.1.1 VS Code的安装和配置...
前端-session、jwt 目录:   (1)session (2&#x...
前端学习第三阶段-第4章 jQ... 4-1 jQuery介绍及常用API导读 01-jQuery入门导读 02-JavaScri...
EL表达式JSTL标签库 EL表达式     EL:Expression Language 表达式语言     ...
数字温湿度传感器DHT11模块... 模块实例https://blog.csdn.net/qq_38393591/article/deta...
【内网安全】 隧道搭建穿透上线... 文章目录内网穿透-Ngrok-入门-上线1、服务端配置:2、客户端连接服务端ÿ...
【Spring Cloud A... 文章目录前言Metadata元数据ClassMetadataSpring中常见的一些元注解Nacos...
React篇-关于React的... 一.简介1.介绍用于构建用户界面的 JavaScript 库2.创建项目(1)手动创建Documen...
win7 Pro 英文版添加中... win7pro x64英文版添加中文语言包1、下载语言包,并解压成lp.cab,复制到...
Android开发-Andro... 01  Android UI 1.1  UI 用户界面(User Interface,...
基于springboot教师人... 基于springboot教师人事档案管理系统【源码+论文】 开发语言:Jav...
编写软件界面的方式 本文重点解决如下问题:编写软件的界面有哪几种方式?通常情形下࿰...
keil调试专题篇 调试的前提是需要连接调试器比如STLINK。 然后点击菜单或者快捷图标均可进入调试模式。 如果前面...
GO语言小锤硬磕十三、数组与切... 数组用来保存一组相同类型的数据,go语言数组也分一维数组和多维数组。 直接上代码看一下...
三级数据库备考--数据库应用系... 1.数据库应用系统设计包括概念设计、逻辑设计、物理设计3个步骤,每个步骤的设计活动按照...
prometheus数据持久化... https://segmentfault.com/a/1190000015710814 promet...
孩子用什么样的灯对眼睛没有伤害... 现代社会高速发展,越来越多的人开始重视身体健康,尤其是很多家长ÿ...
微软Bing GPT支持AI绘... 我想要一张图片:大象、珊瑚、火山、云朵我想要一张图片:亚特兰蒂斯...
最新!5月IPO受理数创今年新... IPO市场新动态。5月30日,共有4家IPO企业获得受理,其中创业板打破今年“0受理”现象,迎来首批...