[concurrency-interest] ThreadPoolExecutor - Concurrent "Batch" Executions

Mike Boyers mboyers at yahoo.com
Wed May 19 23:13:11 EDT 2010


I'm building a service where, in order to generate each response, I'll need to go against several remote sources.  In many cases, the remote requests can be made concurrently.

I'm looking for a super simple interface where developers can execute these remote requests in parallel while being blocked as they execute, with the ability to time out after a specified period.

As a (very) rough idea:

Collection batch = new LinkedList();
batch.add(new RemoteRequest1());
batch.add(new RemoteRequest2());
batch.add(new RemoteRequest3());
Batcher.execute(batch, 5, TimeUnit.SECONDS);

The Batcher.execute call blocks until either all of the tasks are complete or until 10 seconds passes.

I can see how to use a ThreadPoolExecutor to accomplish this, by submitting each Callable then waiting on the Future objects returned upon submit, meanwhile keeping track of the overall amount of time I've waited as I wait on each Future.  While this logic isn't overly complicated, I'd still like to keep it housed and supply developers with something like I outlined above.

But before building this "wrapper", I figure I'd ask to see if there's already something existing I should be looking at, either in the concurrent package or elsewhere (or if I should be thinking about this in a completely different way).

Thanks,
Mike


      


More information about the Concurrency-interest mailing list