0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

异步神器CompletableFuture万字详解!

jf_ro2CN3Fa 来源:CSDN 作者:CSDN 2022-11-15 10:15 次阅读


CompletableFuture是jdk8的新特性。CompletableFuture实现了CompletionStage接口和Future接口,前者是对后者的一个扩展,增加了异步会点、流式处理、多个Future组合处理的能力,使Java在处理多任务的协同工作时更加顺畅便利。

一、创建异步任务

1. supplyAsync

supplyAsync是创建带有返回值的异步任务。它有如下两个方法,一个是使用默认线程池(ForkJoinPool.commonPool())的方法,一个是带有自定义线程池的重载方法

//带返回值异步请求,默认线程池
publicstaticCompletableFuturesupplyAsync(Suppliersupplier)

//带返回值的异步请求,可以自定义线程池
publicstaticCompletableFuturesupplyAsync(Suppliersupplier,Executorexecutor)

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf=CompletableFuture.supplyAsync(()->{
System.out.println("dosomething....");
return"result";
});

//等待任务执行完成
System.out.println("结果->"+cf.get());
}


publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
//自定义线程池
ExecutorServiceexecutorService=Executors.newSingleThreadExecutor();
CompletableFuturecf=CompletableFuture.supplyAsync(()->{
System.out.println("dosomething....");
return"result";
},executorService);

//等待子任务执行完成
System.out.println("结果->"+cf.get());
}

测试结果:

90bc4a4e-648a-11ed-8abf-dac502259ad0.png

2. runAsync

runAsync是创建没有返回值的异步任务。它有如下两个方法,一个是使用默认线程池(ForkJoinPool.commonPool())的方法,一个是带有自定义线程池的重载方法

//不带返回值的异步请求,默认线程池
publicstaticCompletableFuturerunAsync(Runnablerunnable)

//不带返回值的异步请求,可以自定义线程池
publicstaticCompletableFuturerunAsync(Runnablerunnable,Executorexecutor)

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf=CompletableFuture.runAsync(()->{
System.out.println("dosomething....");
});

//等待任务执行完成
System.out.println("结果->"+cf.get());
}


publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
//自定义线程池
ExecutorServiceexecutorService=Executors.newSingleThreadExecutor();
CompletableFuturecf=CompletableFuture.runAsync(()->{
System.out.println("dosomething....");
},executorService);

//等待任务执行完成
System.out.println("结果->"+cf.get());
}

测试结果:

90e0da4e-648a-11ed-8abf-dac502259ad0.png

3.获取任务结果的方法

//如果完成则返回结果,否则就抛出具体的异常
publicTget()throwsInterruptedException,ExecutionException

//最大时间等待返回结果,否则就抛出具体异常
publicTget(longtimeout,TimeUnitunit)throwsInterruptedException,ExecutionException,TimeoutException

//完成时返回结果值,否则抛出unchecked异常。为了更好地符合通用函数形式的使用,如果完成此CompletableFuture所涉及的计算引发异常,则此方法将引发unchecked异常并将底层异常作为其原因
publicTjoin()

//如果完成则返回结果值(或抛出任何遇到的异常),否则返回给定的valueIfAbsent。
publicTgetNow(TvalueIfAbsent)

//如果任务没有完成,返回的值设置为给定值
publicbooleancomplete(Tvalue)

//如果任务没有完成,就抛出给定异常
publicbooleancompleteExceptionally(Throwableex)

基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://github.com/YunaiV/ruoyi-vue-pro
  • 视频教程:https://doc.iocoder.cn/video/

二、异步回调处理

1. thenApply和thenApplyAsync

thenApply 表示某个任务执行完成后执行的动作,即回调方法,会将该任务的执行结果即方法返回值作为入参传递到回调方法中,带有返回值。

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=cf1.thenApplyAsync((result)->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
result+=2;
returnresult;
});
//等待任务1执行完成
System.out.println("cf1结果->"+cf1.get());
//等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=cf1.thenApply((result)->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
result+=2;
returnresult;
});
//等待任务1执行完成
System.out.println("cf1结果->"+cf1.get());
//等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}

