package com.haier.backservice.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class ThreadPoolConfig {
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int COUR_SIZE = CPU_COUNT * 2;
private static final int MAX_COUR_SIZE = CPU_COUNT * 4;
@Bean("myThreadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(COUR_SIZE);
threadPoolTaskExecutor.setMaxPoolSize(MAX_COUR_SIZE);
threadPoolTaskExecutor.setQueueCapacity(MAX_COUR_SIZE * 4);
threadPoolTaskExecutor.setThreadNamePrefix("back-thread-");
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return threadPoolTaskExecutor;
}
}
package com.haier.backservice.config;
import com.haier.backservice.common.Constant;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@Async(value = "myThreadPoolTaskExecutor")
public class ThreadPoolConfigUse {
@Resource
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
public void pointcut(){
threadPoolTaskExecutor.execute(()->{
System.out.println(Thread.currentThread().getName());
});
}
@Async
public void asyncTest(){
System.out.println(Thread.currentThread().getName());
}
}
package com.haier.backservice;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@Slf4j
@MapperScan("com.haier.backservice.system.mapper")
@EnableAsync
public class BackserviceApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BackserviceApplication.class, args);
}
}