찾다
Javajava지도 시간Java8 CompletableFuture는 비동기 다중 스레드 프로그래밍을 어떻게 구현합니까?

1. Future의 예시 검토

일부 비즈니스 시나리오에서는 작업 실행 속도를 높이기 위해 다중 스레드를 사용하여 작업을 비동기적으로 실행해야 합니다.

JDK5는 비동기 계산 결과를 설명하는 데 사용되는 새로운 Future 인터페이스를 추가합니다.

Future 및 관련 사용 방법은 작업을 비동기적으로 실행할 수 있는 기능을 제공하지만 결과를 얻는 것은 매우 불편합니다. Future.get()을 사용하여 호출 스레드를 차단하거나 폴링을 사용하여 작업이 Future.isDone인지 확인해야 합니다. 끝났으니 결과를 받아보세요.

두 가지 처리 방법 모두 그다지 우아하지 않습니다.

    @Test
    public void testFuture() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Future<String> future = executorService.submit(() -> {
            Thread.sleep(2000);
            return "hello";
        });
        System.out.println(future.get());
        System.out.println("end");
    }

동시에 Future는 여러 비동기 작업이 서로 의존해야 하는 시나리오를 해결할 수 없습니다. 스레드는 하위 스레드 작업이 완료될 때까지 기다려야 합니다. 이때 "CountDownLatch"를 생각할 수 있습니다. 예, 코드는 다음과 같습니다.

여기서 두 개의 Future가 정의됩니다. 첫 번째는 사용자 ID를 통해 사용자 정보를 얻고, 두 번째는 제품 ID를 통해 제품 정보를 얻습니다.

    @Test
    public void testCountDownLatch() throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        CountDownLatch downLatch = new CountDownLatch(2);
        long startTime = System.currentTimeMillis();
        Future<String> userFuture = executorService.submit(() -> {
            //模拟查询商品耗时500毫秒
            Thread.sleep(500);
            downLatch.countDown();
            return "用户A";
        });
 
        Future<String> goodsFuture = executorService.submit(() -> {
            //模拟查询商品耗时500毫秒
            Thread.sleep(400);
            downLatch.countDown();
            return "商品A";
        });
 
        downLatch.await();
        //模拟主程序耗时时间
        Thread.sleep(600);
        System.out.println("获取用户信息:" + userFuture.get());
        System.out.println("获取商品信息:" + goodsFuture.get());
        System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
 
    }

「실행 결과」

사용자 정보 가져오기: 사용자 A
상품 정보 가져오기: 제품 A
총 시간은 1110ms

실행 결과를 보면 결과가 나온 것을 알 수 있고, 비동기 작업을 사용하지 않으면 실행 시간은 500+400+600 = 1500이어야 합니다. 비동기 작업을 사용한 후에는 실제로 1110만 사용됩니다.

하지만 Java8 이후에는 이것이 더 이상 우아한 솔루션이라고 생각하지 않습니다. 다음으로 CompletableFuture의 사용에 대해 알아 보겠습니다.

2. CompletableFuture를 통해 위 예시를 구현

    @Test
    public void testCompletableInfo() throws InterruptedException, ExecutionException {
        long startTime = System.currentTimeMillis();
 
        //调用用户服务获取用户基本信息
        CompletableFuture<String> userFuture = CompletableFuture.supplyAsync(() ->
                //模拟查询商品耗时500毫秒
        {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "用户A";
        });
 
        //调用商品服务获取商品基本信息
        CompletableFuture<String> goodsFuture = CompletableFuture.supplyAsync(() ->
                //模拟查询商品耗时500毫秒
        {
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "商品A";
        });
 
        System.out.println("获取用户信息:" + userFuture.get());
        System.out.println("获取商品信息:" + goodsFuture.get());
 
        //模拟主程序耗时时间
        Thread.sleep(600);
        System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
    }

실행 결과

사용자 정보 가져오기: 사용자 A
제품 정보 가져오기: 제품 A
총 시간은 1112ms

CompletableFuture를 통해 쉽게 CountDownLatch 기능을 구현할 수 있으며, 당신은 이것이 끝이라고 생각합니다. 그보다 훨씬 더 CompletableFuture는 이것보다 훨씬 낫습니다.