测试结果:

9114db50-648a-11ed-8abf-dac502259ad0.png 914c3bd6-648a-11ed-8abf-dac502259ad0.png

从上面代码和测试结果我们发现thenApply和thenApplyAsync区别在于,使用thenApply方法时子任务与父任务使用的是同一个线程,而thenApplyAsync在子任务中是另起一个线程执行任务,并且thenApplyAsync可以自定义线程池,默认的使用ForkJoinPool.commonPool()线程池。

2. thenAccept和thenAcceptAsync

thenAccep表示某个任务执行完成后执行的动作,即回调方法,会将该任务的执行结果即方法返回值作为入参传递到回调方法中,无返回值。

测试代码

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=cf1.thenAccept((result)->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
});

//等待任务1执行完成
System.out.println("cf1结果->"+cf1.get());
//等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}


publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=cf1.thenAcceptAsync((result)->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
});

//等待任务1执行完成
System.out.println("cf1结果->"+cf1.get());
//等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}

测试结果:

915c4ad0-648a-11ed-8abf-dac502259ad0.png 917f8ce8-648a-11ed-8abf-dac502259ad0.png从上面代码和测试结果我们发现thenAccep和thenAccepAsync区别在于,使用thenAccep方法时子任务与父任务使用的是同一个线程,而thenAccepAsync在子任务中可能是另起一个线程执行任务,并且thenAccepAsync可以自定义线程池,默认的使用ForkJoinPool.commonPool()线程池。

3.thenRun和thenRunAsync

thenRun表示某个任务执行完成后执行的动作,即回调方法,无入参,无返回值。

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=cf1.thenRun(()->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
});

//等待任务1执行完成
System.out.println("cf1结果->"+cf1.get());
//等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=cf1.thenRunAsync(()->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
});

//等待任务1执行完成
System.out.println("cf1结果->"+cf1.get());
//等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}

测试结果:

919bd614-648a-11ed-8abf-dac502259ad0.png91aa1882-648a-11ed-8abf-dac502259ad0.png

从上面代码和测试结果我们发现thenRun和thenRunAsync区别在于,使用thenRun方法时子任务与父任务使用的是同一个线程,而thenRunAsync在子任务中可能是另起一个线程执行任务,并且thenRunAsync可以自定义线程池,默认的使用ForkJoinPool.commonPool()线程池。

4.whenComplete和whenCompleteAsync

whenComplete是当某个任务执行完成后执行的回调方法,会将执行结果或者执行期间抛出的异常传递给回调方法,如果是正常执行则异常为null,回调方法对应的CompletableFuture的result和该任务一致,如果该任务正常执行,则get方法返回执行结果,如果是执行异常,则get方法抛出异常。

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
inta=1/0;
return1;
});

CompletableFuturecf2=cf1.whenComplete((result,e)->{
System.out.println("上个任务结果:"+result);
System.out.println("上个任务抛出异常:"+e);
System.out.println(Thread.currentThread()+"cf2dosomething....");
});

////等待任务1执行完成
//System.out.println("cf1结果->"+cf1.get());
////等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}

测试结果:

91b85c44-648a-11ed-8abf-dac502259ad0.png

whenCompleteAsync和whenComplete区别也是whenCompleteAsync可能会另起一个线程执行任务,并且thenRunAsync可以自定义线程池,默认的使用ForkJoinPool.commonPool()线程池。

5.handle和handleAsync

跟whenComplete基本一致,区别在于handle的回调方法有返回值。

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
//inta=1/0;
return1;
});

CompletableFuturecf2=cf1.handle((result,e)->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
System.out.println("上个任务结果:"+result);
System.out.println("上个任务抛出异常:"+e);
returnresult+2;
});

//等待任务2执行完成
System.out.println("cf2结果->"+cf2.get());
}

测试结果 :

91dba032-648a-11ed-8abf-dac502259ad0.png

