CompletableFutrue异步处理

news2024/9/21 22:23:22

异步处理

image.png

一、线程的实现方式

1. 线程的实现方式

1.1 继承Thread

class ThreadDemo01 extends Thread{
    @Override
    public void run() {
        System.out.println("当前线程:" + Thread.currentThread().getName());
    }
}

1.2 实现Runnable接口

class ThreadDemo02 implements Runnable{
    @Override
    public void run() {
        System.out.println("当前线程:" + Thread.currentThread().getName());
    }
}

1.3 Callable接口

class MyCallable implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        System.out.println("当前线程:" + Thread.currentThread().getName());
        return 10;
    }
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main方法执行了...");
        ThreadDemo01 t1 = new ThreadDemo01();
        t1.start();

        ThreadDemo02 t2 = new ThreadDemo02();
        new Thread(t2).start();
        new Thread(()->{
            System.out.println("当前线程:" + Thread.currentThread().getName());
        }).start();

        // 通过Callable接口来实现  FutureTask 本质上是一个Runnable接口
        FutureTask futureTask = new FutureTask(new MyCallable());
        Thread t3 = new Thread(futureTask);
        t3.start();
        // 阻塞等待子线程的执行完成,然后获取线程的返回结果
        Object o = futureTask.get();
        System.out.println("o = " + o);
        System.out.println("main方法结束了...");
    }

2.线程池的实现

  上面的三种获取线程的方法是直接获取,没有对线程做相关的管理,这时可以通过线程池来更加高效的管理线程对象。

// 定义一个线程池对象
    private static ExecutorService service = Executors.newFixedThreadPool(5);

然后我们就可以通过这个线程池对象来获取对应的线程

        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程池--》当前线程:" + Thread.currentThread().getName());
            }
        });

3. 获取线程的区别

  通过上面的介绍我们发现获取线程的方式

  • 继承Thread对象
  • 实现Runnable接口
  • 实现Callable接口
  • 线程池

继承Thread对象和实现Runnable接口没有办法获取返回结果的,实现Callable接口可以获取线程的返回结果。当然这三种方式都不能控制我们的资源,线程池可以控制资源。

二、线程池的详解

1.线程池的创建方式

  • 通过Executors的静态方法
  • 通过 new ThreadPoolExecutor方式创建

七大参数的作用

参数作用
corePoolSize核心线程数,线程池创建好后就准备就绪的线程数量,一直存在
maximumPoolSize最大线程数量,控制资源
keepAliveTime存活时间,如果当前线程数量如果大于核心线程数量,释放空闲的线程,<br />最大线程-核心数量
unit时间单位
BlockingQueue阻塞队列,如果任务很多,就会把多的任务放在队列中
threadFactory线程的工厂
handler如果队列满了,按照指定的拒绝策略执行任务
    /**
     * 线程池详解
     * @param args
     */
    public static void main(String[] args) {
        // 第一种获取的方式
        ExecutorService service = Executors.newFixedThreadPool(10);
        // 第二种方式: 直接new ThreadPoolExecutor()对象,并且手动的指定对应的参数
        // corePoolSize:线程池的核心线程数量 线程池创建出来后就会 new Thread() 5个
        // maximumPoolSize:最大的线程数量,线程池支持的最大的线程数
        // keepAliveTime:存活时间,当线程数大于核心线程,空闲的线程的存活时间 8-5=3
        // unit:存活时间的单位
        // BlockingQueue<Runnable> workQueue:阻塞队列 当线程数超过了核心线程数据,那么新的请求到来的时候会加入到阻塞的队列中
        // new LinkedBlockingQueue<>() 默认队列的长度是 Integer.MAX 那这个就太大了,所以我们需要指定队列的长度
        // threadFactory:创建线程的工厂对象
        // RejectedExecutionHandler handler:当线程数大于最大线程数的时候会执行的淘汰策略
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5
                , 100
                , 10
                , TimeUnit.SECONDS
                , new LinkedBlockingQueue<>(10000)
                , Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.AbortPolicy()
        );
        poolExecutor.execute(()->{
            System.out.println("----->" + Thread.currentThread().getName());
        });
    }

2.线程池的执行顺序

线程池创建,准备好core数量的核心线程,准备接收任务

