09_Future 升级版CompletableFuture

前言

在这里插入图片描述

Future 的基本知识

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

try {
            FutureTask task = new FutureTask(new myCall());
            Thread t1 =  new Thread(task,"t1");
            t1.start();
            //System.out.println(task.get());
            task.get(1,TimeUnit.SECONDS);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
//            executorService.shutdown();
        }
        System.out.println("main");
class myCall implements Callable<String>{

    @Override
    public String  call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        while (true){
            System.out.println("----");
            Thread.sleep(1000);
            if(Thread.currentThread().isInterrupted()){
                break;
            }
        }
        return "aa";
    }
}

CompletableFuture对Future的改进

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

案例精讲-从电商网站的比价需求谈起

比较简单不赘述了

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

CompletableFuture常用方法

在这里插入图片描述

package com.wfg.futrue;

import java.util.concurrent.*;

/**
 * @author wufagang
 * @description
 * @date 2022年07月02日 19:28
 */
public class CompletableFutureDemo {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("33333");
//            try {
//                TimeUnit.SECONDS.sleep(2);
//                System.out.println(Thread.currentThread().getName());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        },executorService);
//        System.out.println(voidCompletableFuture.get());

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int i = 1/0;
            return "aa";
        },executorService).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("计算完成,返回结果:" + v);

            }
        }).exceptionally(e -> {
            System.out.println("异常信息" + e.getMessage());
            return null;
        });
        //TimeUnit.SECONDS.sleep(2);
        System.out.println("main。。。。。。");
        executorService.shutdown();
    }
}

package com.wfg.futrue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * @author wufagang
 * @description
 * @date 2022年07月02日 19:28
 */
public class CompletableFutureDemo2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
       m7();
    }
    public static void m7() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("A come in");
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("b com in ");
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 30;
        }), (x, y) -> {
            return x + y;
        });
        System.out.println(integerCompletableFuture.get());
    }
    public static void m6() throws ExecutionException, InterruptedException {
        CompletableFuture<String> c1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("A come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "A";
        });
        CompletableFuture<String> c2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("B come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "B";
        });
        CompletableFuture<String> c3 = c1.thenCombine(c2, (e, f) -> {
            return e+f;
        });
        System.out.println(c3.get());

    }
    public static void m5() throws ExecutionException, InterruptedException {
        CompletableFuture<String> c1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("A come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "A";
        });
        CompletableFuture<String> c2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("B come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "B";
        });
        CompletableFuture<String> c3 = c1.applyToEither(c2, f -> {
            return f;
        });
        System.out.println(c3.get());

    }

    public static void m4(){
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        try {
            CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            },executorService).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }).thenRunAsync(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
            System.out.println(c1.get());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }

    }
    public static void m3(){
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        try {
            CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            },executorService).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
            System.out.println(c1.get());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }

    }
    public static void m2(){
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        try {
            CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }).thenRun(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
            System.out.println(c1.get());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
         executorService.shutdown();
        }

    }
    public static void m1() throws Exception{
        ExecutorService executorService = Executors.newFixedThreadPool(3);
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("33333");
//            try {
//                TimeUnit.SECONDS.sleep(2);
//                System.out.println(Thread.currentThread().getName());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        },executorService);
//        System.out.println(voidCompletableFuture.get());

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int i = 1/0;
            return "aa";
        },executorService).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("计算完成,返回结果:" + v);

            }
        }).exceptionally(e -> {
            System.out.println("异常信息" + e.getMessage());
            return null;
        });
        System.out.println();
        //TimeUnit.SECONDS.sleep(2);
        System.out.println("main。。。。。。");
        executorService.shutdown();
        completableFuture.get();
        completableFuture.join();
    }
}


CompetableFuture和线程池说明

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值