开发者

limited multithreaded operation

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)   
    Uns开发者_运维问答afe.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    
    ...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