• Spring boot 异步线程池


    package com.loan.msg.config;
    
    import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
    import org.springframework.context.annotation.ComponentScan;
    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;
    
    /**
     * @author 周志伟
     * @projectname 项目名称: ${project_name}
     * @classname: TaskExecutorConfig
     * @description:
     * @date 2018/11/29:13:10
     */
    @Configuration
    @ComponentScan("com.loan.msg.service")
    @EnableAsync //开启异步任务支持
    public class TaskExecutorConfig implements AsyncConfigurer {
    
        @Override
        public Executor getAsyncExecutor() {//实现AsyncConfigurer接口并重写getAsyncExecutor方法,并返回一个ThreadPoolTaskExecutor,这样我们就获得了一个基于线程池TaskExecutor
            ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
            taskExecutor.setCorePoolSize(10);
            taskExecutor.setMaxPoolSize(80);
            taskExecutor.setQueueCapacity(100);
            taskExecutor.initialize();
            return taskExecutor;
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return null;
        }
    }
    package com.loan.msg.service;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    /**
     * @author 周志伟
     * @projectname 项目名称: ${project_name}
     * @classname: AsService
     * @description:
     * @date 2018/11/21:14:27
     */
    @Service
    public class AsService {
        @Async
        public String sayHello() throws InterruptedException {
            System.out.println("执行任务一");
            //模拟执行任务一花费的时间
            Thread.sleep(3000);
            System.out.println("执行任务二");
            return "ok";
        }
    }
  • 相关阅读:
    C#中抽象类和接口
    ArcGIS for Flex中引入google map作底图
    USB、UART、SPI等总线速率
    步步详解之第1节----ALTERA FPGA关于PLL的使用,帮你用光所有PLL
    FPGA笔试必会知识点2—FPGA器件
    FPGA笔试必会知识点1--数字电路基本知识
    (转)modelsim-win64-10.1c的安装
    基于FPGA的温度采集显示与报警
    基于FPGA的步进电机说明文档
    基于FPGA的直流电机
  • 原文地址:https://www.cnblogs.com/yy123/p/10037691.html
Copyright © 2020-2023  润新知