|
@ -0,0 +1,53 @@
|
|
|
|
package com.yihu.wlyy.config.async;
|
|
|
|
|
|
|
|
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Administrator on 2016.10.18.
|
|
|
|
* 启用多綫程
|
|
|
|
*/
|
|
|
|
@Configuration
|
|
|
|
@EnableAsync
|
|
|
|
public class AsyncConfig {
|
|
|
|
/**
|
|
|
|
* 如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程
|
|
|
|
*/
|
|
|
|
private int corePoolSize = 5;
|
|
|
|
/**
|
|
|
|
* 如果池中的线程数=maximumPoolSize,则有空闲线程使用空闲线程,否则新任务放入queueCapacity.
|
|
|
|
*/
|
|
|
|
private int maxPoolSize = 10;
|
|
|
|
/**
|
|
|
|
* 缓冲队列大小
|
|
|
|
*/
|
|
|
|
private int queueCapacity = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 线程池维护线程所允许的空闲时间 秒
|
|
|
|
*/
|
|
|
|
private int keepAliveSeconds = 300;
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public Executor wlyyExecutor() {
|
|
|
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
|
|
|
executor.setCorePoolSize(corePoolSize);
|
|
|
|
executor.setMaxPoolSize(maxPoolSize);
|
|
|
|
executor.setQueueCapacity(queueCapacity);
|
|
|
|
executor.setKeepAliveSeconds(keepAliveSeconds);
|
|
|
|
/** Reject策略预定义有四种:
|
|
|
|
(1)ThreadPoolExecutor.AbortPolicy策略,是默认的策略,处理程序遭到拒绝将抛出运行时 RejectedExecutionException。
|
|
|
|
(2)ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
|
|
|
|
(3)ThreadPoolExecutor.DiscardPolicy策略,不能执行的任务将被丢弃.
|
|
|
|
(4)ThreadPoolExecutor.DiscardOldestPolicy策略,如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程).
|
|
|
|
*/
|
|
|
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
|
executor.initialize();
|
|
|
|
return executor;
|
|
|
|
}
|
|
|
|
}
|