SpringBoot
  • Introduction
  • springboot日志
    • logstash-json日志
  • springboot数据库
    • 初始化数据库
  • MongoDB使用
    • SpringBoot整合MongoDB(1)
    • SpringBoot整合MongoDB(2)
    • SpringBoot整合MongoDB(3)
    • SpringBoot整合MongoDB(4)
  • prometheus监控
    • springboot整合prometheus(一)
    • springboot整合prometheus(二)
    • springboot整合prometheus(三)
    • springboot整合prometheus(四)
    • springboot整合prometheus(五)
  • spring IOC
    • 注解IOC
  • 过滤器和拦截器
    • 过滤器
    • 拦截器
  • mybatis
    • springboot快速整合mybatis
  • 使用缓存
    • redis-cache
  • 异步线程池
    • 快速开始
    • 线程池
  • AOP切面
    • 快速开始
  • 限流
    • 使用Sentinel实现接口限流
      • 使用Sentinel实现接口限流
  • MybatisPlus
    • 快速开始
    • 基本使用
    • 查询方法
    • 主键策略和基本配置
Powered by GitBook
On this page

Was this helpful?

  1. 异步线程池

线程池

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import lombok.Generated;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lichunpeng
 */
@Configuration
@Slf4j
@Generated
public class AsyncConfig extends AsyncConfigurerSupport {

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor getAsyncThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(10);
        threadPool.setMaxPoolSize(60);
        threadPool.setKeepAliveSeconds(60);
        threadPool.setQueueCapacity(10000);
        threadPool.setWaitForTasksToCompleteOnShutdown(true);
        threadPool.setAwaitTerminationSeconds(60);
        threadPool.initialize();
        return threadPool;
    }

    @Bean
    public ThreadPoolExecutor echartExecutor(@Autowired @Qualifier("echartThreadFactory") ThreadFactory threadFactory) {
        log.info("ThreadPoolExecutor echartExecutor init ... ");
        ThreadPoolExecutor threadPoolExecutor = 
                new ThreadPoolExecutor(9, 27, 30, TimeUnit.MINUTES, new ArrayBlockingQueue<>(10000),
                        threadFactory, new AbortPolicy());
        threadPoolExecutor.prestartAllCoreThreads(); // 预启动所有核心线程
        log.info("ThreadPoolExecutor echartExecutor init completed  !!! ");
        return threadPoolExecutor;
    }

    @Bean("echartThreadFactory")
    public ThreadFactory getThreadFactory() {
        return new ThreadFactory() {
            private final AtomicInteger mThreadNum = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r, "echart-thread-" + mThreadNum.getAndIncrement());
                log.info(t.getName() + " has been created");
                return t;
            }

        };
    }


}
Previous快速开始NextAOP切面

Last updated 6 years ago

Was this helpful?