예를 들어, 태스크 1이 실행된 후 태스크 2가 실행되거나 태스크 1의 실행 결과도 태스크 2의 입력 매개변수 및 기타 강력한 기능으로 사용될 수 있습니다. CompletableFuture의 API를 배워보겠습니다. .

3. CompletableFuture 생성 방법

3.1.4 일반적으로 사용되는 생성 방법

CompletableFuture 소스 코드에는 비동기 작업을 수행하는 4가지 정적 메서드가 있습니다.

public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier){..}
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor){..}
public static CompletableFuture<Void> runAsync(Runnable runnable){..}
public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor){..}

일반적으로 우리는 CompletableFuture를 생성하기 위해 위의 정적 메서드를 사용합니다. 차이점은 다음과 같습니다.

  • "supplyAsync" 작업을 실행하고 반환 값을 지원합니다.

  • 「runAsync」는 작업을 실행하며 반환 값이 없습니다.

3.1.1, "supplyAsync method"

//使用默认内置线程池ForkJoinPool.commonPool(),根据supplier构建执行任务
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
//自定义线程,根据supplier构建执行任务
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

3.1.2, "runAsync method"

//使用默认内置线程池ForkJoinPool.commonPool(),根据runnable构建执行任务
public static CompletableFuture<Void> runAsync(Runnable runnable) 
//自定义线程,根据runnable构建执行任务
public static CompletableFuture<Void> runAsync(Runnable runnable,  Executor executor)

3.2, 결과를 얻는 4가지 방법

CompltableFuture 클래스는 결과를 얻는 4가지 방법을 제공합니다

//方式一
public T get()
//方式二
public T get(long timeout, TimeUnit unit)
//方式三
public T getNow(T valueIfAbsent)
//方式四
public T join()

설명:

  • 「get() 및 get(long timeout, TimeUnit 단위)」 => 미래에는 이미 제공됩니다. 후자는 지정된 시간 내에 결과를 얻지 못하면 Timeout 예외가 발생합니다.

  • 「getNow」 => 결과 계산이 완료되면 결과를 반환하거나, 계산이 완료되지 않은 경우에는 예외를 반환합니다. valueIfAbsent 값이 반환됩니다

  • "join" => 메서드에서 예외가 발생하지 않습니다

: 示例

    @Test
    public void testCompletableGet() throws InterruptedException, ExecutionException {
 
        CompletableFuture<String> cp1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "商品A";
        });
 
        // getNow方法测试 
        System.out.println(cp1.getNow("商品B"));
 
        //join方法测试 
        CompletableFuture<Integer> cp2 = CompletableFuture.supplyAsync((() -> 1 / 0));
        System.out.println(cp2.join());
        System.out.println("-----------------------------------------------------");
        //get方法测试
        CompletableFuture<Integer> cp3 = CompletableFuture.supplyAsync((() -> 1 / 0));
        System.out.println(cp3.get());
    }

「运行结果」:

  • 第一个执行结果为 「商品B」,因为要先睡上1秒结果不能立即获取

  • join方法获取结果方法里不会抛异常,但是执行结果会抛异常,抛出的异常为CompletionException

  • get方法获取结果方法里将抛出异常,执行结果抛出的异常为ExecutionException

4、异步回调方法

Java8 CompletableFuture는 비동기 다중 스레드 프로그래밍을 어떻게 구현합니까?

4.1、thenRun/thenRunAsync

通俗点讲就是,「做完第一个任务后,再做第二个任务,第二个任务也没有返回值」

