020 非同步任務

來源:互聯網
上載者:User

標籤:環境   err   void   trace   ring   ons   task   阻塞   catch   

一 .概述

  在我們的環境下,我們常常會建立一些非同步任務,我們最大的想法就是不想讓耗時的任務阻塞我們方法的運行.

 

二 .使用方式

  在springboot之中使用非同步任務變得非常的簡單,我們只需要兩步就能完成

[1] 開啟非同步

[2]定義非同步方法呼叫

下面看看我們的一個示範例子:

我們只需要在我們的主啟動類上添加一個啟動非同步任務的註解  

@SpringBootApplication@EnableAsyncpublic class SpringbootApplicationStarter {        public static void main(String[] args) {        SpringApplication.run(SpringbootApplicationStarter.class, args);    }}
@Servicepublic class AsyncTask {        @Async    public void async()  {        try {            TimeUnit.SECONDS.sleep(3);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                System.out.println("任務完成");    }}

在我們的業務代碼上,添加一個非同步任務的註解就可以了.

020 非同步任務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.