Java parallel work iterator?

前端 未结 3 1400
后悔当初
后悔当初 2021-01-19 02:05

I\'m looking for a class where I can override a method to do the work, and return the results like an iterator. Something like this:

ParallelWorkIterator<         


        
3条回答
  •  萌比男神i
    2021-01-19 02:52

    The closest thing I can think of is to use a CompletionService to accumulate results as they complete.

    Simple example:

    ExecutorService executor = Executors.newSingleThreadExecutor(); // Create vanilla executor service.
    CompletionService completionService = new ExecutorCompletionService(executor); // Completion service wraps executor and is notified of results as they complete.
    Callable callable = new MyCallable();
    
    executor.submit(callable); // Do not store handle to Future here but rather obtain from CompletionService when we *know* the result is complete.
    
    Future fut = completionService.take(); // Will block until a completed result is available.
    Result result = fut.get(); // Will not block as we know this future represents a completed result.
    

    I would not recommend wrapping this behind an Iterator interface as the Future get() method can throw two possible checked exceptions: ExecutionException and InterruptedException, and you would therefore need to catch and either swallow these or rethrow them as RuntimeExceptions, neither of which is a very nice thing to do. In addition, your Iterator's hasNext() or next() methods would potentially need to block if there was a task in progress, which could be considered counterintuitive for clients using an Iterator. Instead I would implement my own more descriptive interface; e.g.

    public interface BlockingResultSet {
      /**
       * Returns next result when it is ready, blocking is required.
       * Returns null if no more results are available.
       */  
      Result take() throws InterruptedException, ExecutionException;
    }
    

    (Methods called take() typically represent a blocking call in the java.util.concurrent package).

提交回复
热议问题