示例

    @Test
    public void testCompletableThenRunAsync() throws InterruptedException, ExecutionException {
        long startTime = System.currentTimeMillis();
 
        CompletableFuture<Void> cp1 = CompletableFuture.runAsync(() -> {
            try {
                //执行任务A
                Thread.sleep(600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
 
        });
 
        CompletableFuture<Void> cp2 = cp1.thenRun(() -> {
            try {
                //执行任务B
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        // get方法测试
        System.out.println(cp2.get());
 
        //模拟主程序耗时时间
        Thread.sleep(600);
        System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
    }
 
    //运行结果
    /**
     *  null
     *  总共用时1610ms
     */

「thenRun 和thenRunAsync有什么区别呢?」

如果你执行第一个任务的时候,传入了一个自定义线程池:

  • 调用thenRun方法执行第二个任务时,则第二个任务和第一个任务是共用同一个线程池。

  • 调用thenRunAsync执行第二个任务时,则第一个任务使用的是你自己传入的线程池,第二个任务使用的是ForkJoin线程池。

说明: 后面介绍的thenAccept和thenAcceptAsync,thenApply和thenApplyAsync等,它们之间的区别也是这个。

4.2、thenAccept/thenAcceptAsync

第一个任务执行完成后,执行第二个回调方法任务,会将该任务的执行结果,作为入参,传递到回调方法中,但是回调方法是没有返回值的。

示例

    @Test
    public void testCompletableThenAccept() throws ExecutionException, InterruptedException {
        long startTime = System.currentTimeMillis();
        CompletableFuture<String> cp1 = CompletableFuture.supplyAsync(() -> {
            return "dev";
 
        });
        CompletableFuture<Void> cp2 = cp1.thenAccept((a) -> {
            System.out.println("上一个任务的返回结果为: " + a);
        });
 
        cp2.get();
    }

"실행 결과": 🎜🎜🎜🎜 첫 번째 실행 결과는 🎜"제품 B"🎜입니다. 먼저 1초 동안 대기해야 하고 결과를 즉시 얻을 수 없기 때문입니다🎜🎜🎜🎜 조인 방법에서는 예외가 발생하지 않지만 실행 결과에서는 예외가 발생합니다. 던져진 예외는 CompletionException🎜🎜🎜🎜get 메소드가 결과를 가져오며, 실행 결과로 던져진 예외는 ExecutionException🎜🎜🎜🎜4입니다. ://img.php.cn/upload/article/000/887/227/ 168255492938956.png" alt="Java8 CompletableFuture 비동기 멀티스레딩 구현 방법" />🎜🎜4.1, thenRun/thenRunAsync🎜🎜일반인의 용어로 , 🎜"첫 번째 작업을 완료한 후 두 번째 작업을 수행하고, 두 번째 작업마다 반환 값이 없습니다"🎜. 🎜🎜🎜예🎜🎜
    @Test
    public void testCompletableThenApply() throws ExecutionException, InterruptedException {
        CompletableFuture<String> cp1 = CompletableFuture.supplyAsync(() -> {
            return "dev";
 
        }).thenApply((a) -> {
            if (Objects.equals(a, "dev")) {
                return "dev";
            }
            return "prod";
        });
 
        System.out.println("当前环境为:" + cp1.get());
 
        //输出: 当前环境为:dev
    }
🎜🎜"thenRun과 thenRunAsync의 차이점은 무엇입니까?"🎜🎜🎜첫 번째 작업을 실행할 때 사용자 정의 스레드 풀을 전달하는 경우: 🎜🎜🎜🎜thenRun 메서드를 호출하여 두 번째 작업을 실행합니다. 두 개의 작업이 있는 경우 두 번째 작업과 첫 번째 작업은 동일한 스레드 풀을 공유합니다. 🎜🎜🎜🎜 thenRunAsync를 호출하여 두 번째 작업을 실행하면 첫 번째 작업은 전달한 스레드 풀을 사용하고 두 번째 작업은 ForkJoin 스레드 풀을 사용합니다. 🎜🎜🎜🎜설명: thenAccept와 thenAcceptAsync, 나중에 소개하는 thenApply와 thenApplyAsync의 차이점도 이렇습니다. 🎜🎜4.2, thenAccept/thenAcceptAsync🎜🎜첫 번째 작업 실행이 완료된 후 두 번째 콜백 메서드 작업이 실행됩니다. 🎜작업 실행 결과는 입력 매개변수🎜로 콜백 메서드에 전달되지만 콜백은 메소드가 반환 값이 아닙니다. 🎜🎜🎜
    @Test
    public void testCompletableThenAccept() throws ExecutionException, InterruptedException {
        long startTime = System.currentTimeMillis();
        CompletableFuture<String> cp1 = CompletableFuture.supplyAsync(() -> {
            return "dev";
 
        });
        CompletableFuture<Void> cp2 = cp1.thenAccept((a) -> {
            System.out.println("上一个任务的返回结果为: " + a);
        });
 
        cp2.get();
    }

4.3、 thenApply/thenApplyAsync

表示第一个任务执行完成后,执行第二个回调方法任务,会将该任务的执行结果,作为入参,传递到回调方法中,并且回调方法是有返回值的。

示例

    @Test
    public void testCompletableThenApply() throws ExecutionException, InterruptedException {
        CompletableFuture<String> cp1 = CompletableFuture.supplyAsync(() -> {
            return "dev";
 
        }).thenApply((a) -> {
            if (Objects.equals(a, "dev")) {
                return "dev";
            }
            return "prod";
        });
 
        System.out.println("当前环境为:" + cp1.get());
 
        //输出: 当前环境为:dev
    }

5、异常回调

当CompletableFuture的任务不论是正常完成还是出现异常它都会调用「whenComplete」这回调函数。

  • 「正常完成」:whenComplete返回结果和上级任务一致,异常为null;

  • 「出现异常」:whenComplete返回结果为null,异常为上级任务的异常;

即调用get()时,正常完成时就获取到结果,出现异常时就会抛出异常,需要你处理该异常。

下面来看看示例

5.1、只用whenComplete

    @Test
    public void testCompletableWhenComplete() throws ExecutionException, InterruptedException {
        CompletableFuture<Double> future = CompletableFuture.supplyAsync(() -> {
 
            if (Math.random() < 0.5) {
                throw new RuntimeException("出错了");
            }
            System.out.println("正常结束");
            return 0.11;
 
        }).whenComplete((aDouble, throwable) -> {
            if (aDouble == null) {
                System.out.println("whenComplete aDouble is null");
            } else {
                System.out.println("whenComplete aDouble is " + aDouble);
            }
            if (throwable == null) {
                System.out.println("whenComplete throwable is null");
            } else {
                System.out.println("whenComplete throwable is " + throwable.getMessage());
            }
        });
        System.out.println("最终返回的结果 = " + future.get());
    }

正常完成,没有异常时:

正常结束
whenComplete aDouble is 0.11
whenComplete throwable is null
最终返回的结果 = 0.11

出现异常时:get()会抛出异常

whenComplete aDouble is null
whenComplete throwable is java.lang.RuntimeException: 出错了
 
java.util.concurrent.ExecutionException: java.lang.RuntimeException: 出错了
 at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
 at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)

5.2、whenComplete + exceptionally示例

    @Test
    public void testWhenCompleteExceptionally() throws ExecutionException, InterruptedException {
        CompletableFuture<Double> future = CompletableFuture.supplyAsync(() -> {
            if (Math.random() < 0.5) {
                throw new RuntimeException("出错了");
            }
            System.out.println("正常结束");
            return 0.11;
 
        }).whenComplete((aDouble, throwable) -> {
            if (aDouble == null) {
                System.out.println("whenComplete aDouble is null");
            } else {
                System.out.println("whenComplete aDouble is " + aDouble);
            }
            if (throwable == null) {
                System.out.println("whenComplete throwable is null");
            } else {
                System.out.println("whenComplete throwable is " + throwable.getMessage());
            }
        }).exceptionally((throwable) -> {
            System.out.println("exceptionally中异常:" + throwable.getMessage());
            return 0.0;
        });
 
        System.out.println("最终返回的结果 = " + future.get());
    }

当出现异常时,exceptionally中会捕获该异常,给出默认返回值0.0。

whenComplete aDouble is null
whenComplete throwable is java.lang.RuntimeException: 出错了
exceptionally中异常:java.lang.RuntimeException: 出错了
最终返回的结果 = 0.0

6、多任务组合回调

Java8 CompletableFuture는 비동기 다중 스레드 프로그래밍을 어떻게 구현합니까?

6.1、AND组合关系

thenCombine / thenAcceptBoth / runAfterBoth都表示:「当任务一和任务二都完成再执行任务三」

区别在于:

  • 「runAfterBoth」 不会把执行结果当做方法入参,且没有返回值

  • 「thenAcceptBoth」: 会将两个任务的执行结果作为方法入参,传递到指定方法中,且无返回值

  • 「thenCombine」:会将两个任务的执行结果作为方法入参,传递到指定方法中,且有返回值

示例

    @Test
    public void testCompletableThenCombine() throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //开启异步任务1
        CompletableFuture<Integer> task = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务1,当前线程是:" + Thread.currentThread().getId());
            int result = 1 + 1;
            System.out.println("异步任务1结束");
            return result;
        }, executorService);
 
        //开启异步任务2
        CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务2,当前线程是:" + Thread.currentThread().getId());
            int result = 1 + 1;
            System.out.println("异步任务2结束");
            return result;
        }, executorService);
 
        //任务组合
        CompletableFuture<Integer> task3 = task.thenCombineAsync(task2, (f1, f2) -> {
            System.out.println("执行任务3,当前线程是:" + Thread.currentThread().getId());
            System.out.println("任务1返回值:" + f1);
            System.out.println("任务2返回值:" + f2);
            return f1 + f2;
        }, executorService);
 
        Integer res = task3.get();
        System.out.println("最终结果:" + res);
    }

