CountDownLatch 的实际运用

最近项目中开始存在多线程的场景,例如发送消息,如果采用串行的方式就会非常慢,所以就开始了解多线程的CountDownLatch

以下是源码:

首先是线程池的配置,线程池采用自定义的配置


@Configuration
@EnableAsync
@ConfigurationProperties(prefix = "config")
public class ExecutePoolConfiguration {
    private static Logger logger = LoggerFactory.getLogger(ExecutePoolConfiguration.class);

    @Value("${config.threadpool.core-pool-size}")
    private int corePoolSize;

    @Value("${config.threadpool.max-pool-size}")
    private int maxPoolSize;

    @Value("${config.threadpool.queue-capacity}")
    private int queueCapacity;

    @Value("${config.threadpool.keep-alive-seconds}")
    private int keepAliveSeconds;

    @Bean
    public Executor customServiceExecutor(){
        ThreadPoolTaskExecutor  poolExecutor = new ThreadPoolTaskExecutor();
        //线程核心数目
        poolExecutor.setCorePoolSize(corePoolSize);
        poolExecutor.setAllowCoreThreadTimeOut(true);

        poolExecutor.setKeepAliveSeconds(keepAliveSeconds);
        //最大线程数
        poolExecutor.setMaxPoolSize(maxPoolSize);
        //配置队列大小
        poolExecutor.setQueueCapacity(queueCapacity);
        //配置线程池前缀
        poolExecutor.setThreadNamePrefix("custom-service-");
        //配置拒绝策略
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());

        //数据初始化,不推荐一个项目配置一个线程池,这样若是某些业务出现异常时,会影响到整个项目的健壮性。故我们可以根据业务,为不同的业务配置不同参数的数据库连接池
        //threadPoolTaskExecutor.initialize();
        return poolExecutor;
    }
}

然后是一个工具类,List的,因为要把用户按照一批一批的批次分类,下面代码是百度到的,稍做了修改


import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: HaoXin.Yuan
 * @create: 2020-11-20 14:22
 **/
public class ListUtils {

    public static <T> List<List<T>> splitList(List<T> list, int pageSize){

        int listSize = list.size();
        int page = (listSize + (pageSize -1 )) / pageSize;
        List<List<T>> listArray = new ArrayList<>();
        for (int i = 0; i < page; i++){
            List<T> subList = new ArrayList<>();
            for (int j = 0; j < listSize; j++){
                int pageIndex = ((j + 1) +(pageSize - 1)) / pageSize;
                if (pageIndex == (i + 1)){
                    subList.add(list.get(j));
                }
                if((j + 1) == ((i + 1) * pageSize)){
                    break;
                }
            }
            listArray.add(subList);
        }
        return listArray;
    }
}

接着是业务的代码,我分成了两个示例代码,先begin主线程开始等待,业务service每次执行一次就减一,为了防止业务执行过长,配置了超时等待时间,这里感觉处理不是很好,

如果业务本来就很长呢,还有个问题是每个线程里面的业务是否都执行成功,这个也很难得到,如果用Future可以拿到结果可能会好一些

第一种:不需要拿到结果,带有超时


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: HaoXin.Yuan
 * @create: 2020-11-17 21:54
 **/
@Service
public class AccumCallable {

    @Autowired
    StatisticService statisticService;

    public void test(){
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<List<String>> lists = new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("4");
        list2.add("5");
        list2.add("6");
        list2.add("7");
        list2.add("8");
        lists.add(list1);
        lists.add(list2);
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(3);
        for (int i = 0; i < lists.size(); i++){
            try {
                statisticService.threadSendMsgs(begin,end,lists.get(i));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Start ...");
        begin.countDown();
        try {
            end.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("result ...");
        //此处需要注意的是如果选择shutdown,则会出现子进程还在继续的情况
        //executorService.shutdownNow();

    }


}

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @description:
 * @author: HaoXin.Yuan
 * @create: 2020-11-16 22:18
 **/
@Service
public class StatisticService {

    @Async("customServiceExecutor")
    public void threadSendMsgs(CountDownLatch bein,CountDownLatch end,List<String> lists) throws InterruptedException{
        System.out.println("enter:===threadSendMsgs=======" );
        Thread t = Thread.currentThread();
        String name = t.getName();
        System.out.println("thread:===name======="  + name);
        //final ExecutorService executorService = Executors.newFixedThreadPool(10);
        //List<List<String>> lists = new ArrayList<>();
        bein.await();
        Thread.sleep(60000);
        StatisticService.sendMsgs(lists);
        end.countDown();
        //  executorService.submit(runnable);
    }

    public static void sendMsgs(List<String> lists){
        for (String string:lists){
            System.out.println("result:==========" + string);
        }

    }

}

第二种:拿到结果,用Future,因为是阻塞的,所以加上时间限制

package com.thread.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @description:
 * @author: HaoXin.Yuan
 * @create: 2020-11-17 21:54
 **/
@Service
public class AccumCallable {

    @Autowired
    StatisticService statisticService;

    public void test(){
        List<Future<String>> tasks = new ArrayList<>();
        List<String> results = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<List<String>> lists = new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("4");
        list2.add("5");
        list2.add("6");
        list2.add("7");
        list2.add("8");
        lists.add(list1);
        lists.add(list2);
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(3);
        for (int i = 0; i < lists.size(); i++){
            tasks.add(statisticService.threadSendMsgs(i,begin,end,lists.get(i)));
        }
        System.out.println("Start ...");
        begin.countDown();
        try {
            end.await(5, TimeUnit.SECONDS);
            int j = 0;
            //各个任务执行完毕
            for (Future<String> task : tasks) {

                //每个任务都会再在此阻塞
                try {
                    results.add(task.get(2,TimeUnit.SECONDS));
                } catch (TimeoutException e) {
                    e.printStackTrace();
                    results.add(  j +" batch is fail");
                    j ++;
                    continue;
                }
                j ++;
            }
            for(String string : results){
                System.out.println("result .is.."+string);
            }
            System.out.println("end ...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        //此处需要注意的是如果选择shutdown,则会出现子进程还在继续的情况
        //executorService.shutdownNow();

    }


}
package com.thread.demo;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * @description:
 * @author: HaoXin.Yuan
 * @create: 2020-11-16 22:18
 **/
@Service
public class StatisticService {

    @Async("customServiceExecutor")
    public Future<String> threadSendMsgs(int batch,CountDownLatch bein, CountDownLatch end, List<String> lists) {
        Thread t = Thread.currentThread();
        String name = t.getName();
        System.out.println("thread:===name=======" + name);
        try {
            bein.await();
            //Thread.sleep(60000);
            StatisticService.sendMsgs(lists);
            return new AsyncResult<>(batch + " batch is success");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            end.countDown();
        }
        return new AsyncResult<>(batch + " batch is fail");
    }

    public static void sendMsgs(List<String> lists){
        for (String string:lists){
            System.out.println("result:==========" + string);
        }
    }

}

最后贴上yml配置:

config:
  jwt:
    # 加密密钥
    secret: iwqjhda8232bjgh432[cicada-smile]
    # token有效时长
    expire: 60000
    # header 名称
    header: token
  threadpool:
    core-pool-size: 2
    max-pool-size: 4
    queue-capacity: 5
    keep-alive-seconds: 30

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值