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  List> splitList(List list, int pageSize){

        int listSize = list.size();
        int page = (listSize + (pageSize -1 )) / pageSize;
        List> listArray = new ArrayList<>();
        for (int i = 0; i < page; i++){
            List 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 list1 = new ArrayList<>();
        List list2 = new ArrayList<>();
        List> 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 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> lists = new ArrayList<>();
        bein.await();
        Thread.sleep(60000);
        StatisticService.sendMsgs(lists);
        end.countDown();
        //  executorService.submit(runnable);
    }

    public static void sendMsgs(List 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> tasks = new ArrayList<>();
        List results = new ArrayList<>();
        List list1 = new ArrayList<>();
        List list2 = new ArrayList<>();
        List> 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 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 threadSendMsgs(int batch,CountDownLatch bein, CountDownLatch end, List 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 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

 

你可能感兴趣的:(Java,多线程,java)