Clean way in GWT/Java to wait for multiple asynchronous events to finish

前端 未结 8 866
盖世英雄少女心
盖世英雄少女心 2021-01-30 13:32

What is the best way to wait for multiple asynchronous callback functions to finish in Java before continuing. Specifically I\'m using GWT with AsyncCallback, but I think this i

8条回答
  •  再見小時候
    2021-01-30 13:46

    I did something similar to @Sasquatch, but instead making use of a "CallbackCounter" object:

    public class CallbackCounter {
        private int outstanding;
        private final Callback callback;
        private final String message;
    
        public CallbackCounter(int outstanding, Callback callback, String callbackMessage) {
            this.outstanding = outstanding;
            this.callback = callback;
            this.message = callbackMessage;
        }
    
        public void count() {
            if (--outstanding <= 0) {
                callback.onSuccess(message);
            }
        }
    }
    

    Then in my callback I just call:

    counter.count();
    

提交回复
热议问题