views:

32

answers:

1

I have a procedure (procA) which needs to call a webservice many times and then return the set of results.

procA may be called a handful of times concurrently.

the calls to the webservice take a set amount of time, however concurrent calls do not greatly impact the performance.

therefore it would be beneficial to create a thread pool for procA to make the calls to the webservice so that a single call to procA can que up all of it's calls to the webservice and then wait until they are all finished before continuing.

however i do not want to have a thread pool per procA because if there are multiple concurrent calls to procA, i want to limit/throttle the total number of threads accessing the webservice at once.

the optimum solution would be a shared thread pool which each call to procA shares.

the only problem i need help solving is how do i tell that all of the webservice tasks queued by the first call to procA are finished?

EDIT: stack trace

Daemon Thread [http-80-4] (Suspended)   
    Unsafe.park(boolean, long) line: not available [native method]  
    LockSupport.park(Object) line: 158  
    FutureTask$Sync(AbstractQueuedSynchronizer).parkAndCheckInterrupt() line: 747   
    FutureTask$Sync(AbstractQueuedSynchronizer).doAcquireSharedInterruptibly(int) line: 905 
    FutureTask$Sync(AbstractQueuedSynchronizer).acquireSharedInterruptibly(int) line: 1217  
    FutureTask$Sync.innerGet() line: 218    
    FutureTask<V>.get() line: 83    
    ThreadPoolExecutor(AbstractExecutorService).invokeAll(Collection<Callable<T>>) line: 205    
    ...
+3  A: 

The easiest thing is to use a proper ExecutorService and just use the invokeAll method. Your jobs run asynchronously, and the calling method blocks until all are complete.

Alternately you could have each instance of procA gather up a collection of the Future objects that go with the work it submitted to the executor. (Or do it in method scope if appropriate.) Then iterate over them and block on their get() methods. When the loop is done, the work is done.

Affe
not bad, i'll try it out!
pstanton
re edit, event better! will give the tick once i've tested.
pstanton
this works, however it seems to get stuck at invokeAll occasionally, see my edit for a stack trace of the thread..
pstanton
icki, that shouldn't happen! Maybe something is blocking up inside the httpclient library under the increased load? Is there a timeout set for the web service to respond inside the Callable? You could set some absurdly high (relative to normal task time) timeout on the invokeAll call to be able to have it keep going and take a look at where it was/try to recover.
Affe
i tried again last night with 50000 calls on a pool of 10 threads. it's clear (as you suggest) that it is in this case 2 of the threads are hung on the webservice api. i'll have to look at resolving that, but in terms of the multithreading, this solution works great. thanks.
pstanton