崛起於Springboot2.X + 100秒掌握異步任務(55)

《SpringBoot2.X心法總綱》app

一、應用場景

      若是以爲消息隊列太繁瑣了,對於異步發送短信、郵件、app消息推送,等等均可以使用簡單的異步來解決。異步

二、異步配置

      Springboot啓動類添加註解以下,表示開啓,會自動進行掃描。async

@EnableAsync

      異步任務類添加註解測試

@Async
@Component

三、編寫異步Task

@Async
@Component
public class AsyncTask {

    public Future<String> task1() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();

        System.out.println("異步任務async1:耗時="+(end-begin));

        return new AsyncResult<String>("任務1");
    }

    public Future<String> task2() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();

        System.out.println("異步任務async2:耗時="+(end-begin));

        return new AsyncResult<String>("任務2");
    }

    public Future<String> task3() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();

        System.out.println("異步任務async3:耗時="+(end-begin));

        return new AsyncResult<String>("任務3");
    }
}

四、編寫同步Task

@Component
public class SyncTask {

    public Boolean task1() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();

        System.out.println("同步任務sync1:耗時="+(end-begin));

        return true;
    }

    public Boolean task2() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();

        System.out.println("同步任務sync2:耗時="+(end-begin));

        return true;
    }

    public boolean task3() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();

        System.out.println("同步任務sync3:耗時="+(end-begin));

        return true;
    }
}

五、controller

@RestController
public class TestController {

    @Autowired
    AsyncTask asyncTask;

    @Autowired
    SyncTask syncTask;

    @GetMapping(value = "/testTask")
    public String testTask(int type) throws InterruptedException {

        // type:一、異步任務測試 二、同步任務測試
        long begin = System.currentTimeMillis();
        if (type == 1) {
            Future<String> async1 = asyncTask.task1();
            Future<String> async2 = asyncTask.task2();
            Future<String> async3 = asyncTask.task3();

            //若是都執行往就能夠跳出循環,isDone方法若是此任務完成,true
            while (true){
                if (async1.isDone() && async2.isDone() && async3.isDone()) {
                    break;
                }
            }
        } else {
            boolean sync1 = syncTask.task1();
            boolean sync2 = syncTask.task2();
            boolean sync3 = syncTask.task3();
            //若是都執行往就能夠跳出循環,isDone方法若是此任務完成,true
            while (true){
                if (sync1 && sync2 && sync3) {
                    break;
                }
            }
        }
        long end = System.currentTimeMillis();

        long total = end-begin;
        return "執行總耗時="+total;

    }
}

六、測試接口

異步任務測試:http://localhost:8080/testTask?type=1 spa

結果:.net

同步任務測試:http://localhost:8080/testTask?type=2blog

結果:接口

七、總結

      綜上所看,異步爲3秒左右,同步爲6秒,故此異步任務成功。異步任務須要用Future<String>返回結果AsyncResult<String>("")。隊列

相關文章
相關標籤/搜索