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

【Java】CompletableFuture 的使用示例

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

【Java】CompletableFuture 的使用示例

  •  线程的实现方式

    • java 多线程之 extends Thread

    • java 多线程之 implements Runnable

    • java 多线程之 implements Callable

  • 线程池的使用

    • 线程池简介

    • ThreadPoolExecutor

      • java 线程池之 newScheduledThreadPool

      • java 线程池之 newCachedThreadPool

      • java 线程池之 newFixedThreadPool

      • java 线程池之 newSingleThreadExecutor

    • ForkJoinPool

      • java 线程池之 newWorkStealingPool

    •  CompletableFuture

      •   CompletableFuture 的使用示例

 

import lombok.SneakyThrows;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class CompletableRunnable {

    public void runAsync() {
        // 默认 线程池
        CompletableFuture.runAsync(() -> {
            System.out.println("runAsync 使用示例");
        });

        // 自定义 线程池
        CompletableFuture.runAsync(() -> {
            System.out.println("runAsync 使用示例");
        }, this.getPoolExecutor());
    }

    @SneakyThrows
    public void supplyAsync() {
        // 默认 线程池
        String supplyAsync1 = CompletableFuture.supplyAsync(() -> "runAsync 使用示例")
                .get();
        System.out.println(supplyAsync1);

        // 自定义 线程池
        String supplyAsync2 = CompletableFuture.supplyAsync(() -> "runAsync 使用示例")
                .get();
        System.out.println(supplyAsync2);
    }

    @SneakyThrows
    public void anyOf() {
        // 运行 runAsync1
        CompletableFuture runAsync1 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync1 运行");
        });
        // 运行 runAsync2
        CompletableFuture runAsync2 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync2 运行");
        }, this.getPoolExecutor());

        // 等待任意线程执行完成继续
        Object join = CompletableFuture.anyOf(runAsync1, runAsync2)
                .join();
        System.out.println(join);
    }

    @SneakyThrows
    public void allOf() {
        // 运行 runAsync1
        CompletableFuture runAsync1 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync1 运行");
        });
        // 运行 runAsync2
        CompletableFuture runAsync2 = CompletableFuture.runAsync(() -> {
            System.out.println("runAsync2 运行");
        }, this.getPoolExecutor());

        // 等待全部线程执行完成再继续
        Object join = CompletableFuture.allOf(runAsync1, runAsync2)
                .join();
        System.out.println(join);
    }

    
    private ThreadPoolExecutor getPoolExecutor() {
        return new ThreadPoolExecutor(1, 1, 0
                , TimeUnit.SECONDS, new LinkedBlockingDeque<>(10));
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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