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

Posted by gerdemb on Stack Overflow See other posts from Stack Overflow or by gerdemb
Published on 2010-06-07T20:55:16Z Indexed on 2010/06/07 21:12 UTC
Read the original article Hit count: 181

Filed under:
|
|

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 is a generic problem. Here's what I have now, but surely there is cleaner way...

    AjaxLoader.loadApi("books", "0", new Runnable(){
        public void run() {
            bookAPIAvailable = true;
            ready();
        }}, null);
    AjaxLoader.loadApi("search", "1", new Runnable(){
        public void run() {
            searchAPIAvailable = true;
            ready();
        }}, null);


    loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
        public void onSuccess(LoginInfo result) {
            appLoaded  = true;
            ready();
        }
    });

private void ready() {
    if(bookAPIAvailable && searchAPIAvailable && appLoaded) {
                // Everything loaded
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about gwt