|
Re: CompletionService and I/O - can I use it?: msg#00072java.jsr.166-concurrency
A completion service is appropriate if you want to get results from tasks as they complete (either by having a result or by throwing an exception). The completion service returns completed tasks; the timed poll method waits the given duration for any task to become available by completing, not one particular task. You are submitting Searcher instances to a CompletionService<Searcher>, which means that Searcher must implement Callable<Searcher>. This could be part of a design where the call method of the Searcher establishes the connection and reads some data, throwing an exception if either of these activities take too long. Otherwise, if connection and reading complete successfully, the call method returns the searcher itself. Then a sequence of ready-to-use Searchers could be obtained by calling the following method in a loop: Searcher getNextReadySearcher(long deadlineNanos) throws TimeoutException, InterruptedException { while (true) { try { long remainingNanos = deadlineNanos - System.nanoTime(); if (remainingNanos > 0) { Future<Searcher> f = ecs.poll(remainingNanos, TimeUnit.NANOSECONDS); if (f != null) return f.get(); } throw new TimeoutException("deadline passed"); } catch (ExecutionException e) { // could resubmit if exception refers to Searcher if (e.getCause() instanceof SearcherException) { SearcherException se = (SearcherException) e.getCause(); ecs.submit(se.getSearcher()); } // retry loop } } } For an example of doing work with a time budget, see Section 6.3.7 of Java Concurrency in Practice. For an example of nonstandard cancellation within the task execution framework, see Section 7.1.7. --tim
On 10/27/06,
David Harrigan <dharrigan@xxxxxxxxx> wrote:
_______________________________________________ Concurrency-interest mailing list Concurrency-interest@xxxxxxxxxxxxxxxxxxxx http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Volatile field use cases?, Hanson Char |
|---|---|
| Next by Date: | proper handling of InterruptedException, Moran Avigdor |
| Previous by Thread: | CompletionService and I/O - can I use it?, David Harrigan |
| Next by Thread: | proper handling of InterruptedException, Moran Avigdor |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |