Java中的方法来等待多个异步事件完成

Java中的方法来等待多个异步事件完成

本文介绍了清理GWT / Java中的方法来等待多个异步事件完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在继续之前,等待多个异步回调函数以Java完成的最佳方式是什么?具体来说,我使用GWT与AsyncCallback,但我认为这是一个通用的问题。这是我现在拥有的,但肯定有更清晰的方式...

  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);

$ b loginService.login(GWT.getHostPageBaseURL(),new AsyncCallback< LoginInfo>(){
public void onSuccess(LoginInfo result){
appLoaded = true;
ready();
}
});
$ b private void ready(){
if(bookAPIAvailable&&& searchAPIAvailable&& appLoaded){
//所有加载的
}
}


解决方案

项目。基本上,每个回调都会向父级注册。父级等待每个子回调完成,然后触发它自己的handleSuccess()。



客户端代码如下所示:

  

  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);
}



我在这里写了一篇文章解释它:。这两个类的实现是从该帖子链接的(对不起,因为我是一个新手用户,所以不能在这里给出链接 - 没有足够的业力包含多个链接!)。

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
    }
}
解决方案

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!).

这篇关于清理GWT / Java中的方法来等待多个异步事件完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:40