@Async多线程在springboot中的使用

1.主类配置@EnableAsync开启多线程配置

@SpringBootApplication
@EnableAsync  //开启异步调用
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

2.方法块添加@Async注解

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    @Async
    @Transactional
    public void sayHello() {
        System.out.println("sayHello当前线程名称:"+Thread.currentThread().getName());
    }
}

3.调用测试

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("当前线程名称:"+Thread.currentThread().getName());
        helloService.sayHello();
        return "hello";
    }


    @Async
    public void async(){
        System.out.println("async当前线程名称:"+Thread.currentThread().getName());
    }
}

 打印结果:

当前线程名称:http-nio-8085-exec-1
async当前线程名称:http-nio-8085-exec-1
2019-12-19 12:15:07.683  INFO 4560 --- [nio-8085-exec-1] .s.a.AnnotationAsyncExecutionInterceptor : No task executor bean found for async processing: no bean of type TaskExecutor and no bean named 'taskExecutor' either
sayHello当前线程名称:SimpleAsyncTaskExecutor-1

结果表明,当调用其他类的被Async注解的方法时,会开启一个新的线程处理,而调自己类的Async注解的方法时,是不会开启一个新的线程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot使用多线程可以使用Java多线程编程的相关类和接口,例如`Thread`、`Runnable`和`Executor`等,并且可以通过Spring的依赖注入特性来管理线程池。 下面是一个示例代码: ```java import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component public class MultiThreadService { @Async // 声明这个方法是一个异步方法,Spring会自动创建一个线程池来执行这个方法 public void doTask1() { // 具体操作 System.out.println("Task 1 is running on thread " + Thread.currentThread().getName()); } @Async public void doTask2() { // 具体操作 System.out.println("Task 2 is running on thread " + Thread.currentThread().getName()); } } ``` 在以上示例,我们在Spring Boot定义了一个`MultiThreadService`类,其定义了两个异步方法`doTask1`和`doTask2`。在这两个方法上添加了`@Async`注解,表示这两个方法是异步执行的。 接下来,在Spring Boot的配置类添加`@EnableAsync`注解,启用异步方法。示例代码如下: ```java import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; @Configuration @EnableAsync // 启用异步方法 public class AsyncConfig { } ``` 最后,在Spring Boot的控制器调用`MultiThreadService`的异步方法即可。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MultiThreadController { @Autowired private MultiThreadService multiThreadService; @GetMapping("/test") public String test() { multiThreadService.doTask1(); multiThreadService.doTask2(); return "Success"; } } ``` 以上代码,我们在控制器调用了`MultiThreadService`的异步方法`doTask1`和`doTask2`,Spring会自动创建一个线程池来执行这两个方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值