【快学springboot】10.使用@Async注解创建多线程,自定义线程池

说明

使用@Async注解创建多线程非常的方便,还可以通过配置,实现线程池。比直接使用线程池简单太多。而且在使用上跟普通方法没什么区别,加上个@Async注解即可实现异步调用。

用法

AsyncTask.java

@Component
public class AsyncTask {
 private static final Logger LOG = LoggerFactory.getLogger(AsyncTask.class);
 @Async
 public void register(){
 LOG.info("多线程开始注册模拟");
 try {
 Thread.sleep(1000*1);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 LOG.info("多线程注册成功");
 }
}

这里只是做一个简单地打印输出,使用Log4J打印是为了方便看到线程名

AsyncTaskController.java

@RestController
@RequestMapping(value = "/async")
public class AsyncTaskController {
 private final static Logger LOG = LoggerFactory.getLogger(AsyncTaskController.class);
 @Autowired
 private AsyncTask asyncTask;
 @GetMapping(value = "/test")
 public Object test(){
 for (int i = 0; i < 10; i++) {
 asyncTask.register();
 }
 System.out.println("主线程结束");
 return "OK";
 }
}

这里循环创建10个线程

启用Async

启用Async需要添加@EnableAsync注解

@SpringBootApplication
@ServletComponentScan
@EnableAsync
public class App {
 public static void main(String[] args) {
 SpringApplication.run(App.class, args);
 }
}

结果

【快学springboot】10.使用@Async注解创建多线程,自定义线程池

可以看到,主线程结束已经结束。可证证明多线程起了效果。另外通过查看线程名,可以看到创建了10个线程去执行。


使用线程池

通过上面的结果可以看出,直接使用@Async注解是直接创建线程去执行的。但是在实际开发中,都应该使用线程池去管理线程,节省线程开销。

配置

TaskExecutorConfig.class

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;

@Configuration
public class TaskExecutorConfig implements AsyncConfigurer {
 /**
 * Set the ThreadPoolExecutor's core pool size.
 */
 private static final int CORE_POOL_SIZE = 2;
 /**
 * Set the ThreadPoolExecutor's maximum pool size.
 */
 private static final int MAX_POOL_SIZE = 2;
 /**
 * Set the capacity for the ThreadPoolExecutor's BlockingQueue.
 */
 private static final int QUEUE_CAPACITY = 10;
 /**
 * 通过重写getAsyncExecutor方法,制定默认的任务执行由该方法产生
 *
 * 配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法,并返回一个ThreadPoolTaskExevutor
 * 这样我们就获得了一个基于线程池的TaskExecutor
 */
 @Override
 public Executor getAsyncExecutor() {
 ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
 taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
 taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
 taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
 taskExecutor.initialize();
 return taskExecutor;
 }
}

这里设置了最大两个线程。

测试

重启程序测试下:

【快学springboot】10.使用@Async注解创建多线程,自定义线程池

结果

可以看到只有两个线程在执行,证明配置的线程池起作用了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值