image.png

  • 1.先判断核心线程是否已满,未满分配线程
  • 2.任务队列是否已满,未满放入队列
  • 3.是否达到最大的线程数量,未达到创建新的线程
  • 4.通过对应的reject指定的拒绝策略进行处理

线程池的面试题:

  • 有一个线程池,core:5,max:50,queue:100,如果并发是200,那么线程池是怎么处理的?
  • 首先 200个中的前面5个会直接被核心线程处理,然后6个到105个会加入到阻塞队列中,然后106到155的请求在最大线程数中,那么会创建对应的线程来处理这些请求,之后剩下的45个请求会被直接放弃

image.png

3.线程池的好处

  • 降低资源消耗
  • 提高响应速度
  • 提高线程的管理

三、CompletableFutrue

一个商品详情页

  • 展示SKU的基本信息 0.5s
  • 展示SKU的图片信息 0.6s
  • 展示SKU的销售信息 1s
  • spu的销售属性 1s
  • 展示规格参数 1.5s
  • spu详情信息 1s

1.ComplatableFuture介绍

  Future是Java 5添加的类,用来描述一个异步计算的结果。你可以使用 isDone方法检查计算是否完成,或者使用 get阻塞住调用线程,直到计算完成返回结果,你也可以使用 cancel方法停止任务的执行。

  虽然 Future以及相关使用方法提供了异步执行任务的能力,但是对于结果的获取却是很不方便,只能通过阻塞或者轮询的方式得到任务的结果。阻塞的方式显然和我们的异步编程的初衷相违背,轮询的方式又会耗费无谓的CPU资源,而且也不能及时地得到计算结果,为什么不能用观察者设计模式当计算结果完成及时通知监听者呢?

  很多语言,比如Node.js,采用回调的方式实现异步编程。Java的一些框架,比如Netty,自己扩展了Java的 Future接口,提供了 addListener等多个扩展方法;Google guava也提供了通用的扩展Future;Scala也提供了简单易用且功能强大的Future/Promise异步编程模式。

  作为正统的Java类库,是不是应该做点什么,加强一下自身库的功能呢?

  在Java 8中, 新增加了一个包含50个方法左右的类: CompletableFuture,提供了非常强大的Future的扩展功能,可以帮助我们简化异步编程的复杂性,提供了函数式编程的能力,可以通过回调的方式处理计算结果,并且提供了转换和组合CompletableFuture的方法。

  CompletableFuture类实现了Future接口,所以你还是可以像以前一样通过 get方法阻塞或者轮询的方式获得结果,但是这种方式不推荐使用。

  CompletableFuture和FutureTask同属于Future接口的实现类,都可以获取线程的执行结果。

image.png

2.创建异步对象

CompletableFuture 提供了四个静态方法来创建一个异步操作。

static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

方法分为两类:

  • runAsync 没有返回结果
  • supplyAsync 有返回结果
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(5
            ,50
            ,10
            , TimeUnit.SECONDS
            ,new LinkedBlockingQueue<>(100)
            , Executors.defaultThreadFactory()
            ,new ThreadPoolExecutor.AbortPolicy()
    );

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("main -- 线程开始了...");
        // 获取CompletableFuture对象
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("线程开始了...");
            int i = 100/50;
            System.out.println("线程结束了...");
        },executor);
        System.out.println("main -- 线程结束了...");

        System.out.println("------------");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("线程开始了...");
            int i = 100 / 50;
            System.out.println("线程结束了...");
            return i;
        }, executor);
        System.out.println("获取的线程的返回结果是:" + future.get() );
    }

3.whenXXX和handle方法

  当CompletableFuture的计算结果完成,或者抛出异常的时候,可以执行特定的Action。主要是下面的方法:

public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action);
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action);
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor);

public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn);

public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) ;
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) ;
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) ;

相关方法的说明:

  • whenComplete 可以获取异步任务的返回值和抛出的异常信息,但是不能修改返回结果
  • execptionlly 当异步任务跑出了异常后会触发的方法,如果没有抛出异常该方法不会执行
  • handle 可以获取异步任务的返回值和抛出的异常信息,而且可以显示的修改返回的结果

4.线程串行方法

thenApply 方法:当一个线程依赖另一个线程时,获取上一个任务返回的结果,并返回当前任务的返回值。

