List of usage examples for java.util.concurrent ExecutorCompletionService ExecutorCompletionService
public ExecutorCompletionService(Executor executor)
From source file:org.apache.tinkerpop.gremlin.tinkergraph.process.computer.TinkerWorkerPool.java
public TinkerWorkerPool(final int numberOfWorkers) { this.numberOfWorkers = numberOfWorkers; this.workerPool = Executors.newFixedThreadPool(numberOfWorkers, THREAD_FACTORY_WORKER); this.completionService = new ExecutorCompletionService<>(this.workerPool); }
From source file:org.apache.hadoop.chukwa.dataloader.MetricDataLoaderPool.java
public void load(ChukwaConfiguration conf, FileSystem fs, FileStatus[] fileList) throws IOException { if (executor == null) { try {//www .j a v a2s . com this.size = Integer.parseInt(conf.get(DATA_LOADER_THREAD_LIMIT)); } catch (Exception e) { this.size = 1; } executor = Executors.newFixedThreadPool(size); } if (completion == null) { completion = new ExecutorCompletionService(executor); } try { for (int i = 0; i < fileList.length; i++) { String filename = fileList[i].getPath().toUri().toString(); log.info("Processing: " + filename); completion.submit(new MetricDataLoader(conf, fs, filename)); } for (int i = 0; i < fileList.length; i++) { completion.take().get(); } } catch (Exception e) { log.error(ExceptionUtil.getStackTrace(e)); throw new IOException(); } finally { } }
From source file:com.cognifide.qa.bb.junit.concurrent.ConcurrentSuiteRunnerScheduler.java
/** * Constructs ConcurrentSuiteRunnerScheduler. * * @param clazz/*from w w w.j a va 2 s.c om*/ * @param properties * @param reportingHandler */ ConcurrentSuiteRunnerScheduler(Class<?> clazz, Properties properties, ReportingHandler reportingHandler, WebDriverRegistry webDriverRegistry) { this.properties = properties; this.reportingHandler = reportingHandler; this.webDriverRegistry = webDriverRegistry; executorService = Executors.newFixedThreadPool(determineNumberOfThreads(clazz), new NamedThreadFactory(clazz.getSimpleName())); completionService = new ExecutorCompletionService<>(executorService); }
From source file:cn.clxy.upload.UploadFileService.java
private void doUpload() { listener.onStart(indexes != null ? indexes.size() : getPartCount()); parts = new ArrayBlockingQueue<Part>(Config.maxRead); CompletionService<String> cs = new ExecutorCompletionService<String>(executor); cs.submit(readTask);//ww w . j a va2s . c o m for (int i = 0; i < Config.maxUpload; i++) { cs.submit(new UploadTask("upload." + i)); } // Wait all done. total count = maxUpload + 1. for (int i = 0; i <= Config.maxUpload; i++) { Future<String> future = null; try { future = cs.take(); checkFuture(future); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } // Notify sever all done. Future<String> result = executor.submit(notifyTask); checkFuture(result); listener.onSuccess(); }
From source file:com.teradata.benchto.driver.jdbc.ConnectionPoolTest.java
private void openGivenConnectionsAmountSimultaneously(String dataSourceName, int connectionsCount) throws SQLException, InterruptedException, TimeoutException { ExecutorService executorService = newFixedThreadPool(connectionsCount); ExecutorCompletionService<?> completionService = new ExecutorCompletionService(executorService); CountDownLatch countDownLatch = new CountDownLatch(connectionsCount); DataSource dataSource = applicationContext.getBean(dataSourceName, DataSource.class); range(0, connectionsCount).mapToObj(i -> createQueryRunnable(dataSource, countDownLatch)) .forEach(completionService::submit); try {// ww w . j ava 2 s . c o m for (int i = 0; i < connectionsCount; i++) { try { Future<?> future = completionService.take(); future.get(1, MINUTES); } catch (ExecutionException e) { rethrowException(e.getCause()); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { executorService.shutdownNow(); executorService.awaitTermination(1, MINUTES); } }
From source file:com.genentech.chemistry.openEye.apps.SDFMDLSSSMatcher.java
public SDFMDLSSSMatcher(String qFile, String outFile, boolean firstMatch, boolean printAll, int nCpu) { this.firstMatch = firstMatch; this.printAll = printAll; this.matcher = new MDLSSSMatcher(qFile); this.ofs = new oemolothread(outFile); this.nCpu = nCpu; this.executor = Executors.newFixedThreadPool(nCpu); this.completionService = new ExecutorCompletionService<Boolean>(executor); finder = new MDLSSSFind[nCpu]; }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphoreCluster.java
@Test public void testKilledServerWithEnsembleProvider() throws Exception { final int CLIENT_QTY = 10; final Timing timing = new Timing(); final String PATH = "/foo/bar/lock"; ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY); ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService); TestingCluster cluster = new TestingCluster(3); try {// w ww. j ava 2s .com cluster.start(); final AtomicReference<String> connectionString = new AtomicReference<String>( cluster.getConnectString()); final EnsembleProvider provider = new EnsembleProvider() { @Override public void start() throws Exception { } @Override public String getConnectionString() { return connectionString.get(); } @Override public void close() throws IOException { } }; final Semaphore acquiredSemaphore = new Semaphore(0); final AtomicInteger acquireCount = new AtomicInteger(0); final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY); for (int i = 0; i < CLIENT_QTY; ++i) { completionService.submit(new Callable<Void>() { @Override public Void call() throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider) .sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()) .retryPolicy(new ExponentialBackoffRetry(100, 3)).build(); try { final Semaphore suspendedSemaphore = new Semaphore(0); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) { suspendedLatch.countDown(); suspendedSemaphore.release(); } } }); client.start(); InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1); while (!Thread.currentThread().isInterrupted()) { Lease lease = null; try { lease = semaphore.acquire(); acquiredSemaphore.release(); acquireCount.incrementAndGet(); suspendedSemaphore.acquire(); } catch (Exception e) { // just retry } finally { if (lease != null) { acquireCount.decrementAndGet(); IOUtils.closeQuietly(lease); } } } } finally { IOUtils.closeQuietly(client); } return null; } }); } Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore)); Assert.assertEquals(1, acquireCount.get()); cluster.close(); timing.awaitLatch(suspendedLatch); timing.forWaiting().sleepABit(); Assert.assertEquals(0, acquireCount.get()); cluster = new TestingCluster(3); cluster.start(); connectionString.set(cluster.getConnectString()); timing.forWaiting().sleepABit(); Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore)); timing.forWaiting().sleepABit(); Assert.assertEquals(1, acquireCount.get()); } finally { executorService.shutdown(); executorService.awaitTermination(10, TimeUnit.SECONDS); executorService.shutdownNow(); IOUtils.closeQuietly(cluster); } }
From source file:net.arp7.HdfsPerfTest.WriteFile.java
private static void writeFiles(final Configuration conf, final FileIoStats stats) throws InterruptedException, IOException { final FileSystem fs = FileSystem.get(conf); final AtomicLong filesLeft = new AtomicLong(params.getNumFiles()); final long runId = abs(rand.nextLong()); final byte[] data = new byte[params.getIoSize()]; Arrays.fill(data, (byte) 65); // Start the writers. final ExecutorService executor = Executors.newFixedThreadPool((int) params.getNumThreads()); final CompletionService<Object> ecs = new ExecutorCompletionService<>(executor); LOG.info("NumFiles=" + params.getNumFiles() + ", FileSize=" + FileUtils.byteCountToDisplaySize(params.getFileSize()) + ", IoSize=" + FileUtils.byteCountToDisplaySize(params.getIoSize()) + ", BlockSize=" + FileUtils.byteCountToDisplaySize(params.getBlockSize()) + ", ReplicationFactor=" + params.getReplication() + ", isThrottled=" + (params.maxWriteBps() > 0)); LOG.info("Starting " + params.getNumThreads() + " writer thread" + (params.getNumThreads() > 1 ? "s" : "") + "."); final long startTime = System.nanoTime(); for (long t = 0; t < params.getNumThreads(); ++t) { final long threadIndex = t; Callable<Object> c = new Callable<Object>() { @Override/* w ww . ja v a 2 s . co m*/ public Object call() throws Exception { long fileIndex = 0; while (filesLeft.addAndGet(-1) >= 0) { final String fileName = "WriteFile-" + runId + "-" + (threadIndex + 1) + "-" + (++fileIndex); writeOneFile(new Path(params.getOutputDir(), fileName), fs, data, stats); } return null; } }; ecs.submit(c); } // And wait for all writers to complete. for (long t = 0; t < params.getNumThreads(); ++t) { ecs.take(); } final long endTime = System.nanoTime(); stats.setElapsedTime(endTime - startTime); executor.shutdown(); }
From source file:org.springframework.batch.support.transaction.ConcurrentTransactionAwareProxyTests.java
@Before public void init() { executor = Executors.newFixedThreadPool(outerMax); completionService = new ExecutorCompletionService<List<String>>(executor); }
From source file:org.apache.hadoop.hbase.client.SpeculativeRequester.java
public ResultWrapper<T> request(final HBaseTableFunction<T> function, final HTableInterface primaryTable, final Collection<HTableInterface> failoverTables) { ExecutorCompletionService<ResultWrapper<T>> exeS = new ExecutorCompletionService<ResultWrapper<T>>(exe); final AtomicBoolean isPrimarySuccess = new AtomicBoolean(false); final long startTime = System.currentTimeMillis(); ArrayList<Callable<ResultWrapper<T>>> callables = new ArrayList<Callable<ResultWrapper<T>>>(); if (System.currentTimeMillis() - lastPrimaryFail.get() > waitTimeFromLastPrimaryFail) { callables.add(new Callable<ResultWrapper<T>>() { public ResultWrapper<T> call() throws Exception { try { T t = function.call(primaryTable); isPrimarySuccess.set(true); return new ResultWrapper(true, t); } catch (java.io.InterruptedIOException e) { Thread.currentThread().interrupt(); } catch (Exception e) { lastPrimaryFail.set(System.currentTimeMillis()); Thread.currentThread().interrupt(); }/* ww w . j a va 2 s . co m*/ return null; } }); } for (final HTableInterface failoverTable : failoverTables) { callables.add(new Callable<ResultWrapper<T>>() { public ResultWrapper<T> call() throws Exception { long waitToRequest = (System.currentTimeMillis() - lastPrimaryFail.get() > waitTimeFromLastPrimaryFail) ? waitTimeBeforeRequestingFailover - (System.currentTimeMillis() - startTime) : 0; if (waitToRequest > 0) { Thread.sleep(waitToRequest); } if (isPrimarySuccess.get() == false) { T t = function.call(failoverTable); long waitToAccept = (System.currentTimeMillis() - lastPrimaryFail.get() > waitTimeFromLastPrimaryFail) ? waitTimeBeforeAcceptingResults - (System.currentTimeMillis() - startTime) : 0; if (isPrimarySuccess.get() == false) { if (waitToAccept > 0) { Thread.sleep(waitToAccept); } } return new ResultWrapper(false, t); } else { throw new RuntimeException("Not needed"); } } }); } try { //ResultWrapper<T> t = exe.invokeAny(callables); for (Callable<ResultWrapper<T>> call : callables) { exeS.submit(call); } ResultWrapper<T> result = exeS.take().get(); //exe.shutdownNow(); return result; } catch (InterruptedException e) { e.printStackTrace(); LOG.error(e); } catch (ExecutionException e) { e.printStackTrace(); LOG.error(e); } return null; }