List of usage examples for java.util.concurrent ExecutorService invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;
From source file:forge.CardStorageReader.java
private void executeLoadTask(final Collection<CardRules> result, final List<Callable<List<CardRules>>> tasks, final CountDownLatch cdl) { try {/* ww w . j ava 2 s . co m*/ if (useThreadPool) { final ExecutorService executor = ThreadUtil.getComputingPool(0.5f); final List<Future<List<CardRules>>> parts = executor.invokeAll(tasks); executor.shutdown(); cdl.await(); for (final Future<List<CardRules>> pp : parts) { result.addAll(pp.get()); } } else { for (final Callable<List<CardRules>> c : tasks) { result.addAll(c.call()); } } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } catch (final Exception e) { // this clause comes from non-threaded branch throw new RuntimeException(e); } }
From source file:com.brienwheeler.lib.concurrent.ExecutorsTest.java
@Test public void testNewSingleThreadExecutorInvokeAll() throws InterruptedException, ExecutionException { NamedThreadFactory threadFactory = new NamedThreadFactory(THREAD_FACTORY_NAME); ExecutorService executor = Executors.newSingleThreadExecutor(threadFactory); IntCallable one = new IntCallable(1); IntCallable two = new IntCallable(2); ArrayList<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); tasks.add(one);/*from w w w. j ava2 s. co m*/ tasks.add(two); List<Future<Integer>> results = executor.invokeAll(tasks); Assert.assertEquals(2, results.size()); Iterator<Future<Integer>> it = results.iterator(); Future<Integer> oneResult = it.next(); Future<Integer> twoResult = it.next(); Assert.assertTrue(oneResult.isDone()); Assert.assertEquals(1, oneResult.get().intValue()); Assert.assertEquals(2, twoResult.get().intValue()); results = executor.invokeAll(tasks, 10, TimeUnit.MILLISECONDS); Assert.assertEquals(2, results.size()); it = results.iterator(); oneResult = it.next(); twoResult = it.next(); Assert.assertTrue(oneResult.isDone()); Assert.assertEquals(1, oneResult.get().intValue()); Assert.assertEquals(2, twoResult.get().intValue()); executor.shutdown(); }
From source file:org.openvpms.archetype.rules.workflow.AbstractScheduleServiceTest.java
/** * Helper to runs tasks concurrently.//from w w w . j a v a 2s . c o m * * @param tasks the tasks to run * @throws Exception for any error */ protected void runConcurrent(Callable<PropertySet>... tasks) throws Exception { List<Callable<PropertySet>> list = Arrays.asList(tasks); ExecutorService executorService = Executors.newFixedThreadPool(list.size()); List<Future<PropertySet>> futures = executorService.invokeAll(list); assertEquals(tasks.length, futures.size()); for (Future future : futures) { future.get(); } }
From source file:org.aludratest.jenkins.aludratest.AludratestProjectStatisticsReport.java
private synchronized void cacheStatistics(String fromBuildNumber, String toBuildNumber) { cachedStatistics = new ProjectStatistics(); // check if range (or at least start) is given int startBuildNo = -1; int endBuildNo = -1; if (fromBuildNumber != null && !"".equals(fromBuildNumber)) { try {//from w ww .j ava 2 s . com startBuildNo = Integer.parseInt(fromBuildNumber); if (toBuildNumber != null && !"".equals(toBuildNumber)) { endBuildNo = Integer.parseInt(toBuildNumber); } if (startBuildNo < 0) { // relative mode: Find last N builds Run<?, ?> build = project.getLastBuild(); int buildCount = 0; int targetBuildCount = startBuildNo * -1; while (build != null && buildCount < targetBuildCount) { if (new File(build.getRootDir(), AludratestStatisticsPublisher.STATISTICS_FILE_NAME) .isFile()) { buildCount++; } startBuildNo = build.getNumber(); build = build.getPreviousBuild(); } // no toBuild supported then endBuildNo = -1; } } catch (NumberFormatException e) { startBuildNo = endBuildNo = -1; } } // iterate over all builds having a stats file Run<?, ?> build = startBuildNo == -1 ? project.getFirstBuild() : project.getBuildByNumber(startBuildNo); if (build == null) { // no fallback here, no caching - empty results return; } // optimized, lengthy code to parallelize String -> JSON parsing // useful for MANY builds with HUGE amount of test cases List<Callable<Void>> runnables = new ArrayList<>(); final Map<Integer, JSONObject> parsedObjects = new ConcurrentHashMap<>(); while (build != null && (endBuildNo == -1 || build.getNumber() <= endBuildNo)) { final File statsFile = new File(build.getRootDir(), AludratestStatisticsPublisher.STATISTICS_FILE_NAME); if (statsFile.isFile()) { final int buildNumber = build.getNumber(); runnables.add(new Callable<Void>() { @Override public Void call() { try { JSONObject o = (JSONObject) JSONSerializer .toJSON(FileUtils.readFileToString(statsFile, "UTF-8")); parsedObjects.put(Integer.valueOf(buildNumber), o); } catch (IOException e) { // TODO log } return null; } }); } build = build.getNextBuild(); } if (!runnables.isEmpty()) { ExecutorService svc = Executors .newFixedThreadPool(Math.max(2, Runtime.getRuntime().availableProcessors() - 1)); try { svc.invokeAll(runnables); svc.shutdown(); if (!svc.awaitTermination(5, TimeUnit.MINUTES)) { // took too long... // TODO handle somehow } } catch (InterruptedException e) { return; } } List<Integer> keys = new ArrayList<>(parsedObjects.keySet()); Collections.sort(keys); for (Integer buildNumber : keys) { // check if there is a display name for a build; otherwise, use # + number Run<?, ?> b = project.getBuildByNumber(buildNumber.intValue()); String dn = (b != null ? b.getDisplayName() : null); if (dn == null) { dn = "#" + buildNumber; } cachedStatistics.addBuildData(buildNumber.intValue(), dn, parsedObjects.get(buildNumber)); } }
From source file:com.facebook.presto.accumulo.tools.TimestampCheckTask.java
public int exec() throws Exception { // Create the instance and the connector Instance inst = new ZooKeeperInstance(config.getInstance(), config.getZooKeepers()); Connector connector = inst.getConnector(config.getUsername(), new PasswordToken(config.getPassword())); if (auths == null) { auths = connector.securityOperations().getUserAuthorizations(config.getUsername()); }/*from w w w. ja v a2 s . c o m*/ // Fetch the table metadata ZooKeeperMetadataManager manager = new ZooKeeperMetadataManager(config, new TypeRegistry()); LOG.info("Scanning Presto metadata for tables..."); AccumuloTable table = manager.getTable(new SchemaTableName(schema, tableName)); if (table == null) { LOG.error("Table is null, does it exist?"); return 1; } AccumuloRowSerializer serializer = new LexicoderRowSerializer(); startBytes = serializer.encode(TimestampType.TIMESTAMP, PARSER.parseDateTime(start).getMillis()); endBytes = serializer.encode(TimestampType.TIMESTAMP, PARSER.parseDateTime(end).getMillis()); this.range = new Range(new Text(startBytes), new Text(endBytes)); long timestamp = System.currentTimeMillis(); Optional<AccumuloColumnHandle> columnHandle = table.getColumns().stream() .filter(handle -> handle.getName().equalsIgnoreCase(column)).findAny(); checkArgument(columnHandle.isPresent(), "no column found"); ExecutorService service = MoreExecutors.getExitingExecutorService( new ThreadPoolExecutor(3, 3, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<>())); List<Future<Void>> tasks = service.invokeAll(ImmutableList.of(() -> { getDataCount(connector, table, columnHandle.get(), timestamp); return null; }, () -> { getIndexCount(connector, table, columnHandle.get(), timestamp); return null; }, () -> { getMetricCount(connector, table, columnHandle.get(), timestamp); return null; })); for (Future<Void> task : tasks) { task.get(); } LOG.info("Finished"); return 0; }
From source file:org.apache.sysml.runtime.io.FrameReaderTextCSVParallel.java
/** * // w w w . j a v a 2 s. c o m * @param path * @param job * @param fs * @param dest * @param schema * @param names * @param rlen * @param clen * @return * @throws IOException */ @Override protected void readCSVFrameFromHDFS(Path path, JobConf job, FileSystem fs, FrameBlock dest, List<ValueType> schema, List<String> names, long rlen, long clen) throws IOException { int numThreads = OptimizerUtils.getParallelTextReadParallelism(); TextInputFormat informat = new TextInputFormat(); informat.configure(job); InputSplit[] splits = informat.getSplits(job, numThreads); splits = IOUtilFunctions.sortInputSplits(splits); try { ExecutorService pool = Executors.newFixedThreadPool(numThreads); //compute num rows per split ArrayList<CountRowsTask> tasks = new ArrayList<CountRowsTask>(); for (int i = 0; i < splits.length; i++) tasks.add(new CountRowsTask(splits[i], informat, job, _props.hasHeader(), i == 0)); List<Future<Long>> cret = pool.invokeAll(tasks); //compute row offset per split via cumsum on row counts long offset = 0; List<Long> offsets = new ArrayList<Long>(); for (Future<Long> count : cret) { offsets.add(offset); offset += count.get(); } //read individial splits ArrayList<ReadRowsTask> tasks2 = new ArrayList<ReadRowsTask>(); for (int i = 0; i < splits.length; i++) tasks2.add(new ReadRowsTask(splits[i], informat, job, dest, offsets.get(i).intValue(), i == 0)); List<Future<Object>> rret = pool.invokeAll(tasks2); pool.shutdown(); //error handling for (Future<Object> read : rret) read.get(); } catch (Exception e) { throw new IOException("Failed parallel read of text csv input.", e); } }
From source file:com.mercatis.lighthouse3.persistence.commons.rest.DomainModelEntityDAOImplementation.java
/** * This method must be implemented by subclasses to resolve result lists * returned from a web service method to a list of corresponding domain * model entities./*from w ww . j a v a2 s . co m*/ * * @param webServiceResultList * the result list returned from the web service * @return the set of entities */ protected List<Entity> resolveWebServiceResultList(String webServiceResultList) { List<Entity> result = new LinkedList<Entity>(); List<String> entityIds = XmlMuncher.readValuesFromXml(webServiceResultList, "//:id"); List<Callable<Entity>> jobs = new ArrayList<Callable<Entity>>(); for (String entityId : entityIds) { jobs.add(new ByIdFinder(Long.parseLong(entityId))); } ExecutorService pooledExecutor = Executors .newFixedThreadPool(this.getResolveWebServiceResultThreadPoolSize()); try { List<Future<Entity>> jobResults = pooledExecutor.invokeAll(jobs); for (Future<Entity> jobResult : jobResults) { result.add(jobResult.get()); } } catch (Exception ex) { throw new PersistenceException("Encountered problem while resolving entity id result list", ex); } return result; }
From source file:org.jasig.cas.ticket.registry.JpaTicketRegistryTests.java
@Test @IfProfileValue(name = "cas.jpa.concurrent", value = "true") public void testConcurrentServiceTicketGeneration() throws Exception { final TicketGrantingTicket newTgt = newTGT(); addTicketInTransaction(newTgt);/*from w w w .j av a 2 s. co m*/ final ExecutorService executor = Executors.newFixedThreadPool(CONCURRENT_SIZE); try { final List<ServiceTicketGenerator> generators = new ArrayList<ServiceTicketGenerator>(CONCURRENT_SIZE); for (int i = 0; i < CONCURRENT_SIZE; i++) { generators.add(new ServiceTicketGenerator(newTgt.getId())); } final List<Future<String>> results = executor.invokeAll(generators); for (Future<String> result : results) { assertNotNull(result.get()); } } catch (Exception e) { logger.debug("testConcurrentServiceTicketGeneration produced an error", e); fail("testConcurrentServiceTicketGeneration failed."); } finally { executor.shutdownNow(); } }
From source file:org.apache.sysml.runtime.io.FrameReaderTextCSVParallel.java
@Override protected Pair<Integer, Integer> computeCSVSize(Path path, JobConf job, FileSystem fs) throws IOException { int numThreads = OptimizerUtils.getParallelTextReadParallelism(); TextInputFormat informat = new TextInputFormat(); informat.configure(job);//from ww w . j a v a 2 s . c om InputSplit[] splits = informat.getSplits(job, numThreads); //compute number of columns RecordReader<LongWritable, Text> reader = informat.getRecordReader(splits[0], job, Reporter.NULL); LongWritable key = new LongWritable(); Text value = new Text(); reader.next(key, value); int ncol = StringUtils.countMatches(value.toString(), _props.getDelim()) + 1; reader.close(); //compute number of rows ExecutorService pool = Executors.newFixedThreadPool(numThreads); //compute num rows per split int nrow = 0; try { ArrayList<CountRowsTask> tasks = new ArrayList<CountRowsTask>(); for (int i = 0; i < splits.length; i++) tasks.add(new CountRowsTask(splits[i], informat, job, _props.hasHeader(), i == 0)); List<Future<Long>> cret = pool.invokeAll(tasks); for (Future<Long> count : cret) nrow += count.get().intValue(); } catch (Exception e) { throw new IOException("Failed parallel read of text csv input.", e); } return new Pair<Integer, Integer>(nrow, ncol); }
From source file:org.openmrs.module.emrapi.adt.AdtServiceComponentTest.java
@Test public void integrationTest_ADT_workflow_duplicate_visits() throws Exception { final Integer numberOfThreads = 5; final CyclicBarrier threadsBarrier = new CyclicBarrier(numberOfThreads); Callable<Integer> checkInCall = new Callable<Integer>() { @Override/* www .j ava 2 s . co m*/ public Integer call() throws Exception { Context.openSession(); authenticate(); try { LocationService locationService = Context.getLocationService(); Patient patient = Context.getPatientService().getPatient(7); // parent location should support visits LocationTag supportsVisits = new LocationTag(); supportsVisits.setName(EmrApiConstants.LOCATION_TAG_SUPPORTS_VISITS); locationService.saveLocationTag(supportsVisits); Location parentLocation = locationService.getLocation(2); parentLocation.addTag(supportsVisits); locationService.saveLocation(parentLocation); threadsBarrier.await(); Encounter checkInEncounter = service.checkInPatient(patient, parentLocation, null, null, null, false); return checkInEncounter.getVisit().getVisitId(); } finally { Context.closeSession(); } } }; List<Callable<Integer>> checkInCalls = new ArrayList<Callable<Integer>>(); for (int i = 0; i < numberOfThreads; i++) { checkInCalls.add(checkInCall); } ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads); List<Future<Integer>> checkIns = executorService.invokeAll(checkInCalls); Integer visitId = null; for (Future<Integer> checkIn : checkIns) { Integer nextVisitId = checkIn.get(); if (visitId != null) { assertThat(nextVisitId, is(visitId)); } else { visitId = nextVisitId; } } }