0

I am using Callable, Executor and Future objects to invoke webservices in parallel from separate threads. After having executed all my webservice calls, I display the results in jsp.

Here I have a doubt: if one of the webservice calls fails, how can I show the results from the successful calls in jsp?

If one thread takes more time to execute in that situation I want to skip that response and show the rest of the results in jsp.

I tried to add a Thread.sleep(1000) in one webserice but the response is also delayed in jsp.

assylias
  • 321,522
  • 82
  • 660
  • 783
rev
  • 77
  • 1
  • 8

3 Answers3

2

You may want to consider CountdownLatch in the main thread that starts all other threads.

The idea is that after you spawn all threads, you start waiting on the latch with given timeout. Each thread, as it finishes, will have to call countDown() method on the latch. So the main thread will proceed when all threads are done or timeout has expired.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
0

If you are using an executor service, you can terminate it with a timeout after having submitted all you callables:

executor.shutdown();
boolean terminated = executor.awaitTermination(1, TimeUnit.SECONDS);

And if you have stored the futures returned by the executor when submitting the jobs, you can loop over the list and check which task has not been completed:

if (!terminated) {
    for (Future f : futureList) {
        if (!f.isDone()) { System.out.println("This task " + f + " did not complete"); }
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • @JohnVint Any reasons why you deleted your answer? – assylias Jul 02 '12 at 17:58
  • Hi assylias is i have to write above code after executer.shutdown(). if yes before excute.shotdown i am submitting my Task, it takng more time my question is if it takes more than 60 secs how can i cancel that task? – rev Jul 02 '12 at 17:58
  • Yes: call shutdown & awaitTermination, if terminated is true, everything was finished, if not you still have some tasks running. You can then browse the futures and call `future.cancel(true)` to cancel the tasks that are still running. Alternatively, you can call `executor.shutdownNow()` after `awaitTermination` to cancel all the tasks that are still running. – assylias Jul 02 '12 at 18:01
  • after executer.shutdown all pooled threads will terminate right why again above code? my question is i have two threads calling 2 different webservice methods , if one these take more time to execute the webservice , then i have to stop that thread and have to display success tread response in jsp? how can terminate a thread it not procses request with in specified time?i am new to concurrent programming sorry if wrote any mistake – rev Jul 02 '12 at 18:11
  • no, executor.shutdown() tells the executor not to accept new tasks but does not cancel the tasks that have already been submitted. You need to call `shutdownNow` to cancel the remaining tasks. – assylias Jul 02 '12 at 18:13
  • @rev I have added another answer which might be helpful for you. Need to leave now - good luck! – assylias Jul 02 '12 at 18:17
0

Someone posted this and deleted his answer but I thought it could be helpful.


You can use an ExecutorCompletionService

You can simply submit to the ExecutorService and when you are ready to render poll on the ExecutorService. It will give you the most recently completed future or sit there and wait until one is ready.

ExecutorCompletionService e = new ExecutorCompletionService(...);

for(int i =0; i < n ; i++)
    e.submit(..);

While you are ready

for(int i =0 ; i < n; i++){
   T t = e.poll(); //or with a timeout: T t = e.poll(100, TimeUnit.MILLISECONDS); 
   //do rendering
}

Should account for your use case.

assylias
  • 321,522
  • 82
  • 660
  • 783