標籤: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); }
關於回呼函數