SpringBoot开启子线程执行任务

目录

一、@EnableAsync

二、@Async

三、测试


一、@EnableAsync

二、@Async

@Service
public class IotLocationServiceImpl {
    @Async
    public void testA() {
        try {
            // 模拟阻塞
            Thread.sleep(5000);
            System.out.println("子线程执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

三、测试

@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    private final IotLocationServiceImpl iotLocationService;

    @PostMapping("/test")
    public Result<?> test() {
        iotLocationService.testA();
        return Result.success();
    }
}

猜你喜欢

转载自blog.csdn.net/wenxingchen/article/details/131632792