spring的异步调用好用吗

spring的异步调用好用吗?@async,企业是直接开线程异步吗

标注了 @Async 的方法在执行时会在新线程中执行,默认情况下没有使用线程池,如果需要使用线程池执行可以声明一个类型为 TaskExecutor 的 bean 或名称为 taskExecutor 且类型为 Executor 的 bean。

使用@Async实现异步

声明一个类实现AsyncConfigurer ,交给spring管理
使用@EnableAsync启动异步

@EnableAsync
@Configuration
public class AsyncConfig implements AsyncConfigurer {

@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(20);
    executor.setQueueCapacity(1000);
    executor.setKeepAliveSeconds(300);
    executor.setThreadNamePrefix("dspk-Executor-");
    // 拒绝策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
}

/**
 * 异常处理器
 */
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    return new SimpleAsyncUncaughtExceptionHandler();
}

}