基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://github.com/YunaiV/yudao-cloud
  • 视频教程:https://doc.iocoder.cn/video/

三、多任务组合处理

1. thenCombine、thenAcceptBoth 和runAfterBoth

这三个方法都是将两个CompletableFuture组合起来处理,只有两个任务都正常完成时,才进行下阶段任务。

区别:thenCombine会将两个任务的执行结果作为所提供函数的参数,且该方法有返回值;thenAcceptBoth同样将两个任务的执行结果作为方法入参,但是无返回值;runAfterBoth没有入参,也没有返回值。注意两个任务中只要有一个执行异常,则将该异常信息作为指定任务的执行结果。

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
return2;
});

CompletableFuturecf3=cf1.thenCombine(cf2,(a,b)->{
System.out.println(Thread.currentThread()+"cf3dosomething....");
returna+b;
});

System.out.println("cf3结果->"+cf3.get());
}

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
return2;
});

CompletableFuturecf3=cf1.thenAcceptBoth(cf2,(a,b)->{
System.out.println(Thread.currentThread()+"cf3dosomething....");
System.out.println(a+b);
});

System.out.println("cf3结果->"+cf3.get());
}

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf1dosomething....");
return1;
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+"cf2dosomething....");
return2;
});

CompletableFuturecf3=cf1.runAfterBoth(cf2,()->{
System.out.println(Thread.currentThread()+"cf3dosomething....");
});

System.out.println("cf3结果->"+cf3.get());
}

测试结果:

91f51922-648a-11ed-8abf-dac502259ad0.png9211a330-648a-11ed-8abf-dac502259ad0.png

92274e9c-648a-11ed-8abf-dac502259ad0.png 2.applyToEither、acceptEither和runAfterEither

这三个方法和上面一样也是将两个CompletableFuture组合起来处理,当有一个任务正常完成时,就会进行下阶段任务。

区别:applyToEither会将已经完成任务的执行结果作为所提供函数的参数,且该方法有返回值;acceptEither同样将已经完成任务的执行结果作为方法入参,但是无返回值;runAfterEither没有入参,也没有返回值。

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf1dosomething....");
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
return"cf1任务完成";
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf2dosomething....");
Thread.sleep(5000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
return"cf2任务完成";
});

CompletableFuturecf3=cf1.applyToEither(cf2,(result)->{
System.out.println("接收到"+result);
System.out.println(Thread.currentThread()+"cf3dosomething....");
return"cf3任务完成";
});

System.out.println("cf3结果->"+cf3.get());
}


publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf1dosomething....");
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
return"cf1任务完成";
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf2dosomething....");
Thread.sleep(5000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
return"cf2任务完成";
});

CompletableFuturecf3=cf1.acceptEither(cf2,(result)->{
System.out.println("接收到"+result);
System.out.println(Thread.currentThread()+"cf3dosomething....");
});

System.out.println("cf3结果->"+cf3.get());
}

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf1dosomething....");
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf1任务完成");
return"cf1任务完成";
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf2dosomething....");
Thread.sleep(5000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf2任务完成");
return"cf2任务完成";
});

CompletableFuturecf3=cf1.runAfterEither(cf2,()->{
System.out.println(Thread.currentThread()+"cf3dosomething....");
System.out.println("cf3任务完成");
});

System.out.println("cf3结果->"+cf3.get());
}

测试结果:

92418438-648a-11ed-8abf-dac502259ad0.png925d7cc4-648a-11ed-8abf-dac502259ad0.png

从上面可以看出cf1任务完成需要2秒,cf2任务完成需要5秒,使用applyToEither组合两个任务时,只要有其中一个任务完成时,就会执行cf3任务,显然cf1任务先完成了并且将自己任务的结果传值给了cf3任务,cf3任务中打印了接收到cf1任务完成,接着完成自己的任务,并返回cf3任务完成;acceptEither和runAfterEither类似,acceptEither会将cf1任务的结果作为cf3任务的入参,但cf3任务完成时并无返回值;runAfterEither不会将cf1任务的结果作为cf3任务的入参,它是没有任务入参,执行完自己的任务后也并无返回值。

