List of usage examples for java.util.concurrent ExecutorCompletionService submit
public Future<V> submit(Callable<V> task)
From source file:MyResult.java
public static void main(String[] args) throws Exception { // Get an executor with three threads in its thread pool ExecutorService exec = Executors.newFixedThreadPool(3); // Completed task returns an object of the TaskResult class ExecutorCompletionService<MyResult> completionService = new ExecutorCompletionService<>(exec); for (int i = 1; i <= 5; i++) { SleepingTask task = new SleepingTask(i, 3); completionService.submit(task); }// w ww . j a va 2s.co m for (int i = 1; i <= 5; i++) { Future<MyResult> completedTask = completionService.take(); MyResult result = completedTask.get(); System.out.println("Completed a task - " + result); } exec.shutdown(); }
From source file:Main.java
public static <V> List<Future<V>> submitTasks(ExecutorCompletionService<V> ecs, Iterable<Callable<V>> tasks) { List<Future<V>> futures = new LinkedList<>(); if (tasks != null) { for (Callable<V> callable : tasks) { futures.add(ecs.submit(callable)); }//from w ww . j a va 2s . c om } return futures; }
From source file:gov.nih.nci.integration.caaers.invoker.CaAERSServiceInvocationStrategyFactory.java
private static synchronized void init(final String[] caaersLibLocation, final String... caaersConfig) { final ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>( Executors.newSingleThreadExecutor()); ecs.submit(new Callable<Boolean>() { @Override//from w ww . j av a 2 s .co m public Boolean call() throws MalformedURLException, BeansException { final CustomClasspathXmlApplicationContext ctx = new CustomClasspathXmlApplicationContext( caaersLibLocation, caaersConfig); caaersRegistrationServiceInvocationStrategy = (ServiceInvocationStrategy) ctx .getBean("caAersRegistrationServiceInvocationStrategy"); caaersUpdateRegistrationServiceInvocationStrategy = (ServiceInvocationStrategy) ctx .getBean("caAersUpdateRegistrationServiceInvocationStrategy"); caaersAdverseEventServiceInvocationStrategy = (ServiceInvocationStrategy) ctx .getBean("caAersAdverseEventServiceInvocationStrategy"); return Boolean.TRUE; } }); try { initStatus = ecs.take().get(); // CHECKSTYLE:OFF } catch (Exception e) { // NOPMD LOG.error("CaAERSServiceInvocationStrategyFactory.Exception inside init(). ", e); initStatus = Boolean.FALSE; } }
From source file:com.jolbox.benchmark.BenchmarkTests.java
/** * Helper function./*from w w w . j ava2 s . com*/ * * @param threads * @param cpds * @param workDelay * @param doPreparedStatement * @return time taken * @throws InterruptedException */ public static long startThreadTest(int threads, DataSource cpds, int workDelay, boolean doPreparedStatement) throws InterruptedException { CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(threads); ExecutorService pool = Executors.newFixedThreadPool(threads); ExecutorCompletionService<Long> ecs = new ExecutorCompletionService<Long>(pool); for (int i = 0; i <= threads; i++) { // create and start threads ecs.submit(new ThreadTesterUtil(startSignal, doneSignal, cpds, workDelay, doPreparedStatement)); } startSignal.countDown(); // START TEST! doneSignal.await(); long time = 0; for (int i = 0; i <= threads; i++) { try { time = time + ecs.take().get(); } catch (ExecutionException e) { e.printStackTrace(); } } pool.shutdown(); return time; }
From source file:com.uber.hoodie.common.util.queue.BoundedInMemoryExecutor.java
/** * Start all Producers//from www .j a v a 2s .com */ public ExecutorCompletionService<Boolean> startProducers() { // Latch to control when and which producer thread will close the queue final CountDownLatch latch = new CountDownLatch(producers.size()); final ExecutorCompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>( executorService); producers.stream().map(producer -> { return completionService.submit(() -> { try { preExecute(); producer.produce(queue); } catch (Exception e) { logger.error("error consuming records", e); queue.markAsFailed(e); throw e; } finally { synchronized (latch) { latch.countDown(); if (latch.getCount() == 0) { // Mark production as done so that consumer will be able to exit queue.close(); } } } return true; }); }).collect(Collectors.toList()); return completionService; }
From source file:com.ironiacorp.http.impl.httpclient3.HttpJobRunnerHttpClient3.java
public void run() { ExecutorService executor = Executors.newFixedThreadPool(maxThreadsCount); ExecutorCompletionService<HttpJob> queue = new ExecutorCompletionService<HttpJob>(executor); List<Future<?>> workers = new ArrayList<Future<?>>(); for (HttpJob job : jobs) { if (HttpMethod.GET == job.getMethod()) { GetRequest request = new GetRequest(httpClient, job); Future<HttpJob> jobStatus = queue.submit(request); workers.add(jobStatus);//w ww .j a v a 2 s . com continue; } if (HttpMethod.POST == job.getMethod()) { PostRequest request = new PostRequest(httpClient, job); Future<HttpJob> jobStatus = queue.submit(request); workers.add(jobStatus); continue; } // TODO: job cannot be handled, what to do? } while (!workers.isEmpty()) { Iterator<Future<?>> i = workers.iterator(); while (i.hasNext()) { try { Future<?> future = i.next(); // future.get(timeout, TimeUnit.MILLISECONDS); future.get(); i.remove(); // } catch (TimeoutException e) { } catch (InterruptedException ie) { System.out.println(ie.getMessage()); } catch (ExecutionException ee) { System.out.println(ee.getMessage()); i.remove(); } } } executor.shutdown(); }
From source file:com.alibaba.otter.manager.biz.monitor.impl.GlobalMonitor.java
private void concurrentProcess(List<Long> channelIds) { ExecutorCompletionService completionExecutor = new ExecutorCompletionService(executor); List<Future> futures = new ArrayList<Future>(); for (final Long channelId : channelIds) { futures.add(completionExecutor.submit(new Callable<Object>() { @Override/* www . ja v a 2 s.c o m*/ public Object call() throws Exception { ChannelStatus status = arbitrateManageService.channelEvent().status(channelId); if (status.isPause()) { restartAlarmRecovery.recovery(channelId); } return null; } })); } List<Throwable> exceptions = new ArrayList<Throwable>(); int index = 0; int size = futures.size(); while (index < size) { try { Future<?> future = completionExecutor.take(); future.get(); } catch (InterruptedException e) { exceptions.add(e); } catch (ExecutionException e) { exceptions.add(e); } index++; } if (!exceptions.isEmpty()) { StringBuilder sb = new StringBuilder(exceptions.size() + " exception happens in global monitor\n"); sb.append("exception stack start :\n"); for (Throwable t : exceptions) { sb.append(ExceptionUtils.getStackTrace(t)); } sb.append("exception stack end \n"); throw new IllegalStateException(sb.toString()); } }
From source file:biz.c24.io.spring.integration.samples.fpml.PreRenderingFpmlGenerator.java
private List<Generator> preRender() throws Exception { List<Generator> result = new ArrayList<Generator>(THREADS); final TradeConfirmed tradeConfirmed = readTradeConfirmed(); ExecutorCompletionService<Generator> completionService = new ExecutorCompletionService<Generator>( Executors.newFixedThreadPool(THREADS)); for (int i = 0; i < THREADS; i++) { completionService.submit(new Callable<Generator>() { public Generator call() throws Exception { System.out.println("Rendering... "); OutputType ot = OutputType.BYTE_ARRAY; Random rand = new Random(); TradeConfirmed myTradeConfirmed = (TradeConfirmed) tradeConfirmed.cloneDeep(); Generator gen = new Generator(); List<Object> payloads = new ArrayList<Object>(ITERATIONS); for (int j = 0; j < ITERATIONS; j++) { TradeConfirmed fpML = randomizeFpML(myTradeConfirmed); if (rand.nextInt(100) == 0) { breakFpml(fpML); }//from w w w . j av a2 s . com Sink sink = ot.getSink(sinkFactory); sink.writeObject(fpML); Object payload = ot.getOutput(sink); payloads.add(payload); } gen.payloads = payloads; return gen; } }); } for (int i = 0; i < THREADS; i++) { Future<Generator> future = completionService.take(); result.add(future.get()); } return result; }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessMutexBase.java
@Test public void testKilledSession() throws Exception { final Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start();//from ww w. j av a 2s . com try { final InterProcessLock mutex1 = makeLock(client); final InterProcessLock mutex2 = makeLock(client); final Semaphore semaphore = new Semaphore(0); ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>( Executors.newFixedThreadPool(2)); service.submit(new Callable<Object>() { @Override public Object call() throws Exception { mutex1.acquire(); semaphore.release(); Thread.sleep(1000000); return null; } }); service.submit(new Callable<Object>() { @Override public Object call() throws Exception { mutex2.acquire(); semaphore.release(); Thread.sleep(1000000); return null; } }); Assert.assertTrue(timing.acquireSemaphore(semaphore, 1)); KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString()); Assert.assertTrue(timing.acquireSemaphore(semaphore, 1)); } finally { client.close(); } }
From source file:com.netflix.curator.framework.recipes.barriers.TestDistributedDoubleBarrier.java
@Test public void testOverSubscribed() throws Exception { final Timing timing = new Timing(); final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); ExecutorService service = Executors.newCachedThreadPool(); ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(service); try {/* www.j a v a 2 s .c o m*/ client.start(); final Semaphore semaphore = new Semaphore(0); final CountDownLatch latch = new CountDownLatch(1); for (int i = 0; i < (QTY + 1); ++i) { completionService.submit(new Callable<Void>() { @Override public Void call() throws Exception { DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier", QTY) { @Override protected List<String> getChildrenForEntering() throws Exception { semaphore.release(); Assert.assertTrue(timing.awaitLatch(latch)); return super.getChildrenForEntering(); } }; Assert.assertTrue(barrier.enter(timing.seconds(), TimeUnit.SECONDS)); Assert.assertTrue(barrier.leave(timing.seconds(), TimeUnit.SECONDS)); return null; } }); } Assert.assertTrue(semaphore.tryAcquire(QTY + 1, timing.seconds(), TimeUnit.SECONDS)); // wait until all QTY+1 barriers are trying to enter latch.countDown(); for (int i = 0; i < (QTY + 1); ++i) { completionService.take().get(); // to check for assertions } } finally { service.shutdown(); IOUtils.closeQuietly(client); } }