Would this work as an implementation?

    private void waitForAll(Future<Object>[] futures) {
        for (Future<Object> future : futures) {
            try {
                future.get();
            } catch (InterruptedException e) {
                break;
            } catch (Exception ignored) {
            }
        }
    }

The InterruptedException means the main user thread was interrupted, which normally mean the application is shutting down. The other ignored exceptions are caused when the invocation threw an exception or was canceled.

-dain

On Aug 5, 2008, at 11:37 AM, David Blevins wrote:


On Aug 5, 2008, at 5:23 AM, Karan Malhi wrote:

Question:- What would be the use-case to call an asynchronous method and
expect synchronous behavior?

Not *an* asynchronous method (singular) but *multiple* asynchronous methods (plural). Do methods A, B, and C in parallel and wait for them to complete before continuing. The asynchronous methods could each be doing a web service call, for example, that gather data needed to start the real work.

It's essentially what you could do on your own with a CountDownLatch but cannot do reliably as you are not in control of the threads or even the method execution. Passing a CountDownLatch as a parameter to the asynchronous methods you call and having the methods themselves call latch.countDown() isn't going to work as the container can throw exceptions before and after a bean method call. You have no easy way of knowing if the bean method called countDown().

-David


Reply via email to