logo       

CompletionService and I/O - can I use it?: msg#00070

java.jsr.166-concurrency

Subject: CompletionService and I/O - can I use it?


Hi,

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 =
ExecutorCompletionService<Searcher>(Executors.newCachedThreadPool());

public void search(final List<Searcher> searchers) {
for(Searcher searcher : searchers) {
ecs.submit(searcher);
}

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) {
Thread.currentThread().interrupt();
} catch(ExecutionException e) {
e.printStackTrace();
}
}

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

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
timeout
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
Reading
Finished

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
basic
I/O read/wait time of situation) and using threads to control it? I would
appreciate
any help/advice on this.

-=david=-

--
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.


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise