spring异步多线程

配置对异步任务的支持

spring通过任务执行器TaskExecutor来实现多线程和并发编程。
使用ThreadPoolTaskExecutor可以实现一个基于线程池的TaskExecutor

package com.sky.springtest.config;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@ComponentScan({"com.sky.springtest"})
//开启异步
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {
	@Override
	public Executor getAsyncExecutor() {
		//线程任务池
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(5);
		taskExecutor.setMaxPoolSize(10);
		taskExecutor.setQueueCapacity(25);
		taskExecutor.initialize();
		return taskExecutor;
	}
	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		System.err.println("getAsyncUncaughtExceptionHandler.....");
		return null;
	}
}

异步任务类

package com.sky.springtest.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncTaskService {
	@Async
	public void executeAsyncTask(Integer i) {
		System.err.println("执行异步任务:" + i + " " + 100 / i);
	}
	@Async
	public void executeAsyncTask2(Integer i) {
		System.err.println("------------执行异步任务2:" + i + " " + 100 / i);
	}
}

测试类

package com.sky.springtest.config;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.sky.springtest.service.AsyncTaskService;

public class TaskExecutorConfigTest {
	private static AnnotationConfigApplicationContext context = null;
	@BeforeClass
	public static void beforeClass() {
		context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
	}
	@Test
	public void test() {
		AsyncTaskService taskService = context.getBean(AsyncTaskService.class);
		for (int i = 0; i < 10; i++) {
			taskService.executeAsyncTask(i);
			taskService.executeAsyncTask2(i);
		}
		// 防止主进程立即运行完毕,延迟10秒退出主进程
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
		}
		context.close();
	}
}
/*打印结果:
执行异步任务:2 50
执行异步任务:1 100
------------执行异步任务2:2 50
------------执行异步任务2:1 100
------------执行异步任务2:3 33
执行异步任务:3 33
执行异步任务:5 20
------------执行异步任务2:4 25
执行异步任务:4 25
------------执行异步任务2:6 16
执行异步任务:6 16
------------执行异步任务2:5 20
------------执行异步任务2:7 14
------------执行异步任务2:8 12
执行异步任务:9 11
------------执行异步任务2:9 11
执行异步任务:7 14
执行异步任务:8 12
[04 20:42:28,975 ERROR] [ThreadPoolTaskExecutor-1] interceptor.SimpleAsyncUncaughtExceptionHandler - Unexpected error occurred invoking async method: public void com.sky.springtest.service.AsyncTaskService.executeAsyncTask(java.lang.Integer)
java.lang.ArithmeticException: / by zero
*/

0不能做除数,程序抛出异常

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26264237

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值