@Async的用法

来源:互联网 发布:同志名媛marc知乎 编辑:程序博客网 时间:2024/06/05 16:54

转载请注明出处 http://www.paraller.com 原文排版地址 点击获取更好阅读体验

属性Bean:```@Configurationpublic 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=2contacts.thread.core-pool=10contacts.thread.max-pool=25contacts.queue.capacity=25

```

主程序入口添加注释:```@EnableAsync@ComponentScan@EnableAutoConfigurationpublic class Application {.......}

```

调用类:@Async("mainExecutor")public void methodA(){}

Configure queue and thread pool for async execution

原创粉丝点击