[concurrency-interest] CompletionService and I/O - can I use it?

David Harrigan dharrigan at gmail.com
Fri Oct 27 09:37:55 EDT 2006


Another posting ;) I appreciate all the help I'm getting in this to further
my (and maybe other's) understanding.

Rethinking my problem, I have decided to take the advice and use a
CompletionService to hand off the searchers, and to wait for their results,
so I have something like this:

private final CompletionService<Searcher> ecs =

public void search(final List<Searcher> searchers) {
    for(Searcher searcher : searchers) {

    for(int i = 0, j = searchers.size() ; i < j ; i++) {
        try {
            Future<Searcher> result = ecs.poll(1000, TimeUnit.MILLISECONDS);
            if(result != null) {
              Searcher s = result.get();
              // do something with s now...
        } catch(InterruptedException e) { 
        } catch(ExecutionException e) {

This I think gives me the ability to poll each searcher for 1 second. But I
have two

1. A searcher makes a connection to the outside world, this could take more
than 1
second, or it could take less than 1 second. If it takes more than 1 second,
fine, then
it's dead and I don't care about it, but say it makes a connection within 1
second, but
then takes another 2 seconds to retrieve the results, the method above will
the searcher and I won't be able to do anything with it. What I would
ideally like is
the ability to poll for the searcher, then if it's doing something, to wait
for it to 
complete. Can I somehow shove it back on the queue? Or to "peek" inside the
queue to see if my searcher is doing anything interesting?

2. Perhaps using a CompletionService isn't appropriate in this case? You
see, I have
3 states a search can move thru:

Connected but not reading

each of those states could timeout. I can control the first one, but I need
to be
able to pause acting on the searcher if it's reading for another bit of
time, but
eventually to timeout if reading is taking too long...

Has anyone else come across this situation (it must be common! - it's a
I/O read/wait time of situation) and using threads to control it? I would
any help/advice on this.


View this message in context: http://www.nabble.com/CompletionService-and-I-O---can-I-use-it--tf2520636.html#a7030419
Sent from the JSR166 Concurrency mailing list archive at Nabble.com.

More information about the Concurrency-interest mailing list