Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Created August 21, 2019 08:59
Show Gist options
  • Save codethereforam/29677c54f314066ef90d33fe75bc1107 to your computer and use it in GitHub Desktop.
Save codethereforam/29677c54f314066ef90d33fe75bc1107 to your computer and use it in GitHub Desktop.
提供一些构造spring的ThreadPoolTaskExecutor的静态工厂方法,模仿JDK中的Executors
/**
* 提供一些构造spring的ThreadPoolTaskExecutor的静态工厂方法,模仿JDK中的Executors
*
* @author yanganyu
* @date 2019/8/21 16:23
*/
public class SpringExecutors {
private SpringExecutors() {
}
/**
* 等价于{@code Executors.newSingleThreadExecutor}
*
* @param threadNamePrefix thread name prefix
* @return ThreadPoolTaskExecutor
*/
public static ThreadPoolTaskExecutor newSingleThreadExecutor(String threadNamePrefix) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix(threadNamePrefix);
executor.setCorePoolSize(1);
executor.setMaxPoolSize(1);
executor.setKeepAliveSeconds(0);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
return executor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment