吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 795|回复: 0
收起左侧

[Java 转载] CompletableFuture 示例

[复制链接]
horro 发表于 2020-8-22 16:23
package com.sykean.smartsite.pay.MyTest;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;

/**
* @AuThor Administrator
* @ClassName TestCompletableFuture
* @description futrue测试
* @date 2020/6/16 0016 14:42
* @modifier Administrator
* @date 2020-06-16 14:42
* @version V1.0
*/
public class TestCompletableFuture {

    public static void main(String[] args) throws Exception{
//        test1();
        test2();
//        thenCombine();
    }

    /***
     * 把两个任务的结果一块交给 thenCombine 来处理
     */
    private static void thenCombine() throws ExecutionException, InterruptedException {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync( () -> "then" );
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync( () -> "Combine" );
        CompletableFuture<String> data = future1.thenCombine( future2, (s, s2) -> s + " " + s2 );
        System.out.println("thenCombine执行结果:{}" + data.get());
    }

    private static void test2() throws ExecutionException, InterruptedException {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("做第一件事");
            waitSomeTime(1000);
            return "做第一件事";
        });
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("做第二件事");
            waitSomeTime(2000);
            return "做第二件事";
        });
        CompletableFuture futureAll = CompletableFuture.allOf(future1,future2);

        System.out.println("开始阻塞" + System.currentTimeMillis());
        futureAll.join();
        System.out.println("结束阻塞" + System.currentTimeMillis());

        System.out.println(future1.isDone());

        if (futureAll.isDone()){
            System.out.println(future1.get());
            System.out.println(future2.get());
            System.out.println("总事件完成");
        }
    }

    private static void waitSomeTime(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static Supplier<String> getStringSupplier() {
        return String::new;
    }

    private static void test1() throws Exception{
        Random rand = new Random();
        CompletableFuture<Integer> future9 = CompletableFuture.supplyAsync(() -> {
            waitSomeTime(1000 + rand.nextInt(1000));
            return 100;
        });
        CompletableFuture<Integer> future10 = CompletableFuture.supplyAsync(() -> {
            waitSomeTime(1000 + rand.nextInt(1000));
            return 200;
        });
        //两个中任意一个计算完成,那么触发Runnable的执行
        CompletableFuture<String> f = future10.applyToEither(future9, i -> i.toString());
        //两个都计算完成,那么触发Runnable的执行
        CompletableFuture f1 = future10.acceptEither(future9, (e) -> {
            System.out.println(e);
        });
        System.out.println(f.get());
    }
}

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 13:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表