2. allOf / anyOf

allOf:CompletableFuture是多个任务都执行完成后才会执行,只有有一个任务执行异常,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回null。

anyOf :CompletableFuture是多个任务只要有一个任务执行完成,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回执行完成任务的结果。

测试代码:

publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf1dosomething....");
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf1任务完成");
return"cf1任务完成";
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf2dosomething....");
inta=1/0;
Thread.sleep(5000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf2任务完成");
return"cf2任务完成";
});

CompletableFuturecf3=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf2dosomething....");
Thread.sleep(3000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf3任务完成");
return"cf3任务完成";
});

CompletableFuturecfAll=CompletableFuture.allOf(cf1,cf2,cf3);
System.out.println("cfAll结果->"+cfAll.get());
}


publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
CompletableFuturecf1=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf1dosomething....");
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf1任务完成");
return"cf1任务完成";
});

CompletableFuturecf2=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf2dosomething....");
Thread.sleep(5000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf2任务完成");
return"cf2任务完成";
});

CompletableFuturecf3=CompletableFuture.supplyAsync(()->{
try{
System.out.println(Thread.currentThread()+"cf2dosomething....");
Thread.sleep(3000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("cf3任务完成");
return"cf3任务完成";
});

CompletableFuturecfAll=CompletableFuture.anyOf(cf1,cf2,cf3);
System.out.println("cfAll结果->"+cfAll.get());
}

		

测试结果:

9278b9a8-648a-11ed-8abf-dac502259ad0.png928fa848-648a-11ed-8abf-dac502259ad0.png

审核编辑 :李倩


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 接口
    +关注

    关注

    33

    文章

    8241

    浏览量

    149907
  • 线程
    +关注

    关注

    0

    文章

    501

    浏览量

    19576

原文标题:一网打尽:异步神器 CompletableFuture 万字详解!

文章出处:【微信号:芋道源码,微信公众号:芋道源码】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    第三篇-V1.5 TB6612电机pwm控制STM32智能小车 STM32F103C8T6单片机

    通过合理的硬件设计和详细的视频笔记介绍,硬件使用STM32F103主控资料多方便学习,通过3万字笔记、12多个小时视频、20多章节代码手把手教会你如何开发和调试。
    的头像 发表于 08-12 18:29 992次阅读
    第三篇-V1.5 TB6612电机pwm控制STM32智能小车 STM32F103C8T6单片机

    Java CompletableFuture 异步超时实现探索

    简介 JDK 8 中 CompletableFuture 没有超时中断任务的能力。现有做法强依赖任务自身的超时实现。本文提出一种异步超时实现方案,解决上述问题。 前言 JDK 8 是一次重大的版本
    的头像 发表于 07-25 14:06 150次阅读

    1.3万字详解半导体先进封装行业,现状及发展趋势!

    共赏好剧   导 读   在以人工智能、高性能计算为代表的新需求驱动下,先进封装应运而生,发展趋势是小型化、高集成度,历经直插型封装、表面贴装、面积阵列封装、2.5D/3D封装和异构集成四个发展阶段。 典型封装技术包括:1)倒片封装(Flip-Chip):芯片倒置,舍弃金属引线,利用凸块连接;2)扇入型/扇出型封装(Fan-In/Fan-Out):在晶圆上进行整体封装,成本更低,关键工艺为重新布线(RDL);3)2.5D/3D封装:2.5D封装中芯片位于硅中介层上,3D封装舍
    的头像 发表于 07-03 08:44 862次阅读
    1.3<b class='flag-5'>万字</b>!<b class='flag-5'>详解</b>半导体先进封装行业,现状及发展趋势!

    万字长文浅谈系统稳定性建设

    流程:需求阶段,研发阶段,测试阶段,上线阶段,运维阶段;整个流程中的所有参与人员:产品,研发,测试,运维人员都应关注系统的稳定性。业务的发展及系统建设过程中,稳定性就是那个1,其他的是1后面的0,没有稳定性,就好比将
    的头像 发表于 07-02 10:31 263次阅读
    <b class='flag-5'>万字</b>长文浅谈系统稳定性建设

    MiniMax推出“海螺AI”,支持超长文本处理

    近日,大模型公司MiniMax宣布,其全新产品“海螺AI”已正式上架。这款强大的AI工具支持高达200ktokens的上下文长度,能够在1秒内处理近3万字的文本。
    的头像 发表于 05-17 09:30 557次阅读

    AI初创企业推MoE混合专家模型架构新品abab 6.5

    losoev 6.5s:与 losoev 6.5 共享相同的训练技术和数据,但效率更高,同样支持 200k tokens 的上下文长度,且能够在 1 秒钟内处理近 3 万字的文本。
    的头像 发表于 04-17 15:06 403次阅读

    鸿蒙OS开发实例:【ArkTS类库异步并发async/await】

    async/await是一种用于处理异步操作的Promise语法糖,使得编写异步代码变得更加简单和易读。通过使用async关键声明一个函数为异步函数,并使用await关键
    的头像 发表于 04-02 20:57 695次阅读
    鸿蒙OS开发实例:【ArkTS类库<b class='flag-5'>异步</b>并发async/await】

    阿里通义千问重磅升级,免费开放1000万字长文档处理功能

    近日,阿里巴巴旗下的人工智能应用通义千问迎来重磅升级,宣布向所有人免费开放1000万字的长文档处理功能,这一创新举措使得通义千问成为全球文档处理容量第一的AI应用。
    的头像 发表于 03-26 11:09 620次阅读

    鸿蒙原生应用开发-ArkTS语言基础类库异步并发简述async/await

    async/await是一种用于处理异步操作的Promise语法糖,使得编写异步代码变得更加简单和易读。通过使用async关键声明一个函数为异步函数,并使用await关键
    发表于 03-06 14:44

    马斯克状告OpenAI,OpenAI回应马斯克诉讼

    马斯克在长达46页、1.4万字的诉讼文件中,控诉OpenAI背离了其初衷——即致力于开发开源人工通用智能(AGI)并服务全人类。
    的头像 发表于 03-04 15:33 759次阅读

    如何用AI聊天机器人写出万字长文

    如何用AI聊天机器人写出万字长文
    的头像 发表于 12-26 16:25 944次阅读

    1万字 20张图带你详解EVPN

    EVPN参考了BGP/MPLS IP VPN的机制,通过扩展BGP协议新定义了几种BGP EVPN路由,通过在网络中发布EVPN路由来实现VTEP的自动发现、主机地址学习等行为。采用EVPN作为VXLAN的控制平面具有以下优势
    的头像 发表于 10-29 16:49 836次阅读
    1<b class='flag-5'>万字</b> 20张图带你<b class='flag-5'>详解</b>EVPN

    前海思高级工程师披露:华为的秘密(4万字

    重创,原有供应链体系被打碎,寸步难行,生存堪忧。在制裁后的两年中,华为的消费者业务和服务器业务出现较大衰退:手机出货量从2020年的1.9亿部,衰退至2021年的3900部(不含荣耀),且将荣耀业务出售;服务器方面,将X86业务完全剥离,仅留下
    的头像 发表于 10-13 08:40 2100次阅读

    CompletableFuture的静态方法使用

    1 CompletableFuture的静态方法使用 CompleteableFuture的静态方法有如下 之前的文章里面已经讲过suuplyAsync,以及runAsync。我们就直接看其他
    的头像 发表于 10-10 14:07 633次阅读
    <b class='flag-5'>CompletableFuture</b>的静态方法使用

    HarmonyOS如何使用异步并发能力进行开发

    async/await是一种用于处理异步操作的Promise语法糖,使得编写异步代码变得更加简单和易读。通过使用async关键声明一个函数为异步函数,并使用await关键
    发表于 09-22 17:35