spring异步多线程

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不能做除数,程序抛出异常

你可能感兴趣的:(spring)