springboot异步方法

本文介绍了在SpringBoot中如何创建并使用异步方法。通过示例展示了同步方法与异步方法的区别,异步方法的使用可以避免主线程阻塞,提高程序执行效率。在开启异步方法后,配合线程池配置,展示了异步调用的不同顺序,强调了异步执行的非确定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同步方法

  1. 创建一个service
@Component
public class AsyncService {

    public void test1(){
        //打印线程name
        System.out.println("test1 start:" + Thread.currentThread().getName());
        //模拟程序执行
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("test1 end");
    }

    public void test2(){
        System.out.println("test2 start:" + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("test2 end");
    }

    public void test3(){
        System.out.println("test3 start:" + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("test3 end");
    }
}
  1. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class AsyncApplicationTests {
	@Autowired
	private AsyncService asyncService;

	@Test
	public void testAsync() {
        asyncService.test1();
        asyncService.test2();
        asyncService.test3();
    }

}

控制台打印:
test1 start:main
test1 end
test2 start:main
test2 end
test3 start:main
test3 end

异步方法

  1. 开启异步方法
@SpringBootApplication
//开启异步方法
@EnableAsync
public class AsyncApplication {

	public static void main(String[] args) {
		SpringApplication.run(AsyncApplication.class, args);
	}

}
  1. 设置线程池(非必须)
@Configuration
public class BeanLoad {

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("springboot-");
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(10);

        executor.initialize();
        return executor;
    }
}
  1. 创建异步方法
@Component
public class AsyncService {

    //@Async表示方法是一个异步方法
    @Async
    public void test1(){
        //和同步方法一样
    }

    @Async
    public void test2(){
        //和同步方法一样
    }

    @Async
    public void test3(){
        //和同步方法一样
    }
}
  1. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class AsyncApplicationTests {
	@Autowired
	private AsyncService asyncService;

	@Test
	public void testAsync() {
        asyncService.test1();
        asyncService.test2();
        asyncService.test3();
        //避免主线程结束出现错误
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

控制台打印:
test3 start:springboot-3
test2 start:springboot-2
test1 start:springboot-1
test2 end
test3 end
test1 end

每次打印的结果顺序都可能不一样

项目路径


作者博客

作者公众号
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值