并发线程工具类分享

hi,我是阿昌,今天分享一下并发线程工具类分享可以更好的在实际项目中进行使用,废话不多说,上来先分享java类

特点

  1. 支持信号量控制并发数
  2. 支持自定义线程
  3. 简单易用
java 复制代码
@Slf4j
@Setter@Getter
public class ConcurrentExecutor<R> {


    public final ExecutorService executorService;

    /**
     * 信号量
     */
    private int permits;

    /**
     * 默认信号量大小
     */
    public static final int DEFAULT_PERMITS = 4;

    /**
     * 信号量实例
     */
    private final Semaphore semaphore;

    public static final int SEMAPHORE_TIMEOUT_MILL_SEC = 100;
    /**
     * 需要异步执行的任务
     */
    private List<R> syncExecuteResult = new ArrayList<>();

    private List<CompletableFuture<R>> futureList = new ArrayList<>();

    public ConcurrentExecutor(int permits, ExecutorService executorService) {
        this.permits = permits;
        this.semaphore = new Semaphore(permits);//并发数;控制
        this.executorService = executorService;
    }

    public static <R> ConcurrentExecutor<R> newConService(int permits, ExecutorService executorService) {
        return new ConcurrentExecutor<>(permits, executorService);
    }

    public static <R> ConcurrentExecutor<R> newConService(ExecutorService executorService) {
        return new ConcurrentExecutor<>(ConcurrentExecutor.DEFAULT_PERMITS, executorService);
    }

    /**
     * 提交单个任务
     */
    public ConcurrentExecutor<R> submit(Supplier<R> supplier) {
        boolean acquire;
        try {
            acquire = semaphore.tryAcquire(SEMAPHORE_TIMEOUT_MILL_SEC, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            acquire = false;
        }
        if (acquire) {
            CompletableFuture<R> future = CompletableFuture.supplyAsync(supplier, executorService);
            future.whenComplete((t, throwable) -> semaphore.release());
            futureList.add(future);
        } else {
            syncExecuteResult.add(supplier.get());
        }
        return this;
    }

    /**
     * 等待所有任务结束,无超时
     */
    public CompletableFuture<List<R>> invokeAllFuture() {
        return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]))
                .thenApply(e -> futureList.stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList()))
                .whenComplete((rs, throwable) -> {
                    if (rs != null && syncExecuteResult != null && syncExecuteResult.size() > 0) {
                        rs.addAll(syncExecuteResult);
                    }
                });
    }

    public List<R> invokeAll() {
        return invokeAllFuture().join();
    }
}

这个工具类可以使用自定义线程池,并设置信号量各种来限制并发执行的线程数量,同时用invokeAllFuture()来让主线程阻塞,等待所有异步线程执行完毕后再执行;

使用案例:比如在多线程调用ai模型

java 复制代码
//多线程调用ai识别
ConcurrentExecutor<String> executor = new ConcurrentExecutor<>(3, ItemConstant.aiThreadPoolExecutor);
for (List<String> partList : Lists.partition(skuNameList, 5)) {
  executor.submit(() -> {
    String str = AiStrategyFactory.chatCompletions(partList);
    if (StrUtil.isNotBlank(str)) {
      System.out.println("ai调用执行结果:" + str);
    }
    return str;
  });
}
//阻塞主线程,直到所有任务完成
executor.invokeAll();
//获取到所有线程执行结果,进行结果处理
List<String> syncExecuteResult = executor.getSyncExecuteResult();
相关推荐
gugucoding2 分钟前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
心平气和量大福大29 分钟前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
952361 小时前
Spring-cloud配置中心
java·spring·spring cloud·微服务
ydd1001001 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
真上帝的左手3 小时前
10. 软件设计&架构-Spring Security 7 整合CAS SSO 单点登录
java·spring·架构·sso
Zane19943 小时前
单例线程安全、生产者消费者、死锁:并发面试三连问串讲
java·后端
云烟成雨TD4 小时前
Micrometer 系列【42】链路追踪:Span 体系 | 核心 API
java·链路追踪·micrometer
Gorway4 小时前
理解 Spring 依赖注入:从构造器注入到集合与条件 Bean
java·后端
LiLiYuan.4 小时前
【字符串常量池】
java·开发语言·面试
Sylvia33.4 小时前
从轮询到推送:足球数据API架构演进与火星数据技术拆解
java·服务器·网络·python·websocket·架构