thenAccept方法:消费处理结果。接收任务的处理结果,并消费处理,无返回结果。

thenRun方法:只要上面的任务执行完成,就开始执行thenRun,只是处理完任务后,执行 thenRun的后续操作

带有Async默认是异步执行的。这里所谓的异步指的是不在当前线程内执行。

public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)

public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);

public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);

5.两个都完成

  上面介绍的相关方法都是串行的执行,接下来看看需要等待两个任务执行完成后才会触发的几个方法

  • thenCombine :可以获取前面两线程的返回结果,本身也有返回结果
  • thenAcceptBoth:可以获取前面两线程的返回结果,本身没有返回结果
  • runAfterBoth:不可以获取前面两线程的返回结果,本身也没有返回结果
/**
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1 线程开始了..." + Thread.currentThread().getName());
            int i = 100 / 5;
            System.out.println("任务1 线程结束了..." + Thread.currentThread().getName());
            return i;
        }, executor);
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2 线程开始了..." + Thread.currentThread().getName());
            int i = 100 /10;
            System.out.println("任务2 线程结束了..." + Thread.currentThread().getName());
            return i;
        }, executor);

        // runAfterBothAsync 不能获取前面两个线程的返回结果,本身也没有返回结果
        CompletableFuture<Void> voidCompletableFuture = future1.runAfterBothAsync(future2, () -> {
            System.out.println("任务3执行了");
        },executor);

        // thenAcceptBothAsync 可以获取前面两个线程的返回结果,本身没有返回结果
        CompletableFuture<Void> voidCompletableFuture1 = future1.thenAcceptBothAsync(future2, (f1, f2) -> {
            System.out.println("f1 = " + f1);
            System.out.println("f2 = " + f2);
        }, executor);

        // thenCombineAsync: 既可以获取前面两个线程的返回结果,同时也会返回结果给阻塞的线程
        CompletableFuture<String> stringCompletableFuture = future1.thenCombineAsync(future2, (f1, f2) -> {
            return f1 + ":" + f2;
        }, executor);

        // 可以处理异步任务之后的操作
        System.out.println("获取的线程的返回结果是:" + stringCompletableFuture.get() );
    }

6.两个任务完成一个

  在上面5个基础上我们来看看两个任务只要有一个完成就会触发任务3的情况

  • runAfterEither:不能获取完成的线程的返回结果,自身也没有返回结果
  • acceptEither:可以获取线程的返回结果,自身没有返回结果
  • applyToEither:既可以获取线程的返回结果,自身也有返回结果
/**
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1 线程开始了..." + Thread.currentThread().getName());
            int i = 100 / 5;
            System.out.println("任务1 线程结束了..." + Thread.currentThread().getName());
            return i;
        }, executor);
        CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2 线程开始了..." + Thread.currentThread().getName());
            int i = 100 /10;
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2 线程结束了..." + Thread.currentThread().getName());
            return i+"";
        }, executor);
        // runAfterEitherAsync 不能获取前面完成的线程的返回结果,自身也没有返回结果
        future1.runAfterEitherAsync(future2,()->{
            System.out.println("任务3执行了....");
        },executor);

        // acceptEitherAsync 可以获取前面完成的线程的返回结果  自身没有返回结果
        future1.acceptEitherAsync(future2,(res)->{
            System.out.println("res = " + res);
        },executor);

        // applyToEitherAsync 既可以获取完成任务的线程的返回结果  自身也有返回结果
        CompletableFuture<String> stringCompletableFuture = future1.applyToEitherAsync(future2, (res) -> {
            System.out.println("res = " + res);
            return res + "-->OK";
        }, executor);
        // 可以处理异步任务之后的操作
        System.out.println("获取的线程的返回结果是:" + stringCompletableFuture.get() );
    }

7.多任务组合

allOf:等待所有任务完成

anyOf:只要有一个任务完成

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs);

public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs);
/**
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1 线程开始了..." + Thread.currentThread().getName());
            int i = 100 / 5;
            System.out.println("任务1 线程结束了..." + Thread.currentThread().getName());
            return i;
        }, executor);
        CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2 线程开始了..." + Thread.currentThread().getName());
            int i = 100 /10;
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2 线程结束了..." + Thread.currentThread().getName());
            return i+"";
        }, executor);

        CompletableFuture<Object> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务3 线程开始了..." + Thread.currentThread().getName());
            int i = 100 /10;
            System.out.println("任务3 线程结束了..." + Thread.currentThread().getName());
            return i+"";
        }, executor);

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1, future2, future3);
        anyOf.get();
        System.out.println("主任务执行完成..." + anyOf.get());

        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2, future3);
        allOf.get();// 阻塞在这个位置,等待所有的任务执行完成
        System.out.println("主任务执行完成..." + future1.get() + " :" + future2.get() + " :" + future3.get());
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/494572.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

故障:更新后电脑卡顿

前一天下班的时候关电脑&#xff0c;关机选项变成了“更新并关机”&#xff0c;没多想&#xff0c;我点了。。。。早上上班就发现电脑卡得不行&#xff0c;看了下更新日志&#xff0c;装了一大堆东西&#xff0c;看了下任务管理器&#xff0c;内存直接跑到了90%&#xff0c;这电…

每日学术速递5.2

CV - 计算机视觉 | ML - 机器学习 | RL - 强化学习 | NLP 自然语言处理 Subjects: cs.CV 1.DataComp: In search of the next generation of multimodal datasets 标题&#xff1a;DataComp&#xff1a;寻找下一代多模态数据集 作者&#xff1a;Samir Yitzhak Gadre, Gab…

【JAVA模块六------ 综合案例基础巩固】

JAVA模块六------ 综合案例基础巩固 1 逢7跳过2 数组元素求和3 判断两个数组的内容是否相同4 查找某个数组元素索引5 数组元素反转输出&#xff1a;&#xff08;逆序输出&#xff09;6 评委打分7 随机产生验证码其他&#xff1a;方法抽取&#xff1a; 1 逢7跳过 要求&#xff1…

国产光伏仪器 6581太阳能电池板伏安特性测试仪

6581太阳能电池板伏安特性测试仪主要用于太阳能电池板生产的最终测试&#xff0c;也可以作为层压前测试使用&#xff0c;能大大提高一次封装成品率。该测试仪适合于单晶、多晶、薄膜等多种电池组件&#xff0c;可进行I-V曲线、P-V曲线、短路电流、开路电压、峰值功率等全部参数…

Hive SQL on Flink 构建流批一体引擎

摘要&#xff1a;本文整理自阿里巴巴开发工程师罗宇侠、阿里巴巴开发工程师方盛凯&#xff0c;在 Flink Forward Asia 2022 流批一体专场的分享。本篇内容主要分为五个部分&#xff1a; 1. 构建流批一体引擎的挑战 2. Hive SQL on Flink 3. 流批一体引擎的收益 4. Demo 5. 未来…

做到“有效沟通”,帮你达到这3个目的

在项目管理中&#xff0c;团队沟通是至关重要的。团队成员之间应该建立良好的沟通机制&#xff0c;及时沟通和协调问题&#xff0c;避免出现问题后甩锅的情况。 在实际项目中&#xff0c;很多问题出现的原因是团队沟通不畅&#xff0c;项目经理需要加强团队沟通的重要性&…

K8S二进制安装报错及各个组件功能介绍

目录 一、K8S安装二、安装时遇到的几个问题2.1、Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "kuberne…

C语言复习笔记2

1.变量命名只能以数字、字母、下划线组成并且不能以数字开头。 #include<stdio.h> #include<unistd.h>//变量名只能由数字字母下划线组成&#xff0c;不能以数字开头 int main() {//int 2b;return 0; }2.内存中保存的是补码 0的补码取反得补码再求源码是-1。 源码…

提升论文影响力的方法

论文发表后&#xff0c;还有一些重要的工作去做&#xff0c;那就是去积极宣传和推广自己的论文&#xff0c;提高自己论文的影响力。这类似于一个电影上映后&#xff0c;主演们还得去做宣传一样&#xff0c;要想办法推销自己的作品。本文将介绍提升论文影响力的方法。 1. 开源数…

xray简单使用指南

前言收到需求如下 用户还需要一个报告 询问了群里的小伙伴推荐使用xray进行扫描 一、下载 https://github.com/chaitin/xray/releases windows下载amd64即可 解压后进入其目录下执行exe程序&#xff0c;帮我们生成一些yaml文件 生成 ca 证书 .\xray_windows_amd64.exe g…

创客匠人:五月,爱成长的力量,有爱必赢

2023年5月4日&#xff0c;创客匠人第六十九届铁军训练营在厦门举办&#xff0c;由创客匠人CEO蒋洪波、CSO张潇峰及HRM何巧婷为厦门总部的伙伴们带来精彩的回顾总结及主题分享。 一、四月总结 为了明确前进奋斗的方向&#xff0c;进一步提升团队战斗力&#xff0c;从而更好地帮助…

Altium Designer中如何在顶层中添加对应端口

转载说明&#xff1a; 大众深度科普 https://jingyan.baidu.com/article/c33e3f4889f327ea15cbb584.html 版权归原作者所有&#xff1b;感谢原作者的分享&#xff1b; 转载到此&#xff0c;主要为了后期查看方便&#xff1b; 本经验简要介绍Altium Designer中如何在顶层中添…

SpringBoot【开发实用篇】---- 热部署

SpringBoot【开发实用篇】---- 热部署 1. 手动启动热部署2. 自动启动热部署3. 参与热部署监控的文件范围配置4. 关闭热部署 什么是热部署&#xff1f;简单说就是你程序改了&#xff0c;现在要重新启动服务器&#xff0c;嫌麻烦&#xff1f;不用重启&#xff0c;服务器会自己悄悄…

人类创新发展的四个阶段:三个核心和一个扩展

纵观人类的发展史&#xff0c;始终伴随着人类的创新过程&#xff0c;这也是人类与其他生物体的最大的区别&#xff0c;别的生物体也就是可以使用工具或者模仿别的生物的动作来制造简单的工具&#xff0c;对工具进行简单的拼接&#xff0c;只有人类是可以进行真正的创造出这个自…

记录-Symbol学习笔记

这里给大家分享我在网上总结出来的一些知识&#xff0c;希望对大家有所帮助 Symbol是JavaScript中的原始数据类型之一&#xff0c;它表示一个唯一的、不可变的值&#xff0c;通常用作对象属性的键值。由于Symbol值是唯一的&#xff0c;因此可以防止对象属性被意外地覆盖或修改。…

Word下划线怎么打?速速get这5个实用方法!

案例&#xff1a;Word下划线怎么打&#xff1f; 【朋友们&#xff0c;最近在写毕业论文&#xff0c;封面文字的下划线打了好久都打不出来&#xff0c;请问大家Word下划线是怎么打的呀&#xff1f;】 在Microsoft Word中&#xff0c;打下划线是一种常见的操作&#xff0c;它可…

Python小姿势 - ## Python与数据库

Python与数据库 简介 当今&#xff0c;数据库是计算机应用中最重要的部分。几乎所有的大型应用都要用到数据库&#xff0c;比如银行、电商、航空、政府、医疗、教育、科研等。数据库的目的是存储数据&#xff0c;并且能够根据用户的需求提供数据。 数据库管理系统&#xff08;D…

QQ音乐银河音效技术实践——音乐重放效果的补偿与修饰

音效渲染是音频或音乐播放器最为重要的后处理模块之一。LiveVideoStackCon 2022 北京站邀请到腾讯音乐银河音效开发负责人——闫震海&#xff0c;为大家介绍银河音效在QQ音乐播放器中的创新应用&#xff0c;包括空间环绕效果和音效制作工具等内容。 文/闫震海 编辑/LiveVideoSt…

取代你的可能不是AI,而是比你更会使用AI的人

1、背景 从开始了解AI到现在已经1个月了&#xff0c;最明显的就是&#xff0c;产品层出不穷&#xff0c;以前只有技术人员才关系AI&#xff0c;现在各行各业都在关系AI&#xff0c;都希望通过它提高生产力和创造力&#xff1b; 在当今大数据和人工智能时代&#xff0c;职场和企…

MySQL基础(一)数据库概述

1. 为什么要使用数据库 持久化(persistence)&#xff1a;把数据保存到可掉电式存储设备中以供之后使用。大多数情况下&#xff0c;特别是企业级应用&#xff0c;数据持久化意味着将内存中的数据保存到硬盘上加以”固化”&#xff0c;而持久化的实现过程大多通过各种关系数据库…