springboot 使用 @Async 提成程序处理速度

前言

众所周知,java代码是顺序执行,而异步是处理高并发问题,提高程序运行速度的银弹。
本例子中,程序需要调用现有的接口,来完成对某些指定数据的刷新,之前是顺序执行,执行时间比较长,改成异步后,效果明显。

代码与测试

配置线程池


import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Description:
 * User: liuhongtao
 * Date: 2019-01-11
 * Time: 16:37
 */

@Configuration
@EnableAsync
@Slf4j
public class ExecutorConfig implements AsyncConfigurer {
    @Value("${async.executor.thread.core_pool_size}")
    private int corePoolSize;
    @Value("${async.executor.thread.max_pool_size}")
    private int maxPoolSize;
    @Value("${async.executor.thread.queue_capacity}")
    private int queueCapacity;
    @Value("${async.executor.thread.name.prefix}")
    private String namePrefix;

    @Bean(name = "asyncServiceExecutor")
    public Executor asyncServiceExecutor() {
        log.warn("start asyncServiceExecutor");
        //在这里可以配置自己定义的线程池,也可以直接使用系统自己的线程池
        //ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(corePoolSize);
        //配置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        //配置队列大小
        executor.setQueueCapacity(queueCapacity);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix(namePrefix);
        //配置拒绝机制
        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }

    /**
     * The {@link Executor} instance to be used when processing async
     * method invocations.
     */
    @Override
    public Executor getAsyncExecutor() {
        return null;
    }

    //配置异常处理机制
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (ex,method,params)->{
            log.error("异步线程执行失败。方法:[{}],异常信息[{}] : ", method, ex.getMessage(),ex);
        };
    }
}

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Description:
 * User: liuhongtao
 * Date: 2019-01-11
 * Time: 16:41
 */
@Slf4j
public class VisiableThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {


    //打印队列的详细信息
    private void showThreadPoolInfo(String prefix){
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();

        if(null==threadPoolExecutor){
            return;
        }

        log.info("{}, {},taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]",
                this.getThreadNamePrefix(),
                prefix,
                threadPoolExecutor.getTaskCount(),
                threadPoolExecutor.getCompletedTaskCount(),
                threadPoolExecutor.getActiveCount(),
                threadPoolExecutor.getQueue().size());
    }


    @Override
    public void execute(Runnable task) {
        showThreadPoolInfo("1. do execute");
        super.execute(task);
    }

    @Override
    public void execute(Runnable task, long startTimeout) {
        showThreadPoolInfo("2. do execute");
        super.execute(task, startTimeout);
    }

    @Override
    public Future<?> submit(Runnable task) {
//        showThreadPoolInfo("3. do submit");
        return super.submit(task);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
//        showThreadPoolInfo("4. do submit");
        return super.submit(task);
    }

    @Override
    public ListenableFuture<?> submitListenable(Runnable task) {
//        showThreadPoolInfo("5. do submitListenable");
        return super.submitListenable(task);
    }

    @Override
    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
//        showThreadPoolInfo("6. do submitListenable");
        return super.submitListenable(task);
    }

}

定义异步处理方法


import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * Description:
 * User: liuhongtao
 * Date: 2019-01-11
 * Time: 16:45
 */
public interface AsyncService {

    void executeAsync(CountDownLatch countDownLatch, List<Integer> list,String url);

    Future<String> doTask1() throws InterruptedException;

    Future<String> doTask2() throws InterruptedException;
}

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * Description:
 * User: liuhongtao
 * Date: 2019-01-11
 * Time: 17:00
 */

@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {

    @Autowired
    protected RestTemplate restTemplate;


    @Override
    @Async("asyncServiceExecutor")
    public void executeAsync(CountDownLatch countDownLatch, List<Integer> list,String url) {
        try{
            log.warn("{},start executeAsync",Thread.currentThread().getName());
            //异步线程要做的事情
            for (Integer i : list) {
                String result = restTemplate.getForObject(String.format(url, i),String.class);
                log.info("{},id={},reuslut={}",Thread.currentThread().getName(),i,result);
            }
            log.warn("{},end executeAsync",Thread.currentThread().getName());
        }finally {
            countDownLatch.countDown();// 很关键, 无论上面程序是否异常必须执行countDown,否则await无法释放
        }
    }


    //带返回值的任务
    @Override
    @Async("asyncServiceExecutor")
    public Future<String> doTask1() throws InterruptedException{
        log.info("Task1 started.");
        long start = System.currentTimeMillis();
        Thread.sleep(5000);
        long end = System.currentTimeMillis();
        int i = 1/0;
        log.info("Task1 finished, time elapsed: {} ms.", end-start);
//        latch.countDown();
        return new AsyncResult<>("Task1 accomplished!");
    }

    @Override
    @Async("asyncServiceExecutor")
    public Future<String> doTask2() throws InterruptedException{
        log.info("Task2 started.");
        long start = System.currentTimeMillis();
        Thread.sleep(3000);
        long end = System.currentTimeMillis();

        log.info("Task2 finished, time elapsed: {} ms.", end-start);
//        latch.countDown();
        return new AsyncResult<>("Task2 accomplished!");
    }
}

运行测试

@Test
    public void test1() {
        Long startTime = System.currentTimeMillis();
        String url = "http://xx.xx.xx.xx:8992/cab/refresh/%s/?p=abcd";
        String sql = "select id\n" +
                "from abc\n" +
                "where ccc in\n" +
                "      (61492768,61492730,61492694,61492680,61492636,61492612,61492574,61496926) order by id;";

        List<Integer> contTvIds = jdbcTemplate_cs.queryForList(sql, Integer.class);
        List<List<Integer>> parts = Lists.partition(contTvIds, 50);
        CountDownLatch countDownLatch = new CountDownLatch(parts.size());
        for (List<Integer> list : parts) {

            asyncService.executeAsync(countDownLatch, list, url);
        }
        try {
            countDownLatch.await(); //等待所有线程都结束了.
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("所有线程都跑完了.耗时={},执行总条数={}",System.currentTimeMillis() - startTime,contTvIds.size());
    }

    @Test
    public void test3(){
        try {
            List<Future<String>> tasks = new ArrayList<>();
            List<String> results = new ArrayList<>();
            tasks.add(asyncService.doTask1());
            tasks.add(asyncService.doTask2());
            //各个任务执行完毕
            for (Future<String> task : tasks) {
                //每个任务都会再在此阻塞。
                try {
                    results.add(task.get(3, TimeUnit.SECONDS));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            log.info("All tasks finished!");
            log.info("执行结果:{}", JSON.toJSONString(results));
//            return "S";
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

测试结果

可以看到程序启动,线程池初始化
在这里插入图片描述
线程运行结果,一共启动了4个线程
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值