「运行结果」

异步任务1,当前线程是:17
异步任务1结束
异步任务2,当前线程是:18
异步任务2结束
执行任务3,当前线程是:19
任务1返回值:2
任务2返回值:2
最终结果:4

6.2、OR组合关系

applyToEither / acceptEither / runAfterEither 都表示:「两个任务,只要有一个任务完成,就执行任务三」

区别在于:

  • 「runAfterEither」:不会把执行结果当做方法入参,且没有返回值

  • 「acceptEither」: 会将已经执行完成的任务,作为方法入参,传递到指定方法中,且无返回值

  • 「applyToEither」:会将已经执行完成的任务,作为方法入参,传递到指定方法中,且有返回值

示例

    @Test
    public void testCompletableEitherAsync() {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //开启异步任务1
        CompletableFuture<Integer> task = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务1,当前线程是:" + Thread.currentThread().getId());
 
            int result = 1 + 1;
            System.out.println("异步任务1结束");
            return result;
        }, executorService);
 
        //开启异步任务2
        CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务2,当前线程是:" + Thread.currentThread().getId());
            int result = 1 + 2;
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("异步任务2结束");
            return result;
        }, executorService);
 
        //任务组合
        task.acceptEitherAsync(task2, (res) -> {
            System.out.println("执行任务3,当前线程是:" + Thread.currentThread().getId());
            System.out.println("上一个任务的结果为:" + res);
        }, executorService);
    }

