@Async异步注解使用自定义线程池

第一步,启动类添加@EnableAsync注解(必须)

/**
 * 启动程序
 * 
 */
@SpringBootApplication
@EnableAsync
public class RuoYiApplication
{
    public static void main(String[] args)
    {
       }
}

第二部,添加配置类,创建线程池,放入bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolCo {

    @Bean(name = "scorePoolTaskExecutor")
    public ThreadPoolTaskExecutor getScorePoolTaskExecutor() {
        System.out.println("使用了");
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //核心线程数
        taskExecutor.setCorePoolSize(10);
        //线程池维护线程的最大数量,只有在缓冲队列满了之后才会申请超过核心线程数的线程
        taskExecutor.setMaxPoolSize(100);
        //缓存队列
        taskExecutor.setQueueCapacity(50);
        //许的空闲时间,当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
        taskExecutor.setKeepAliveSeconds(200);
        //异步方法内部线程名称
        taskExecutor.setThreadNamePrefix("poolTestThread-");
        /**
         * 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize,如果还有任务到来就会采取任务拒绝策略
         * 通常有以下四种策略:
         * ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。
         * ThreadPoolExecutor.DiscardPolicy:也是丢弃任务,但是不抛出异常。
         * ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)
         * ThreadPoolExecutor.CallerRunsPolicy:重试添加当前的任务,自动重复调用 execute() 方法,直到成功
         */
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();

        System.out.println("------>== @Async 业务处理线程配置成功,核心线程池:[{}],最大线程池:[{}],队列容量:[{}],线程名称前缀:[{}] ==<------");
        return taskExecutor;
    }
}

第三步,controller中调用自定义线程池异步方法

@GetMapping("/list")
public void test01() throws Exception{

    batchPaymentService.t();

    batchPaymentService.doTaskOne();
    batchPaymentService.doTaskTwo();
    batchPaymentService.doTaskThree();
    System.out.println("执行完了");

}

第四步,service中异步方法

public static Log log = LogFactory.getLog(BatchPaymentServiceImpl.class);


@Async(value ="scorePoolTaskExecutor")
public    void   t() throws InterruptedException {
    System.out.println("1");
    Thread.sleep(10000);
    for (int i = 0; i < 100; i++) {
        System.out.println(i);
    }
    log.info("ttt");
}

public static Random random = new Random();




@Async("scorePoolTaskExecutor")
public void doTaskOne() throws Exception {
    log.info("开始做任务一");
    long start = System.currentTimeMillis();
    Thread.sleep(random.nextInt(10000));
    long end = System.currentTimeMillis();
    log.info("完成任务一,耗时:" + (end - start) + "毫秒");
}

@Async("scorePoolTaskExecutor")
public void doTaskTwo() throws Exception {
    log.info("开始做任务二");
    long start = System.currentTimeMillis();
    Thread.sleep(random.nextInt(10000));
    long end = System.currentTimeMillis();
    log.info("完成任务二,耗时:" + (end - start) + "毫秒");
}

@Async("scorePoolTaskExecutor")
public void doTaskThree() throws Exception {
    System.out.println("开始做任务三");
    long start = System.currentTimeMillis();
    Thread.sleep(random.nextInt(10000));
    long end = System.currentTimeMillis();
    System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");
}

最终结果,可见实现了自定义线程池 

@Async异步注解使用自定义线程池_第1张图片

你可能感兴趣的:(java,开发语言,后端,线程池)