Springboot的异步任务
1.配置
(1)在Springboot的主方法上加@EnableAsync注解
(2)在你所需要异步的方法上加@Async注解
2.示例
@Service
public class AsyncService {
@Async
public Future< String> testAsync ( )
{
try {
Thread. sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "异步方法线程名字:" + Thread. currentThread ( ) . getName ( ) ) ;
System. out. println ( "测试带有返回值的异步任务方法" ) ;
return new AsyncResult < > ( "带参数的异步方法返回值" ) ;
}
@Async
public void testNoReturnAsync ( )
{
try {
Thread. sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "没有返回值的异步方法线程名字:" + Thread. currentThread ( ) . getName ( ) ) ;
System. out. println ( "测试不带有返回值的异步任务方法" ) ;
}
@Async
public void test1 ( )
{
System. out. println ( "1" ) ;
}
@Async
public void test2 ( )
{
System. out. println ( "2" ) ;
}
@Async
public void test3 ( )
{
System. out. println ( "3" ) ;
}
@Async
public void test4 ( )
{
System. out. println ( "4" ) ;
}
}
@Autowired
private AsyncService asyncService;
@RequestMapping ( "/testAysnc" )
@ResponseBody
public String testAsync ( ) throws InterruptedException, ExecutionException {
asyncService. testNoReturnAsync ( ) ;
System. out. println ( "controller线程名字:" + Thread. currentThread ( ) . getName ( ) ) ;
System. out. println ( "wubin333" ) ;
return "404" ;
}
```
#### 配置线程池,可配可不配
```java
package com. axfiber. intelligenceteam. configuration;
import org. springframework. aop. interceptor. AsyncUncaughtExceptionHandler;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import org. springframework. scheduling. annotation. AsyncConfigurer;
import org. springframework. scheduling. concurrent. ThreadPoolTaskExecutor;
import java. lang. reflect. Method;
import java. util. concurrent. Executor;
import java. util. concurrent. ThreadPoolExecutor;
@Configuration
public class AsyncPoolConfig implements AsyncConfigurer {
@Bean
@Override
public Executor getAsyncExecutor ( ) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ( ) ;
executor. setCorePoolSize ( 10 ) ;
executor. setMaxPoolSize ( 20 ) ;
executor. setQueueCapacity ( 20 ) ;
executor. setKeepAliveSeconds ( 60 ) ;
executor. setThreadNamePrefix ( "wubin_asyncThreadPool_" ) ;
executor. setWaitForTasksToCompleteOnShutdown ( true ) ;
executor. setAwaitTerminationSeconds ( 60 ) ;
executor. setRejectedExecutionHandler ( new ThreadPoolExecutor. AbortPolicy ( ) ) ;
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler ( ) {
return new AsyncExceptionHandler ( ) ;
}
class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException ( Throwable throwable, Method method, Object. . . objects) {
throwable. printStackTrace ( ) ;
}
}
}
总结:异步任务是通过多线程的方法实现的,两个线程原则上是异步执行的,但是如果一个线程的执行需要异步任务的结果,这个时候两个线程就是同步的了,就比如如果你想在主线程中输入异步服务中带返回值方法的返回值,这个时候主线程就是和异步方法是同步了。因此要实现线程的异步就不能利用异步服务的返回值。而且带返回值的异步方法返回值要用Future这个接口写,return new AsyncResult.