运行结果

//通过结果可以看出,异步任务2都没有执行结束,任务3获取的也是1的执行结果
异步任务1,当前线程是:17
异步任务1结束
异步任务2,当前线程是:18
执行任务3,当前线程是:19
上一个任务的结果为:2

注意

如果把上面的核心线程数改为1也就是

 ExecutorService executorService = Executors.newFixedThreadPool(1);

运行结果就是下面的了,会发现根本没有执行任务3,显然是任务3直接被丢弃了。

异步任务1,当前线程是:17
异步任务1结束
异步任务2,当前线程是:17

6.3、多任务组合

  • 「allOf」:等待所有任务完成

  • 「anyOf」:只要有一个任务完成

示例

allOf:等待所有任务完成

    @Test
    public void testCompletableAallOf() throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //开启异步任务1
        CompletableFuture<Integer> task = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务1,当前线程是:" + Thread.currentThread().getId());
            int result = 1 + 1;
            System.out.println("异步任务1结束");
            return result;
        }, executorService);
 
        //开启异步任务2
        CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务2,当前线程是:" + Thread.currentThread().getId());
            int result = 1 + 2;
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("异步任务2结束");
            return result;
        }, executorService);
 
        //开启异步任务3
        CompletableFuture<Integer> task3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务3,当前线程是:" + Thread.currentThread().getId());
            int result = 1 + 3;
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("异步任务3结束");
            return result;
        }, executorService);
 
        //任务组合
        CompletableFuture<Void> allOf = CompletableFuture.allOf(task, task2, task3);
 
        //等待所有任务完成
        allOf.get();
        //获取任务的返回结果
        System.out.println("task结果为:" + task.get());
        System.out.println("task2结果为:" + task2.get());
        System.out.println("task3结果为:" + task3.get());
    }

