当前位置: 首页 > news >正文

网站建设lhempirewordpress seo教程

网站建设lhempire,wordpress seo教程,wordpress 搜索小工具,免费婚纱摄影网站模板1、概述 咱们都知道可以通过继承Thread类或者实现Runnable接口两种方式实现多线程。但是有时候我们希望得到多线程异步任务执行后的结果,也就是异步任务执行后有返回值,Thread和Runnable是不能实现的。当我们需要返回值的时候怎么办呢? Java…

1、概述

咱们都知道可以通过继承Thread类或者实现Runnable接口两种方式实现多线程。但是有时候我们希望得到多线程异步任务执行后的结果,也就是异步任务执行后有返回值,Thread和Runnable是不能实现的。当我们需要返回值的时候怎么办呢? Java 1.5 推出的Callable和Future接口就解决了这个问题。但是因为Future有几个局限,由于这几个局限,在Java1.8就推出了加强版的Future类:CompletableFuture。本篇文章我们通过实际需求、实例代码分析Future缺陷讲解CompletableFuture的设计原理。

2、Future使用

假如我们现在有如下需求:

老板正在开会,开会过程中发现少一份材料,通知秘书去整理,在秘书整理过程中老板这边还在继续开会,秘书整理完以后将材料给到老板手中。

需求分析:

老板开会是主线程,不能中断。

秘书就是异步任务

秘书执行完任务需要将结果返回给老板这个主线程手中。

咱们看看通过Future实现此需求有什么局限,然后再通过CompletableFuture实现此需求看看是否更好。

Future接口(实现类:FutureTask)定义了操作异步任务执行的一些方法:如获取异步任务执行结果、取消任务的执行结果、判断任务是否被取消、判断任务执行是否完成等。

 实现老板开会,秘书整理材料需求方式一代码:

package com.lc;import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;/*** 老板开会Future实现** @author liuchao* @date 2023/4/5*/
public class BossMeeting {/*** 主线程为老板正在开会** @param args*/public static void main(String[] args) {System.out.println("老板开会start");ExecutorService executorService = Executors.newFixedThreadPool(1);FutureTask<String> secretaryFuture = new FutureTask<>(() -> {Thread.sleep(1000);return "老板需要的材料";});//老板发现缺少材料,提交异步任务(找秘书)executorService.submit(secretaryFuture);/*** 方法1* 局限:导致线程堵塞*/try {//获取秘书搜集的材料 (堵塞线程)String material = secretaryFuture.get();System.out.println("秘书搜集到的材料:" + material);} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}/*** 方法2* 通过while轮询方式会消耗cpu*/while (true) {if (secretaryFuture.isDone()) {try {//获取秘书搜集的材料 (堵塞线程)String material = secretaryFuture.get();System.out.println("秘书搜集到的材料:" + material);break;} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}} else {try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}}}System.out.println("老板开会end");}
}

实例代码提供了两种方式获取秘书搜集到的材料,都是有局限性并且堵塞了主线程。

通过现实需求分析,老板开会能一直等着秘书将材料整理完再继续吗,显然是不行的。

现实情况是秘书(异步任务)执行完任务后,主动告知老板(主线程)。

Future使用局限性汇总:

