java 使用多线程处理业务逻辑和批量处理数据

文章展示了如何利用Java的Guava库和ExecutorService进行线程池的创建,以并发地执行多个业务逻辑。通过CountDownLatch实现线程同步,确保所有任务执行完毕后主线程继续执行。示例中包括了批量处理任务的分批执行和多业务的并行处理。
摘要由CSDN通过智能技术生成

将数据分批可以使用guava里面的工具类,pom中引入

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>23.0</version>
</dependency>
package com.jettech.utils;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池执行多业务逻辑
 */
public class ThreadExecutesBusiness {

    public static void main(String[] args) {
        executesMultiBusiness();
        executesBusiness();
    }

    /**
     * 线程池执行多业务逻辑
     * 如:有四个业务要处理 每个业务使用一个线程执行
     */
    public static void executesMultiBusiness(){
        // 每个线程池执行一个业务逻辑
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        CountDownLatch countDownLatch = new CountDownLatch(4);
        try {
            executorService.execute(()-> {
                try {
                    Thread.sleep(1000L);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("业务一");
            });

            executorService.execute(()->{
                try {
                    Thread.sleep(2000L);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("业务二");
            });
            executorService.execute(()->{
                try {
                    Thread.sleep(3000L);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("业务三");
            });

            executorService.execute(()->{
                try {
                    Thread.sleep(4000L);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("业务四");
            });
            //主线程阻塞,如果线程池之间也是需要前后结果的话,应该也是阻塞的 只有当countDown变为0的时候,主线程才不是阻塞的
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } finally {
            executorService.shutdown();
        }

    }

    /**
     * 线程池执行批量任务
     */
    public static void executesBusiness() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 240; i++) {
            list.add(i);
        }
        // 分批 每批100个
        List<List<Integer>> batchList = Lists.partition(list,100);

        // 创建线程池 线程数是 分批的数
        ExecutorService executorService = Executors.newFixedThreadPool(batchList.size());
        //线程计数器,就是 分批的数
        CountDownLatch countDownLatch = new CountDownLatch(batchList.size());

        batchList.forEach(e->{
            //每个分批用一个线程执行
            executorService.execute(()->{
                e.forEach(num->{
                    // 取到每个数据进行业务执行
                    System.out.println(num);
                });
            });
            countDownLatch.countDown();
        });

        try {
            //主线程阻塞,如果线程池之间也是需要前后结果的话,应该也是阻塞的 只有当countDown变为0的时候,主线程才不是阻塞的
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liangshitian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值