zoukankan      html  css  js  c++  java
  • java spring boot 异步方法@Async

    java spring boot 异步方法@Async

    ps:我这里要说明下 这个异步是多线程 所以要考虑线程安全 变量共享的问题

    1 首先开启类 

    @EnableAsync //开启异步调用



    2 直接方法前面加
    @Async 就可以异步了
    package com.example.demo2122;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import javax.annotation.Resource;
    
    import java.util.*;
    import java.util.stream.IntStream;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    @RestController
    @Component
    public class HelloControl {
    
        @GetMapping("/hello")
        public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
            System.out.println("####IndexController####   1");
            this.sendSms();
            System.out.println("####IndexController####   3");
            return "success";
        }
    
    
    
        @Async
        public void sendSms(){
            Timer timer = new Timer();
            timer.schedule(new RemindTask(), 3*1000);
        }
    
    }
    class RemindTask extends TimerTask {
        public void run() {
            System.out.println("3秒后执行sendSms");
    
        }
    }
  • 相关阅读:
    数位DP入门
    划分树
    CodeForces #362 div2 B. Barnicle
    CodeForces #363 div2 Vacations DP
    CodeForces #368 div2 D Persistent Bookcase DFS
    解决Ubuntu 下 vi编辑器不能使用方向键和退格键问题
    python之爬虫爬有道词典
    hdu 5145 NPY and girls 莫队
    hdu 6185 Covering 矩阵快速幂
    字典树求异或值
  • 原文地址:https://www.cnblogs.com/newmiracle/p/12750788.html
Copyright © 2011-2022 走看看