關於回呼函數

來源:互聯網
上載者:User

標籤:sendto   過程   table   dom   wait   tty   art   style   server   

49359923/

這篇文章

在Android的學習過程中經常會聽到或者見到“回調”這個詞,那麼什麼是回調呢?所謂的回呼函數就是:在A類中定義了一個方法,這個方法中用到了一個介面和該介面中的抽象方法,但是抽象方法沒有具體的實現,需要B類去實現,B類實現該方法後,它本身不會去調用該方法,而是傳遞給A類,供A類去調用,這種機制就稱為回調。
---------------------
衷水木
來源:CSDN
原文:49359923?utm_source=copy
著作權聲明:本文為博主原創文章,轉載請附上博文連結!

這是作者對回調的理解

總覺得差點意思,大概自己理解不深,所以也說不上來,

是我在工作中做的一個類似的,調用方法處是接收了來自用戶端的請求,然後像netty通道寫訊息,又要同步在限定時間內等待client返回,

業務代碼需要傳入自己的請求,時間限定,以及對按時返回和逾時返回的處理,是不是也是一種回調處理呢。

if (null != channel) {      NettyServerUtils.sendToDev(channel, GsonUtil.object2Gson(centralPlatformReq));      waitingThreadPool.newWaitingThread(          centralPlatformReq,          WaitingThreadPool.WAIT_5_SECONDS,          new HandleOntime() {            @Override            public void handle() {}          },          new HandleOvertime() {            @Override            public void handle() {}          });    }

 

public void newWaitingThread(      CentralPlatformReq req,      long waitingTime,      HandleOntime handleOntime,      HandleOvertime handleOvertime) {    String uuid = UUID.randomUUID().toString();    CountDownLatch latch = newReq(req, uuid);    // 發送請求完畢,同步等待五秒    CompletableFuture<Integer> completableFuture =        CompletableFuture.supplyAsync(            () -> {              try {                if (latch.await(waitingTime, TimeUnit.MILLISECONDS)) {                  // 5s內返回了                  handleOntime.handle();                } else {                  // 逾時                  handleOvertime.handle();                }              } catch (InterruptedException e) {                e.printStackTrace();              }              return 1;            },            WaitingThreadPool.executor);    reqEnd(uuid);  }

 

關於回呼函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.