首页 > 代码库 > ExecutorService与ThreadPoolTaskExecutor
ExecutorService与ThreadPoolTaskExecutor
1.ExecutorService
private static ExecutorService exec = null;
public static ExecutorService getExecutorServiceInstance(){
if(exec == null){
exec = Executors.newCachedThreadPool();
}
return exec;
}
public void threadNoticeOrMessageOrShortMessage (Integer type, Map<String, String> map, List<String> replaceParameter, List<String> list, Integer saveFlag){
exec = getExecutorServiceInstance();
NoticeOrMessageOrShortMessage noticeOrMessageOrShortMessage = new NoticeOrMessageOrShortMessage(getMessagePushInstance(), type, map, replaceParameter, list, saveFlag,
messagePushService, sendPushService, sendSmsService, sendMessageService);
exec.execute(noticeOrMessageOrShortMessage);
}
2.ThreadPoolTaskExecutor
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="http://www.mamicode.com/${task.core_pool_size}" />
<property name="maxPoolSize" value="http://www.mamicode.com/${task.max_pool_size}" />
<property name="queueCapacity" value="http://www.mamicode.com/${task.queue_capacity}" />
<property name="keepAliveSeconds" value="http://www.mamicode.com/${task.keep_alive_seconds}" />
<!-- 新增 -->
- <!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
- <property name="rejectedExecutionHandler">
- <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
- </property>
</bean>
@Resource(name = "taskExecutor")
private TaskExecutor taskExecutor;
private void addSendTask(final MimeMessage mimeMessage) {
try {
taskExecutor.execute(new Runnable() {
public void run() {
javaMailSender.send(mimeMessage);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
另外一种方式(未验证代码准确性)
private static ThreadPoolTaskExecutor threadPoolTaskExecutor = null;
public static ThreadPoolTaskExecutor getThreadPoolTaskExecutor Instance(){
if(threadPoolTaskExecutor == null){
threadPoolTaskExecutor.setCorePoolSize(5);
threadPoolTaskExecutor.setMaxPoolSize(50);
threadPoolTaskExecutor.setQueueCapacity(1000);
threadPoolTaskExecutor.setKeepAliveSeconds(60);
try {
taskExecutor.execute(new Runnable() {
public void run() {
javaMailSender.send(mimeMessage);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
return exec;
}
具体线程还是有些不懂
ExecutorService与ThreadPoolTaskExecutor
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。