Java8创建多线程工具类

Java8创建多线程工具类

使用Java8创建多线程任务池

直接上代码

package com.smz.service.utlis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Date 20230915
 *
 * @author smz
 */
@Slf4j
@Component
public class AssetsSyncUtil {

  /** 每个线程处理数量 */
  private static int INTERVAL = 500;

  /**
   * 多线程批量导入数据
   *
   * @param successDataList 总数据
   * @param batchInsertHandler 批量插入数据逻辑
   * @param <T>
   */
  public <T> void batchInsertAssetsWithMultiThread(
      List<T> successDataList, Function<List<T>, Integer> batchInsertHandler) throws Exception {
    // 每个线程处理数量
    int interval = INTERVAL;
    // 导入数据总数
    int count = successDataList.size();
    // list数据拆分数(线程启动数)
    int splitNum = count / interval + 1;
    if (count % interval == 0) {
      splitNum = count / interval;
    }
    // 创建线程池
    ThreadPoolExecutor threadPoolExecutor =
        new ThreadPoolExecutor(
            splitNum, splitNum, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    // 定义一个任务集合
    List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
    // 拆分successDataList并分配给每个线程
    Stream.iterate(0, n -> n + 1)
        .limit(splitNum)
        .forEach(
            i -> {
              //                单个任务的处理的数据集合
              List<T> collectList =
                  successDataList.stream()
                      .skip(i * interval)
                      .limit(interval)
                      .collect(Collectors.toList());
              //              任务集合中增加任务
              tasks.add(
                  () -> {
                    Thread t = Thread.currentThread();
                    log.info("---->>>>线程名{}", t.getName());
                    return batchInsertHandler.apply(collectList);
                  });
            });
    //    调用所有任务
    threadPoolExecutor.invokeAll(tasks);
    //    关闭线程池
    threadPoolExecutor.shutdown();
  }
}

调用方式


  @Lazy @Resource private AssetsSyncUtil assetsSyncUtil;

  public void test() throws Exception {
    //    // 执行批量插入的逻辑实现
    assetsSyncUtil.batchInsertAssetsWithMultiThread(
        queryShipOwnerInfo(), (t) -> saveTheInitializationData(t));
  }

  public Integer saveData(List<XcbShipownerUser> shipownerUserList) {
        //具体执行逻辑
           shipownerUserList.forEach( ship->{
                  System.out.println("操作内容");
              });
            
  }

结束

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值