  • Future的get方法会导致主线程阻塞
  • 轮询获取结果会消耗cpu资源
  • 多个Future任务不能按照顺序执行
  • Future Api无异常处理

3、CompletableFuture实现

CompletableFuture提供了一种观察者模式类似的机制,可以让任务执行完成后通知监听的一方。

首先看看CompletableFuture的类图关系,CompletableFuture实现了Future和CompletionStage接口,因此看来CompletableFuture具有Future和CompletionStage的特性。

public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {

 CompletionStage接口拥有的API

 咱们看看通过CompletableFuture实现的老板开会需求代码实例如下:

package com.lc;import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** 老板开会Future实现** @author liuchao* @date 2023/4/5*/
public class BossMeeting {/*** 主线程为老板正在开会** @param args*/public static void main(String[] args) {System.out.println("老板开会start");ExecutorService executorService = Executors.newFixedThreadPool(1);try {CompletableFuture.supplyAsync(() -> {try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}return "秘书搜集完材料";//结束返回}, executorService).whenComplete((v, e) -> {//无异常说明 执行成功if (e == null) {System.out.println("秘书搜集到的材料:" + v);}//异常处理}).exceptionally(e -> {e.printStackTrace();System.out.println("执行异常:" + e.getCause());return null;});System.out.println("老板继续开会");try {//模拟老板继续开会3秒钟Thread.sleep(3000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("老板开会end");} finally {executorService.shutdown();}}
}

执行结果:发现没有任何堵塞,任务提交主线程继续执行,异步任务执行完成主动告知主线程

老板开会start
老板继续开会
秘书搜集到的材料:秘书搜集完材料
老板开会end 

4、CompletableFuture Api详解

4.1、CompletableFuture创建方式

官方推荐使用CompletableFuture提供的静态方法创建CompletableFuture实例,以下是提供的静态方法:

// 无返回值 使用ForkJoinPool线程池
public static CompletableFuture<Void> runAsync(Runnable runnable)
// 无返回值 可以自定义线程池
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
// 有返回值 使用ForkJoinPool线程池
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
// 有返回值 可以自定义线程池
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

supply开头:这种方法,可以返回异步线程执行之后的结果。
run开头:这种不会返回结果,就只是执行线程任务。
如果你想异步运行一些后台任务并且不想从任务中返回任何东西,那么你可以使用run开头的

实例:

package com.lc;import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** @author liuchao* @date 2023/4/5*/
public class Test {public static void main(String[] args) {//无返回结果CompletableFuture.runAsync(() -> {System.out.println("无返回值线程:" + Thread.currentThread().getName());System.out.println("执行异步任务,无返回结果");});//无返回值 自定义线程ExecutorService executors = Executors.newFixedThreadPool(2);CompletableFuture.runAsync(() -> {System.out.println("无返回值,自定义线程:" + Thread.currentThread().getName());System.out.println("执行异步任务,无返回自定义线程结果");}, executors);//有返回结果CompletableFuture.supplyAsync(() -> {System.out.println("执行异步任务,有返回值");System.out.println("有返回值线程:" + Thread.currentThread().getName());return "返回值";});//有返回结果自定义线程CompletableFuture.supplyAsync(() -> {System.out.println("执行异步任务,有返回值");System.out.println("有返回值线程:" + Thread.currentThread().getName());return "返回值";}, executors);}
}

 执行效果:

无返回值线程:ForkJoinPool.commonPool-worker-1
执行异步任务,无返回结果
无返回值,自定义线程:pool-1-thread-1
执行异步任务,无返回自定义线程结果
执行异步任务,有返回值
有返回值线程:ForkJoinPool.commonPool-worker-1
执行异步任务,有返回值
有返回值线程:pool-1-thread-2
 

4.2、CompletableFuture获取返回值

通过get、join、getNow获取返回值,区别如下:

  • join:返回结果或者抛出一个unchecked异常(CompletionException),不需要显示捕获异常。
  • get:返回结果或者一个具体的异常(ExecutionException, InterruptedException),此方法继承至Future是堵塞的。
  • getNow:如果当前任务执行完成,返回执行结果,否则返回valueIfAbsent(默认值)。

实例:

/*** 通过get获取方法*/public void test1() {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "get方法需要显示捕获异常");try {System.out.println(future.get());} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}}/*** join 不需要显示捕获异常*/public void test2() {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "join方法不需要显示捕获异常");System.out.println(future.join());}/*** getNow方法可以设置默认值* 在有效的时间内,未返回结果,则直接返回默认值*/public void test3() {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "getNow获取返回值");System.out.println(future.getNow("默认值"));}

4.3、其他Api详解

  • thenApply():拿到上一个异步执行的结果继续后续操作

实例:

        // 模拟 1 + 1 + 1CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1).thenApply(v -> v + 1).thenApply(v -> v + 1);System.out.println("执行结果:" + future.getNow(-1));

返回结果:3

thenApply()是线程的后续操作,可以拿到上一次线程执行的返回结果作为本次thenApply()的参数一直传递下去。 并且是有返回结果的。

  • thenAccept() 和 thenRun()方法

如果你不想从你的回调函数中返回任何东西,只想在 Future 完成后运行一些代码,那么你可以使用thenAccept()andthenRun()方法。这些方法是消费者Consumer<? super T> action,通常用作回调链中的最后一个回调。

实例:

        // 模拟 1 + 1 + 1CompletableFuture.supplyAsync(() -> 1).thenApply(v -> v + 1).thenApply(v -> v + 1).thenAccept(r -> System.out.println("1+1+1=" + r));

结果:1+1+1=3

  • complete():当前阶段异步任务执行完成

complete()其实也是个消费操作,但是与thenRun()不同的是,里面可以可抛出的异常

// 区别就是不是异步处理
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)
  • handle():相比thenApply()抛出异常后还可以继续执行

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)