anyOf: 只要有一个任务完成

    @Test
    public void testCompletableAnyOf() throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //开启异步任务1
        CompletableFuture<Integer> task = CompletableFuture.supplyAsync(() -> {
            int result = 1 + 1;
            return result;
        }, executorService);
 
        //开启异步任务2
        CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {
            int result = 1 + 2;
            return result;
        }, executorService);
 
        //开启异步任务3
        CompletableFuture<Integer> task3 = CompletableFuture.supplyAsync(() -> {
            int result = 1 + 3;
            return result;
        }, executorService);
 
        //任务组合
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(task, task2, task3);
        //只要有一个有任务完成
        Object o = anyOf.get();
        System.out.println("完成的任务的结果:" + o);
    }

7、CompletableFuture使用有哪些注意点

Java8 CompletableFuture는 비동기 다중 스레드 프로그래밍을 어떻게 구현합니까?

 CompletableFuture 使我们的异步编程更加便利的、代码更加优雅的同时,我们也要关注下它,使用的一些注意点。

7.1、Future需要获取返回值,才能获取异常信息

    @Test
    public void testWhenCompleteExceptionally() {
        CompletableFuture<Double> future = CompletableFuture.supplyAsync(() -> {
            if (1 == 1) {
                throw new RuntimeException("出错了");
            }
            return 0.11;
        });
 
        //如果不加 get()方法这一行,看不到异常信息
        //future.get();
    }

Future需要获取返回值,才能获取到异常信息。如果不加 get()/join()方法,看不到异常信息。

小伙伴们使用的时候,注意一下哈,考虑是否加try...catch...或者使用exceptionally方法。

7.2、CompletableFuture的get()方法是阻塞的

CompletableFuture的get()方法是阻塞的,如果使用它来获取异步调用的返回值,需要添加超时时间。

//反例
 CompletableFuture.get();
//正例
CompletableFuture.get(5, TimeUnit.SECONDS);

7.3、不建议使用默认线程池

CompletableFuture代码中又使用了默认的「ForkJoin线程池」,处理的线程个数是电脑「CPU核数-1」。在大量请求过来的时候,处理逻辑复杂的话,响应会很慢。一般建议使用自定义线程池,优化线程池配置参数。

7.4、自定义线程池时,注意饱和策略

CompletableFuture的get()方法是阻塞的,我们一般建议使用future.get(5, TimeUnit.SECONDS)。并且一般建议使用自定义线程池。

但是如果线程池拒绝策略是DiscardPolicy或者DiscardOldestPolicy,当线程池饱和时,会直接丢弃任务,不会抛弃异常。因此建议,CompletableFuture线程池策略最好使用AbortPolicy,然后耗时的异步线程,做好线程池隔离哈。

위 내용은 Java8 CompletableFuture는 비동기 다중 스레드 프로그래밍을 어떻게 구현합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
이 기사는 亿速云에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제
JVM은 운영 체제 API의 차이를 어떻게 처리합니까?JVM은 운영 체제 API의 차이를 어떻게 처리합니까?Apr 27, 2025 am 12:18 AM

JVM은 JNI (JavanativeInterface) 및 Java 표준 라이브러리를 통한 운영 체제 API 차이를 처리합니다. 1. JNI는 Java 코드가 로컬 코드를 호출하고 운영 체제 API와 직접 상호 작용할 수 있습니다. 2. Java Standard Library는 통합 API를 제공하며,이 API는 내부적으로 다른 운영 체제 API에 매핑되어 코드가 플랫폼에서 실행되도록합니다.

Java 9에 도입 된 모듈성은 플랫폼 독립성에 어떤 영향을 미칩니 까?Java 9에 도입 된 모듈성은 플랫폼 독립성에 어떤 영향을 미칩니 까?Apr 27, 2025 am 12:15 AM

modularityDoesNotDirectHeftJava'splatformincendence.java'splatformincendenceIngeasted whejvm, butModularItyInfluencesApplicationStructureAndmanagement, deploymentandDuffictionBecomeMoreferficaliticiboliticalWI

