Refine search
final ExecutorService pool = Executors.newFixedThreadPool(2); final CompletionService<String> service = new ExecutorCompletionService<String>(pool); final List<? extends Callable<String>> callables = Arrays.asList( new SleepingCallable("slow", 5000), new SleepingCallable("quick", 500)); for (final Callable<String> callable : callables) { service.submit(callable); } pool.shutdown(); try { while (!pool.isTerminated()) { final Future<String> future = service.take(); System.out.println(future.get()); } } catch (ExecutionException | InterruptedException ex) { }
/** * @return a map with successful lookups */ private Map<String, LookupExtractorFactoryContainer> startLookups( List<LookupBean> lookupBeans, CompletionService<Map.Entry<String, LookupExtractorFactoryContainer>> completionService ) throws InterruptedException { for (LookupBean lookupBean : lookupBeans) { completionService.submit(() -> startLookup(lookupBean)); } Map<String, LookupExtractorFactoryContainer> successfulLookups = new HashMap<>(); for (int i = 0; i < lookupBeans.size(); i++) { Future<Map.Entry<String, LookupExtractorFactoryContainer>> completedFuture = completionService.take(); try { Map.Entry<String, LookupExtractorFactoryContainer> lookupResult = completedFuture.get(); if (lookupResult != null) { successfulLookups.put(lookupResult.getKey(), lookupResult.getValue()); } } catch (ExecutionException e) { LOG.error(e.getCause(), "Exception while starting a lookup"); // not adding to successfulLookups } } return successfulLookups; }
@Override protected void collectReports(List<Renderer> renderers) { try { for (int i = 0; i < submittedTasks; i++) { final Report report = completionService.take().get(); super.renderReports(renderers, report); } } catch (final InterruptedException ie) { Thread.currentThread().interrupt(); } catch (final ExecutionException ee) { final Throwable t = ee.getCause(); if (t instanceof RuntimeException) { throw (RuntimeException) t; } else if (t instanceof Error) { throw (Error) t; } else { throw new IllegalStateException("PmdRunnable exception", t); } } finally { executor.shutdownNow(); } } }
@Override boolean executeCloseTask(CompletionService<Void> completionService, List<IOException> thrown, List<Path> paths) throws InterruptedException, ExecutionException { for (final Map.Entry<byte[], RegionEntryBuffer> buffer : entryBuffers.buffers.entrySet()) { LOG.info("Submitting writeThenClose of {}", Arrays.toString(buffer.getValue().encodedRegionName)); completionService.submit(new Callable<Void>() { @Override public Void call() throws Exception { Path dst = writeThenClose(buffer.getValue()); paths.add(dst); return null; } }); } boolean progress_failed = false; for (int i = 0, n = entryBuffers.buffers.size(); i < n; i++) { Future<Void> future = completionService.take(); future.get(); if (!progress_failed && reporter != null && !reporter.progress()) { progress_failed = true; } } return progress_failed; }
LOG.trace("Submitting close of " + ((WriterAndPath) writersEntry.getValue()).p); completionService.submit(new Callable<Void>() { @Override public Void call() throws Exception { WriterAndPath wap = (WriterAndPath) writersEntry.getValue(); Future<Void> future = completionService.take(); future.get(); if (!progress_failed && reporter != null && !reporter.progress()) { progress_failed = true;
ExecutorService executor = Executors.newFixedThreadPool(maxThreadsToUse); CompletionService completion = new ExecutorCompletionService(executor); for (each sub task) { completion.submit(new SomeTaskYouCreate()) } // wait for all tasks to complete. for (int i = 0; i < numberOfSubTasks; ++i) { completion.take(); // will block until the next sub task has completed. } executor.shutdown();
taskCompletionService.submit(new CallableTask( String.valueOf(i), (i * 10), try { System.out.println("trying to take from Completion service"); Future<CalcResult> result = taskCompletionService.take(); System.out.println("result for a task availble in queue.Trying to get()" ); CalcResult l = result.get(); System.out.println("Task " + String.valueOf(tasksHandled) + "Completed - results obtained : " + String.valueOf(l.result));