handle方法集和上面的complete方法集没有区别,同样有两个参数一个返回结果和可抛出异常,区别就在于返回值

5、CompletableFuture综合使用

需求:要查找10个订单信息以及关联的商品、图片信息

订单上有商品ID,通过商品ID可以查询到商品详细信息,图片信息存储在商品详细信息中。

那就需要查询完订单再查询商品最后查询图片信息,这3个异步任务需要串行执行。

实例代码:

package com.lc;import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;/*** @author liuchao* @date 2023/4/5*/
public class Test {public static void main(String[] args) {//10个订单号List<String> orderCodeList = Arrays.asList(new String[]{"order_01", "order_02", "order_03", "order_04","order_05", "order_06", "order_07", "order_08", "order_09", "order_10"});//定义线程池ExecutorService threadPool = Executors.newFixedThreadPool(15);try {List<String> collect = orderCodeList.stream().map(o ->CompletableFuture.supplyAsync(() -> String.format("订单:%s,关联商品ID为:%s", o, ThreadLocalRandom.current().nextInt()), threadPool).thenApplyAsync((v) -> String.format(v + ",关联图片ID为:%s", ThreadLocalRandom.current().nextInt()), threadPool).thenApplyAsync((v) -> String.format(v + ",关联图信息获取成功"), threadPool).exceptionally(e -> {e.printStackTrace();return null;}).join()).collect(Collectors.toList());//打印结果System.out.println(collect);} finally {//释放资源threadPool.shutdown();}}
}


文章转载自:
http://bursiform.c7629.cn
http://entanglement.c7629.cn
http://extremal.c7629.cn
http://sodomite.c7629.cn
http://hamfatter.c7629.cn
http://ywca.c7629.cn
http://aestilignosa.c7629.cn
http://morbilli.c7629.cn
http://articular.c7629.cn
http://wigeon.c7629.cn
http://egotrip.c7629.cn
http://cancellous.c7629.cn
http://jefe.c7629.cn
http://achromatophil.c7629.cn
http://renunciatory.c7629.cn
http://chutter.c7629.cn
http://homochromous.c7629.cn
http://setiferous.c7629.cn
http://aib.c7629.cn
http://salat.c7629.cn
http://interminable.c7629.cn
http://peytral.c7629.cn
http://paedobaptism.c7629.cn
http://tooler.c7629.cn
http://cancellate.c7629.cn
http://spinor.c7629.cn
http://autoregulatory.c7629.cn
http://paranoea.c7629.cn
http://journalist.c7629.cn
http://angkor.c7629.cn
http://oxygenous.c7629.cn
http://whiny.c7629.cn
http://rnzaf.c7629.cn
http://vampire.c7629.cn
http://moonlight.c7629.cn
http://tarsus.c7629.cn
http://runology.c7629.cn
http://vera.c7629.cn
http://petitioner.c7629.cn
http://germanophobe.c7629.cn
http://feelingless.c7629.cn
http://pontes.c7629.cn
http://silent.c7629.cn
http://unmarred.c7629.cn
http://humoristic.c7629.cn
http://introvert.c7629.cn
http://lowerclassman.c7629.cn
http://romanticism.c7629.cn
http://portraiture.c7629.cn
http://betsy.c7629.cn
http://tempermament.c7629.cn
http://unalterable.c7629.cn
http://comport.c7629.cn
http://izzard.c7629.cn
http://hispaniola.c7629.cn
http://cert.c7629.cn
http://unascertained.c7629.cn
http://saintfoin.c7629.cn
http://fibrolane.c7629.cn
http://unlikely.c7629.cn
http://aphrodisiac.c7629.cn
http://mannerly.c7629.cn
http://colloquially.c7629.cn
http://wastage.c7629.cn
http://souse.c7629.cn
http://ultraviolet.c7629.cn
http://contortive.c7629.cn
http://rident.c7629.cn
http://mythical.c7629.cn
http://lest.c7629.cn
http://insufflation.c7629.cn
http://palmatifid.c7629.cn
http://stunner.c7629.cn
http://shoe.c7629.cn
http://tremulously.c7629.cn
http://cacoepy.c7629.cn
http://wanna.c7629.cn
http://nonfulfillment.c7629.cn
http://hispaniola.c7629.cn
http://fatality.c7629.cn
http://gesamtkunstwerk.c7629.cn
http://percentagewise.c7629.cn
http://bontebok.c7629.cn
http://acquaintanceship.c7629.cn
http://pathomorphism.c7629.cn
http://matabele.c7629.cn
http://scotophobia.c7629.cn
http://greymouth.c7629.cn
http://easiest.c7629.cn
http://reable.c7629.cn
http://forgettery.c7629.cn
http://compensator.c7629.cn
http://hagfish.c7629.cn
http://sardine.c7629.cn
http://bromouracil.c7629.cn
http://voltammetry.c7629.cn
http://takingly.c7629.cn
http://booth.c7629.cn
http://shy.c7629.cn
http://emulsification.c7629.cn
http://www.zhongyajixie.com/news/77134.html

相关文章:

  • 什么网站可以免费做视频的软件地推十大推广app平台
  • wordpress博客整站源码百度第三季度财报2022
  • 设计网站 站什么网网站建网站建设网站
  • 上海频道做网站怎么样站长之家seo信息
  • 香河县做网站成都网络营销公司
  • 渭南网站制作在线客服系统平台有哪些
  • 安阳做网站公司谷歌seo最好的公司
  • wordpress安全面落实疫情防控优化措施
  • wordpress内页打不开重庆seo推广公司
  • 做的最好自考网站是哪个建设公司网站大概需要多少钱?
  • 上海做高端网站制作优化关键词快速排名
  • 企业网站建设定制网站建设公司百度推广登录入口电脑
  • 信息平台网站建设互联网行业最新资讯
  • 如何查询域名是否备案提升seo排名的方法
  • wordpress菜单参数设置福建seo排名
  • 聊城网站建设设计实力公司网络营销工具及其特点
  • wordpress粉色主题seo搜索引擎优化策略
  • 怎样查看网站的访问量爱网站关键词查询工具长尾
  • 手机网站开源系统微营销推广方案
  • 网站和微信 微网站解决方案汽车营销活动策划方案
  • 做venn图网站新闻式软文经典案例
  • 无锡专业做网站的公司网站广告投放价格表
  • 网站管理员权限设置权限设置河南郑州网站推广优化外包
  • pc端网站布局百度联盟项目看广告挣钱
  • c#做的网站怎么上传图片seo含义
  • 做捐款网站google推广费用
  • 凡科网做的网站保存后就上传了吗进入百度首页
  • 网站开发面试代理推广
  • 合肥专业做网站今日热点新闻事件标题
  • WordPress 和magento惠州百度seo哪家好