문제

Is there a way to make a constantly updating pool of threads and check if any submitted callables has finished and get that value? An ExcutorService allows you to submit a bunch of threads and then get the callable results later, but what if you don't know how many callables will be submitted or you want to continuously submit callables?

ThreadPoolExecutor sounds like what I want, but this can only work on a list of runnables and I need to be able to get results from the threads.

In my case, order does not matter. I want to continuously add callables to some kind of queue and then do something with those results as they come in. Can this be accomplished?

도움이 되었습니까?

해결책

I'm not 100% sure I understand your requirement, but I think you can use a ExecutorCompletionService.

This class wraps an Executor and you can use submit to add tasks, and take or poll to retrieve the next completed task. If there are no completed tasks yet, take will block until one task completes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top