바이트 코드 란 무엇이며 Java의 플랫폼 독립성과 어떤 관련이 있습니까?바이트 코드 란 무엇이며 Java의 플랫폼 독립성과 어떤 관련이 있습니까?Apr 27, 2025 am 12:06 AM

bytecodeinjavaistheintermediaterepresentation attenablesplatformincendence.1) javacodeiscompiledintobytecodestoredin.2) thejvminterpretsorcompilesthisbytecodeintomachinecodeartruntime, theCodeTorUnanynanynovice를 허용합니다

Java가 플랫폼 독립 언어로 간주되는 이유는 무엇입니까?Java가 플랫폼 독립 언어로 간주되는 이유는 무엇입니까?Apr 27, 2025 am 12:03 AM

javaachievesplatformincendenceThoughthegroughthejavavirtualMachine (JVM), theexecutesByTecodeonAnyDeviceWitHajvm.1) javacodeiscompiledintobytecode.2) thejvm-mmectretsandexecutesThisteCodeIntomachineCificinStructions, 허용

그래픽 사용자 인터페이스 (GUI)는 Java의 플랫폼 독립성에 대한 과제를 어떻게 제시 할 수 있습니까?그래픽 사용자 인터페이스 (GUI)는 Java의 플랫폼 독립성에 대한 과제를 어떻게 제시 할 수 있습니까?Apr 27, 2025 am 12:02 AM

Javagui 개발의 플랫폼 독립성은 과제에 직면하지만 Swing, Javafx, Unifying Locance, Performance Optimization, 타사 라이브러리 및 크로스 플랫폼 테스트를 사용하여 처리 할 수 ​​있습니다. Javagui Development는 교차 플랫폼 일관성을 제공하는 것을 목표로하는 AWT 및 Swing에 의존하지만 실제 효과는 운영 체제마다 다릅니다. 솔루션은 다음과 같습니다. 1) Swing 및 Javafx 사용 GUI 툴킷으로 사용; 2) UIMANAGER.SETLOOKANDFEEL ()을 통해 외관을 통합합니다. 3) 다른 플랫폼에 맞게 성능을 최적화합니다. 4) apachepivot 또는 SWT와 같은 타사 라이브러리 사용; 5) 일관성을 보장하기 위해 크로스 플랫폼 테스트를 수행하십시오.

Java 개발의 어떤 측면이 플랫폼 의존적입니까?Java 개발의 어떤 측면이 플랫폼 의존적입니까?Apr 26, 2025 am 12:19 AM

javadevelopmentisnotentirelyplatform-IndectionentDuetoSeveralFactors.1) JVMVARIATIONSAFFERFFERFORMANDBEHAVIORACROSSDIFFERENTOS.2) nativelibrariesViajniintrictionPlatform-specificiss.3) filepathsandsystempropertiesdifferbetweenplatectry. 4)

다른 플랫폼에서 Java 코드를 실행할 때 성능 차이가 있습니까? 왜?다른 플랫폼에서 Java 코드를 실행할 때 성능 차이가 있습니까? 왜?Apr 26, 2025 am 12:15 AM

Java 코드는 다른 플랫폼에서 실행할 때 성능 차이가 있습니다. 1) JVM의 구현 및 최적화 전략은 OracleJDK 및 OpenJDK와 같이 다릅니다. 2) 메모리 관리 및 스레드 스케줄링과 같은 운영 체제의 특성도 성능에 영향을 미칩니다. 3) 적절한 JVM을 선택하여 JVM 매개 변수 및 코드 최적화를 조정하여 성능을 향상시킬 수 있습니다.

Java의 플랫폼 독립성의 몇 가지 한계는 무엇입니까?Java의 플랫폼 독립성의 몇 가지 한계는 무엇입니까?Apr 26, 2025 am 12:10 AM

Java'SplatformIndenceHASLIMITATIONSINTERFORMANTOWORHEAD, 버전 컴포팅 가능성, 도전 과제, 플랫폼-특이 적 식품, 및 JVMINSTALLATION/MAYMENDENT.ThesefacteThe "WriteOnce, Runanywhere"

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구