转载请注明出处 http://www.paraller.com
原文排版地址 点击跳转
属性Bean:
@Configuration
public class ContactsExecutor {
@Value("${contacts.thread.core-pool}")
private int corePoolSize;
@Value("${contacts.thread.max-pool}")
private int maxPoolSize;
@Value("${contacts.queue.capacity}")
private int queueCapacity;
@Value("${contacts.thread.timeout}")
private int threadTimeout;
@Bean
@Qualifier("contactsExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
threadPoolTaskExecutor.setKeepAliveSeconds(threadTimeout);
return threadPoolTaskExecutor;
}
}
线程配置文件:
# thread pool and queue size for processing contacts data
contacts.thread.timeout=2
contacts.thread.core-pool=10
contacts.thread.max-pool=25
contacts.queue.capacity=25
主程序入口添加注释:
@EnableAsync
@ComponentScan
@EnableAutoConfiguration
public class Application {
.......
}
调用类:
@Async("mainExecutor")
public void methodA(){}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。