使用Completablefuture和Stream实现获取多个任务,满足要求的任意结果

先上代码

		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("主线程开始 "+LocalDateTime.now().format(formatter));
        //任务函数
        Function<Long,Long> longConsumer = (Long time) -> {
            try {
                //暂停模拟耗时
                TimeUnit.SECONDS.sleep(time);
                //打印日志
                System.out.println(LocalDateTime.now().format(formatter)+" 时长:"+time+" 线程名:"+Thread.currentThread().getName());
                return time;
            } catch (InterruptedException e) {
                e.printStackTrace();
                return time;
            }
        };

        //初始化时间
        Set<Long> collect = IntStream.rangeClosed(1, 3).mapToObj(value -> value+3L).collect(Collectors.toSet());
        collect.add(3L);

        //线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        //创建Completablefuture执行任务
        List<CompletableFuture<Long>> collect1 = collect.stream().map(aLong -> CompletableFuture.supplyAsync(() -> longConsumer.apply(aLong), executorService)).collect(Collectors.toList());
        //join回主线程且判断结果然后只获取最快的一个结果
        Optional<Long> any = collect1.stream().map(CompletableFuture::join).filter(aLong -> aLong==3).findAny();
        System.out.println(any);
        
		获取满足条件的多个结果,不阻塞主线程
        //List<Long> longList = collect1.stream().map(CompletableFuture::join)
        //        //.filter(aLong -> aLong >= 3)
        //        .limit(2).collect(Collectors.toList());
        //System.out.println(longList);
		
		//2022年10月9日 18:05:52 下面代码和stream.limit()实际上是等价的
		//Long result = 0L;
        //for (CompletableFuture<Long> longCompletableFuture : collect1) {
        //    if((result=longCompletableFuture.join())>=4){
        //        break;
        //    }
        //}
        //System.out.println("主线程结果:"+result);

        System.out.println("主线程结束 "+LocalDateTime.now().format(formatter));

执行结果

主线程开始 2021-08-10 10:15:05
2021-08-10 10:15:08 时长:3 线程名:pool-1-thread-1
Optional[3]
主线程结束 2021-08-10 10:15:09
2021-08-10 10:15:10 时长:4 线程名:pool-1-thread-2
2021-08-10 10:15:11 时长:5 线程名:pool-1-thread-3
2021-08-10 10:15:11 时长:6 线程名:pool-1-thread-4

可以看到主线程3秒执行完毕没有因为其他的结果而阻塞,filter可以修改为自定义过滤条件,any可以获取结果
Completablefuture的allof和anyof不建议使用,换成Stream来代替要好得多
allof无法获取结果,stream toList可以拿到结果(前提是同一个返回类型)
anyof只能拿到最快响应的结果,findany可以拿到做最快并且符合要求的结果

不要故作聪明使用parallel,否则会变成

主线程开始 2021-08-10 10:13:55
2021-08-10 10:13:58 时长:3 线程名:pool-1-thread-1
2021-08-10 10:13:59 时长:4 线程名:pool-1-thread-2
2021-08-10 10:14:00 时长:5 线程名:pool-1-thread-3
2021-08-10 10:14:01 时长:6 线程名:pool-1-thread-4
Optional[3]
主线程结束 2021-08-10 10:14:01

这样的结果

最终结论:
用Completablefuture代替parallelStream
Stream处理Completablefuture和结果

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值