栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多线程CompletableFuture用法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多线程CompletableFuture用法



import java.util.concurrent.*;

public class ThreadTest {


    private static ExecutorService service = Executors.newFixedThreadPool(10);


    public static void main(String[] args) throws Exception {

//        runAsync();
//        supplyAsync();
//        whenComplete();
//        thenApply();
//        handle();
//        test1();
//        test2();
        test3();
        System.out.println("主线程执行完成。。。。。");

    }

    
    public static void runAsync() throws ExecutionException, InterruptedException {
        CompletableFuture voidCompletableFuture = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000L);
                int a = 10 / 2;
                System.out.println("结果为:" + a);
            } catch (Exception e) {

            }
            System.out.println("run end ..." + Thread.currentThread().getId());
        }, service);

//        voidCompletableFuture.get();  等待完成再返回
    }


    
    public static CompletableFuture supplyAsync() throws Exception {
        CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
            }
            System.out.println("run end ...");
            return System.currentTimeMillis();
        },service);
        return future;
    }

    
    public static void whenComplete() throws ExecutionException, InterruptedException {
        CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("run start ...");
                TimeUnit.SECONDS.sleep(1);
                Long i = 1000L / 0;
            } catch (InterruptedException e) {
            }
            return 1L;
        },service).whenCompleteAsync((res,exception)->{
            System.out.println("结果完成"+res);
            System.out.println("出现异常:"+exception);
        }).exceptionally(t->{
            System.out.println(t.getMessage());
            return 10L;
        });
        System.out.println("run end ..." +future.get());
    }

    private static void thenApply() throws Exception {
        CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("run start ...");
                TimeUnit.SECONDS.sleep(1);
                Long i = 1000L / 3;
            } catch (InterruptedException e) {
            }
            return 1L;
        }, service).thenApply(res -> res * 2).thenApply(res->res * 2);
        System.out.println(future.get());
    }



    private static void handle() throws Exception {
        CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("run start ...");
                TimeUnit.SECONDS.sleep(1);
                Long i = 1000L / 0;
                return 1L;
            } catch (InterruptedException e) {
                return 2L;
            }catch(ArithmeticException e){
                return 3L;
            }
        }, service).handle((t,u)->{
            System.out.println(u.getMessage());
            return t * 2;
        });
        System.out.println(future.get());
    }


    
    private static void test1() throws Exception {
        CompletableFuture f1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1开始");
            System.out.println("任务1结束");
            return 1;
        }, service);

        CompletableFuture f2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2开始");
            System.out.println("任务2结束");
            return 2;
        }, service);

        f1.thenAcceptBothAsync(f2, (a, b) -> {
            System.out.println("执行完成t"+a+":"+b);
        }, service);

    }


    
    public static void test2() throws Exception{
        CompletableFuture f1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1开始");
            System.out.println("任务1结束");
            return 1;
        }, service);

        CompletableFuture f2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2开始");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
            }
            System.out.println("任务2结束");
            return 2;
        }, service);

        f1.acceptEitherAsync(f2,(a)->{
            System.out.println("执行完成:"+a);
        },service);
    }


    
    public static void test3() throws Exception{
        CompletableFuture f1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1开始");
            System.out.println("任务1结束");
            return 1;
        }, service);

        CompletableFuture f2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2开始");
            System.out.println("任务2结束");
            return 2;
        }, service);


        CompletableFuture f3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务3开始");
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {

            }
            System.out.println("任务3结束");
            return 3;
        }, service);


        
//        CompletableFuture all = CompletableFuture.allOf(f1, f2, f3);
//        all.get();


        
        CompletableFuture anyOf = CompletableFuture.anyOf(f1, f2, f3);

        System.out.println("执行完毕:"+anyOf.get());


    }



}
 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号