步骤1:配置线程池,添加@Configuration和@EnableAsync注解
@Configuration@EnableAsyncpublic class ExecutorConfig { /** * 线程池 * * @return */ @Bean(name = "asyncExecutor") public Executor asyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(15); executor.setQueueCapacity(25); executor.setKeepAliveSeconds(200); executor.setThreadNamePrefix("async-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 等待所有任务都完成再继续销毁其他的Bean executor.setWaitForTasksToCompleteOnShutdown(true); // 线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住 executor.setAwaitTerminationSeconds(60); executor.initialize(); return executor; } }
步骤2:定义方法A,方法B,方法C,方法D
@Servicepublic class AsyncService { @Async("asyncExecutor") public Future
上一篇:微信小程序中怎么开发