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

前端 未结 8 878
盖世英雄少女心
盖世英雄少女心 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 14:00

    I wrote two classes that solve this problem on my project. Basically, each individual callback registers with a parent. The parent waits for each child callback to complete, then fires off it's own handleSuccess().

    The client code looks like this:

    public void someGwtClientSideMethod() {
        SomeServiceAsync someService = GWT.create(SomeService.class);
        ParallelCallback fooCallback = new ParallelCallback();
        ParallelCallback barCallback = new ParallelCallback();
        ParentCallback parent = new ParentCallback(fooCallback, barCallback) {
            public void handleSuccess() {
                doSomething(getCallbackData(1), getCallbackData(2));
            }
        };
        someService.foo(fooCallback);
        someService.bar(barCallback);
    }
    

    I wrote a post explaining it here: Parallel Asynchronous Calls in GWT. The implementation for these two classes is linked from that post (sorry, can't give links here because I'm a newbie user - not enough karma to include more than one link!).

提交回复
热议问题