Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top