/*** 线程池常量*/publicclassThreadPoolConstants {/*** 用户线程前缀*/publicfinalstaticString USER_THREAD_PREFIX = "user-thread";/*** 学校线程前缀*/publicfinalstaticString SCHOOL_THREAD_PREFIX = "school-thread";/*** 线程池bean后缀名*/publicfinalstaticString THREA_BEAN_SUFFIX = "-exector-bean...
分为三步 启动类加 @EnableAsync 注解 在方法上加 @Async 注解 创建线程池配置类 1.启动类加 @EnableAsync 注解 @SpringBootApplication @EnableAsync public class FacadeH5Application { publi
//创建一个核心线程数和最大线程数相同的线程池ExecutorServiceexecutorService=Executors.newFixedThreadPool(4);executorService.execute(()->{System.out.println("this is fixed thread pool");});//创建一个单线程的线程池ExecutorServicesingleExecutor=Executors.newSingleThreadExecutor();singleExecutor.execute(()-...
然后执行之前写的测试代码发现,使用的线程池已经变成自定义的线程池了。 INFO 12740 --- [ myExecutor--2] c.h.s.t.t.ThreadpoolApplicationTests : threadPoolTaskExecutor 创建线程 INFO 12740 --- [ myExecutor--1] c.h.s.threadpool.threadpool.AsyncTest : 异步线程启动 started.async注解创建 第二步...
使用扩展线程池 一、异步调用 @EnableAsync开启异步调用 启动类添加@EnableAsync注解 1. @SpringBootApplication @EnableAsync public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args);
1.4 线程池的核心参数 ThreadPoolExecutor 有四个构造方法,前三个都是调用最后一个(最后一个参数最全) public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQ...
用Spring提供的对 ThreadPoolExecutor 封装的线程池 ThreadPoolTaskExecutor ,直接使用注解启用 配置 @Configuration @EnableAsync public class ExecutorConfig { privatestaticfinalLogger logger=LoggerFactory.getLogger(ExecutorConfig.class);@Value("${async.executor.thread.core_pool_size}")privateint corePoolSize;@...
* TODO:线程池的配置 */@Configuration @EnableAsyncpublicclassExecutorConfig{privatestaticfinal Logger logger=LoggerFactory.getLogger(ExecutorConfig.class);/** * TODO: 此方法名称为asyncPromiseExecutor,即在spring中注入了一个名字为asyncPromiseExecutor的bean ...
使用默认的线程池# 方式一:通过@Async注解调用# public class AsyncTest { @Async public void async(String name) throws InterruptedException { System.out.println("async" + name + " " + Thread.currentThread().getName()); Thread.sleep(1000); ...
//设置线程名称前缀 executor.setThreadNamePrefix("sendSms-"); //设置拒绝策略executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //等待所有任务结束后再关闭线程池 executor.setWaitForTasksToCompleteOnShutdown(true); //设置线程池中任务的等待时间 ...