关于多线程的异步处理,由于项目的需求有个方法需要使用异步的方法来调用,方法是调用外部的接口,执行时间会比较长导致的没有办法同步拿去到结果,所以需要写一个异步线程的方法进行该接口的调用,下面是一个案例以及自己测试的异步结果的情况;
@Configuration
public class SpringBootAsyncConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootMvcConfig.class);
@Value("${spring.async.thread.pool.core-pool-size}")
private int corePoolSize = 10;
@Value("${spring.async.thread.pool.max-pool-size}")
private int maxPoolSize = 1000;
@Value("${spring.async.thread.pool.queue-capacity}")
private int queueCapacity = 1000;
@Value("${spring.async.thread.pool.keep-alive-seconds}")
private int keepAliveSeconds = 600;
public SpringBootAsyncConfig() {
}
@Bean({"asyncThreadPool"})
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(this.corePoolSize);
executor.setMaxPoolSize(this.maxPoolSize);
executor.setQueueCapacity(this.queueCapacity);
executor.setKeepAliveSeconds(this.keepAliveSeconds);
executor.setThreadNamePrefix("AsyncThreadPool-");
executor.setRejectedExecutionHandler(new AbortPolicy());
executor.setTaskDecorator(new TaskDecorator() {
public Runnable decorate(Runnable runnable) {
Map<String, Object> captureMapData = DataCaptureContext.getMapData();
Map<String, String> mdcMap = MDC.getCopyOfContextMap();
return () -> {
try {
if (mdcMap != null) {
MDC.setContextMap(mdcMap);
}
if (captureMapData != null) {
DataCaptureContext.setMapData(captureMapData);
}
runnable.run();
} finally {
MDC.clear();
DataCaptureContext.removeMapData();
}
};
}
});
executor.initialize();
return executor;
}
@Bean({"springSessionRedisTaskExecutor"})
public ThreadPoolTaskExecutor springSessionRedisTaskExecutor() {
ThreadPoolTaskExecutor springSessionRedisTaskExecutor = new ThreadPoolTaskExecutor();
springSessionRedisTaskExecutor.setCorePoolSize(10);
springSessionRedisTaskExecutor.setMaxPoolSize(10);
springSessionRedisTaskExecutor.setKeepAliveSeconds(600);
springSessionRedisTaskExecutor.setQueueCapacity(1000);
springSessionRedisTaskExecutor.setThreadNamePrefix("Spring session redis executor thread: ");
springSessionRedisTaskExecutor.setRejectedExecutionHandler(new DiscardPolicy());
springSessionRedisTaskExecutor.initialize();
return springSessionRedisTaskExecutor;
}
@Bean({"bankInstructionTaskExecutor"})
public ThreadPoolTaskExecutor bankInstructionTaskExecutor() {
ThreadPoolTaskExecutor bankInstructionTaskExecutor = new ThreadPoolTaskExecutor();
bankInstructionTaskExecutor.setCorePoolSize(20);
bankInstructionTaskExecutor.setMaxPoolSize(20);
bankInstructionTaskExecutor.setKeepAliveSeconds(1800);
bankInstructionTaskExecutor.setQueueCapacity(20);
bankInstructionTaskExecutor.setThreadNamePrefix("bank instruction task executor thread: ");
bankInstructionTaskExecutor.setRejectedExecutionHandler(new DiscardPolicy());
bankInstructionTaskExecutor.setTaskDecorator(new TaskDecorator() {
public Runnable decorate(Runnable runnable) {
Map<String, Object> captureMapData = DataCaptureContext.getMapData();
Map<String, String> mdcMap = MDC.getCopyOfContextMap();
return () -> {
try {
if (mdcMap != null) {
MDC.setContextMap(mdcMap);
}
if (captureMapData != null) {
DataCaptureContext.setMapData(captureMapData);
}
runnable.run();
} finally {
MDC.clear();
DataCaptureContext.removeMapData();
}
};
}
});
bankInstructionTaskExecutor.initialize();
return bankInstructionTaskExecutor;
}
@Bean({"instructionStatusUpdateTaskExecutor"})
public ThreadPoolTaskExecutor instructionStatusUpdateTaskExecutor() {
ThreadPoolTaskExecutor bankInstructionTaskExecutor = new ThreadPoolTaskExecutor();
bankInstructionTaskExecutor.setCorePoolSize(20);
bankInstructionTaskExecutor.setMaxPoolSize(20);
bankInstructionTaskExecutor.setKeepAliveSeconds(1800);
bankInstructionTaskExecutor.setQueueCapacity(20);
bankInstructionTaskExecutor.setThreadNamePrefix("instruction status update executor thread: ");
bankInstructionTaskExecutor.setRejectedExecutionHandler(new DiscardPolicy());
bankInstructionTaskExecutor.setTaskDecorator(new TaskDecorator() {
public Runnable decorate(Runnable runnable) {
Map<String, Object> captureMapData = DataCaptureContext.getMapData();
Map<String, String> mdcMap = MDC.getCopyOfContextMap();
return () -> {
try {
if (mdcMap != null) {
MDC.setContextMap(mdcMap);
}
if (captureMapData != null) {
DataCaptureContext.setMapData(captureMapData);
}
runnable.run();
} finally {
MDC.clear();
DataCaptureContext.removeMapData();
}
};
}
});
bankInstructionTaskExecutor.initialize();
return bankInstructionTaskExecutor;
}
}
第五步:异步方法的调用情况二
需要注意的点有三个,也就是分为三种情况:
综上所述,异步方法要正确的时候才会达到想要的结果,否则的话容易同步方法里面调用异步方法,导致异步方法没有效果,闹出笑话来。始终坚持学习,始终去实践测试代码,看一下执行的结果才能正确写出想要的代码,程序员一定要做到知其然知其所以然,共勉。