Spring Boot 调用@Async异步方法创建线程2种方式(调用外部的异步方法,调用内部的异步方法) 清疚 2022-09-11 14:23 168阅读 0赞 # 1 Maven依赖 # <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--hutool工具包--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.1</version> </dependency> # 2 调用外部的异步方法 # ## 2.1 实现思路 ## 调用外部的异步方法创建线程,直接装配异步方法所在类,再调用异步方法便可创建线程。 ## 2.2 异步方法 ## package com.service; import cn.hutool.core.date.DateUtil; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.Future; @Service public class FutureAsyncService { /** * 测试Future子线程 * * @return */ @Async public Future futureSubThread(int i) throws Exception { Thread.sleep(100); System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " " + i + " "); return new AsyncResult(i); } } ## 2.3 调用代码 ## package com.service; import cn.hutool.core.date.DateUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; @Service public class AsyncService { @Autowired private FutureAsyncService futureAsyncService; /** * 调用外部的异步方法 * * @throws Exception */ public void testFuture() throws Exception { for (int i = 0; i < 100; i++) { futureAsyncService.futureSubThread(i); } System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ":退出循环\t"); } } # 3 调用内部的异步方法 # ## 3.1 实现思路 ## 调用内部的异步方法创建线程,必须获取当前类的容器,然后再调用内部的异步方法创建线程。 ### 3.2 调用代码 ### package com.service; import cn.hutool.core.date.DateUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import java.util.Date; import java.util.concurrent.Future; @Service public class AsyncService { @Autowired private ApplicationContext applicationContext; /** * 调用内部的异步方法 * * @throws Exception */ public void testFuture() throws Exception { AsyncService asyncService = applicationContext.getBean(AsyncService.class); for (int i = 0; i < 100; i++) { asyncService.futureSubThread(i); } System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ":退出循环\t"); } /** * 测试Future子线程 * * @return */ @Async public Future futureSubThread(int i) throws Exception { Thread.sleep(100); System.out.print(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " " + i + " "); return new AsyncResult(i); } } # 注: # (1)有关多线程的配置请查看以下博客。 [Spring Boot配置多线程(创建、使用)![icon-default.png_t_L892][]https://blog.csdn.net/qq\_38974638/article/details/120145228][Spring Boot_icon-default.png_t_L892_https_blog.csdn.net_qq_38974638_article_details_120145228] [icon-default.png_t_L892]: /images/20220828/a00c937a019b47e7914603603937628a.png [Spring Boot_icon-default.png_t_L892_https_blog.csdn.net_qq_38974638_article_details_120145228]: https://blog.csdn.net/qq_38974638/article/details/120145228
还没有评论,来说两句吧...