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

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

    Best case scenario, as sri said, is to redesign your app to only call the backend once at a time. This avoids this kind of scenario, and preserves bandwidth and latency time. In a web app, this is your most precious resource.

    Having said that the GWT RPC model doesn't really help you to organize things in this manner. I've run into this problem myself. My solution was to implement a timer. The timer will poll your results every X seconds, and when all your expected results are retrieved, your execution flow can continue.

    PollTimer extends Timer
    {
         public PollTimer()
         {
              //I've set to poll every half second, but this can be whatever you'd like.
              //Ideally it will be client side only, so you should be able to make it 
              //more frequent (within reason) without worrying too much about performance
              scheduleRepeating(500);
         }
         public void run 
         {
              //check to see if all your callbacks have been completed
              if (notFinished)
                  return;

          //continue with execution flow
          ...
         }
    

    }

    Make your calls to your RPC, then instantiate a new PollTimer object. That should do the trick.

    The stuff in java.util.concurrent is not supported by GWT Emulation. Wont help you in this case. For all intents and purposes, all of the code you do on the client side is single threaded. Try to get into that mind set.

提交回复
热议问题