List of usage examples for java.util.concurrent ForkJoinPool submit
@SuppressWarnings("unchecked") public ForkJoinTask<?> submit(Runnable task)
From source file:Main.java
public static void main(String[] arg) throws Exception { Runnable parallelCode = () -> { HashSet<String> allThreads = new HashSet<>(); IntStream.range(0, 1_000_000).parallel().filter(i -> { allThreads.add(Thread.currentThread().getName()); return false; }).min();// w w w . j av a 2 s . co m System.out.println("executed by " + allThreads); }; System.out.println("default behavior: "); parallelCode.run(); System.out.println("specialized pool:"); ForkJoinPool pool = new ForkJoinPool(2); pool.submit(parallelCode).get(); }
From source file:net.openhft.chronicle.timeseries.Columns.java
public static void generateBrownian(DoubleColumn col, double start, double end, double sd) { long length = col.length(); double sd2 = sd / Math.sqrt(length); NormalDistribution nd = new NormalDistribution(0, sd2 * CHUNK_SIZE); int trendLength = Math.toIntExact((length - 1) / CHUNK_SIZE + 2); BytesStore trend = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(trendLength * 8L); double x = start; RandomGenerator rand = new MersenneTwister(); for (int i = 0; i < trendLength - 1; i++) { float f = rand.nextFloat(); trend.writeDouble((long) i << 3, x); x += nd.inverseCumulativeProbability(f); }/*from ww w . j av a 2 s . com*/ trend.writeDouble((long) (trendLength - 1) << 3, x); double diff = end - x; double gradient = diff / (trendLength - 1); for (int i = 0; i < trendLength; i++) { double y = trend.addAndGetDoubleNotAtomic((long) i << 3, i * gradient); // System.out.println(i + ": "+y); } int procs = Runtime.getRuntime().availableProcessors(); int chunksPerTask = (trendLength - 1) / procs + 1; ForkJoinPool fjp = ForkJoinPool.commonPool(); List<ForkJoinTask> tasks = new ArrayList<>(procs); for (int i = 0; i < procs; i++) { int si = i * chunksPerTask; int ei = Math.min(trendLength, si + chunksPerTask); tasks.add(fjp.submit(() -> { NormalDistribution nd2 = new NormalDistribution(0, sd2); RandomGenerator rand2 = new MersenneTwister(); for (int j = si; j < ei; j++) { generateBrownian(col, (long) j * CHUNK_SIZE, trend.readDouble((long) j << 3), trend.readDouble((long) (j + 1) << 3), nd2, rand2); } })); } for (ForkJoinTask task : tasks) { task.join(); } trend.release(); }
From source file:net.openhft.chronicle.timeseries.Columns.java
public static <T> void setAll(LongColumn col, Supplier<T> perThread, LongColumnIndexObjectConsumer<T> consumer) { long length = col.length(); int chunks = Math.toIntExact((length - 1) / CHUNK_SIZE + 1); ForkJoinPool fjp = ForkJoinPool.commonPool(); int procs = Runtime.getRuntime().availableProcessors(); List<ForkJoinTask> tasks = new ArrayList<>(procs); int chunksPerTask = (chunks - 1) / procs + 1; for (int i = 0; i < procs; i++) { int si = i * chunksPerTask; int ei = Math.min(chunks, si + chunksPerTask); tasks.add(fjp.submit(() -> { T t = perThread.get();/*from w w w . ja v a 2 s. c o m*/ long first = (long) si * CHUNK_SIZE; int max = (int) Math.min((ei - si) * CHUNK_SIZE, length - first); for (int j = 0; j < max; j++) { consumer.apply(col, first + j, t); } })); } for (ForkJoinTask task : tasks) { task.join(); } }
From source file:com.github.horrorho.inflatabledonkey.cloud.Donkey.java
void processConcurrent(HttpClient httpClient, ForkJoinPool fjp, AssetPool pool, FileAssembler consumer) throws IOException { logger.trace("<< processConcurrent()"); try {/*from w ww . j a v a 2s . c o m*/ Collection<StorageHostChunkList> containers = pool.authorize(httpClient); fjp.submit( () -> containers.parallelStream().forEach(u -> processContainer(httpClient, u, pool, consumer))) .get(); } catch (InterruptedException ex) { throw new UncheckedInterruptedException(ex); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } if (cause instanceof IOException) { throw (IOException) cause; } throw new RuntimeException(cause); } logger.trace("<< processConcurrent()"); }
From source file:com.linkedin.gradle.python.tasks.ParallelWheelGenerationTask.java
@TaskAction public void buildWheels() { ProgressLoggerFactory progressLoggerFactory = getServices().get(ProgressLoggerFactory.class); ProgressLogger progressLogger = progressLoggerFactory.newOperation(ParallelWheelGenerationTask.class); progressLogger.setDescription("Building wheels"); progressLogger.started();/*from ww w. j a v a 2 s. com*/ TaskTimer taskTimer = new TaskTimer(); // This way we don't try to over-alloc the system to much. We'll use slightly over 1/2 of the machine to build // the wheels in parallel. Allowing other operations to continue. ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors() / 2 + 1); Set<File> files = getFilesToConvert().getFiles(); int totalSize = files.size(); try { forkJoinPool.submit(() -> { files.stream().parallel().forEach(file -> { PackageInfo packageInfo = PackageInfo.fromPath(file); currentPackages.add(packageInfo.getName()); counter.incrementAndGet(); updateStatusLine(progressLogger, totalSize, counter.get()); TaskTimer.TickingClock clock = taskTimer .start(packageInfo.getName() + "-" + packageInfo.getVersion()); if (!packageSettings.requiresSourceBuild(packageInfo)) { makeWheelFromSdist(packageInfo); } currentPackages.remove(packageInfo.getName()); updateStatusLine(progressLogger, totalSize, counter.get()); clock.stop(); }); }).get(); } catch (InterruptedException | ExecutionException e) { logger.warn("Unable to pre-build some dependencies"); } finally { forkJoinPool.shutdown(); } try { FileUtils.write(getBuildReport(), taskTimer.buildReport()); } catch (IOException ignore) { // Don't fail if there is are issues writing the timing report. } progressLogger.completed(); }
From source file:com.newtranx.util.mysql.fabric.SpringQueryAllShardsAspect.java
@Around("@annotation(com.newtranx.util.mysql.fabric.QueryAllShards)") public Object union(ProceedingJoinPoint pjp) throws Throwable { Method method = AspectJUtils.getMethod(pjp); QueryAllShards annotation = method.getAnnotation(QueryAllShards.class); String table = annotation.table(); log.debug("Table=" + table); Set<String> groups = groupsCache.get(cacheKey); log.debug("ServerGroups=" + groups); List<Object> list; boolean readOnly = annotation.readOnly(); Pattern excludePattern;//w w w . j a v a 2 s. c om String excludeRegex = annotation.excludeShardsPatternRegex(); if (!StringUtils.isEmpty(excludeRegex)) { excludePattern = Pattern.compile(excludeRegex); } else { excludePattern = null; } Function<Boolean, List<Object>> computeFunction = (par) -> { Stream<String> stream = groups.stream(); if (par) stream = stream.parallel(); return stream.filter(gp -> { boolean exclude = excludePattern != null && excludePattern.matcher(gp).matches(); if (exclude) { log.debug("Skipping group:" + gp); } return !exclude; }).map(gp -> { log.debug("Querying group: " + gp); ds.whenNewConnection().doInit(conn -> conn.setServerGroupName(gp)) .doInit(conn -> conn.setReadOnly(readOnly)); try { return pjp.proceed(); } catch (Throwable t) { throw Exceptions.propagate(t); } finally { ds.clearInitOps(); } }).collect(Collectors.toList()); }; if (StringUtils.isEmpty(annotation.parallelPool())) { list = computeFunction.apply(false); } else { ForkJoinPool pool; if ("!jdkCommon".equals(annotation.parallelPool())) pool = ForkJoinPool.commonPool(); else pool = applicationContext.getBean(annotation.parallelPool(), ForkJoinPool.class); log.debug("Executing queries in parallel, pool=" + pool); list = pool.submit(() -> { return computeFunction.apply(true); }).get(); } Aggregator aggregator; try { aggregator = (Aggregator) annotation.aggregator().getDeclaredMethod("getInstance", EMPTY_PARAM) .invoke(null, EMPTY_ARGS); } catch (Exception e) { log.warn("Can not get singleton for class " + annotation.aggregator().getName() + ", creating new instance"); aggregator = annotation.aggregator().newInstance(); } return aggregator.apply(list); }
From source file:edu.usu.sdl.openstorefront.report.ExternalLinkValidationReport.java
private void checkLinks() { int timeOutTime = MAX_CONNECTION_TIME_MILLIS; if (report.getReportOption() != null) { if (report.getReportOption().getMaxWaitSeconds() != null) { timeOutTime = report.getReportOption().getMaxWaitSeconds() * 1000; }//w w w .j a va2s .co m } ForkJoinPool forkJoinPool = new ForkJoinPool(MAX_CHECKPOOL_SIZE); Map<String, LinkCheckModel> linkMap = new HashMap(); List<ForkJoinTask<LinkCheckModel>> tasks = new ArrayList<>(); for (LinkCheckModel link : links) { linkMap.put(link.getId(), link); tasks.add(forkJoinPool.submit(new CheckLinkTask(link, timeOutTime))); } int completedCount = 0; for (ForkJoinTask<LinkCheckModel> task : tasks) { try { LinkCheckModel processed; try { processed = task.get(timeOutTime, TimeUnit.MILLISECONDS); if (processed != null) { LinkCheckModel reportModel = linkMap.get(processed.getId()); reportModel.setStatus(processed.getStatus()); reportModel.setCheckResults(processed.getCheckResults()); reportModel.setHttpStatus(processed.getHttpStatus()); } else { //This shouldn't occur, however if it does at least show a message. log.log(Level.WARNING, MessageFormat.format( "A link check task failed to return results. Status at Completed Abnormally? {0}", task.isCompletedAbnormally())); } } catch (TimeoutException e) { task.cancel(true); } completedCount++; } catch (InterruptedException | ExecutionException ex) { log.log(Level.WARNING, "Check task was interrupted. Report results may be not complete.", ex); } log.log(Level.FINE, MessageFormat.format("Complete Checking Link Count: {0} out of {1}", new Object[] { completedCount, links.size() })); } for (LinkCheckModel checkModel : links) { if (StringUtils.isBlank(checkModel.getStatus())) { checkModel.setStatus("Unable to verify. Timed out while waiting."); } } forkJoinPool.shutdownNow(); try { forkJoinPool.awaitTermination(1000, TimeUnit.MILLISECONDS); } catch (InterruptedException ex) { log.log(Level.WARNING, "Check task shutdown was interrupted. The application will recover and continue.", ex); } }
From source file:io.kamax.mxisd.lookup.provider.DnsLookupProvider.java
@Override public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) { Map<String, List<ThreePidMapping>> domains = new HashMap<>(); for (ThreePidMapping mapping : mappings) { if (!ThreePidMedium.Email.is(mapping.getMedium())) { log.info("Skipping unsupported type {} for {}", mapping.getMedium(), mapping.getValue()); continue; }//from w w w . jav a2 s . com Optional<String> domainOpt = getDomain(mapping.getValue()); if (!domainOpt.isPresent()) { log.warn("No domain for 3PID {}", mapping.getValue()); continue; } String domain = domainOpt.get(); List<ThreePidMapping> domainMappings = domains.computeIfAbsent(domain, s -> new ArrayList<>()); domainMappings.add(mapping); } log.info("Looking mappings across {} domains", domains.keySet().size()); ForkJoinPool pool = ForkJoinPool.commonPool(); RecursiveTask<List<ThreePidMapping>> task = new RecursiveTask<List<ThreePidMapping>>() { @Override protected List<ThreePidMapping> compute() { List<ThreePidMapping> mappingsFound = new ArrayList<>(); List<DomainBulkLookupTask> tasks = new ArrayList<>(); for (String domain : domains.keySet()) { DomainBulkLookupTask domainTask = new DomainBulkLookupTask(domain, domains.get(domain)); domainTask.fork(); tasks.add(domainTask); } for (DomainBulkLookupTask task : tasks) { mappingsFound.addAll(task.join()); } return mappingsFound; } }; pool.submit(task); pool.shutdown(); List<ThreePidMapping> mappingsFound = task.join(); log.info("Found {} mappings overall", mappingsFound.size()); return mappingsFound; }
From source file:MSUmpire.PeptidePeakClusterDetection.PDHandlerBase.java
protected void PeakCurveCorrClustering(XYData mzRange) throws IOException { Logger.getRootLogger().info("Grouping isotopic peak curves........"); LCMSPeakBase.PeakClusters = new ArrayList<>(); //Thread pool final ForkJoinPool fjp = new ForkJoinPool(NoCPUs); // ArrayList<PeakCurveClusteringCorrKDtree> ResultList = new ArrayList<>(); final ArrayList<ForkJoinTask<ArrayList<PeakCluster>>> ftemp = new ArrayList<>(); final int end_idx = LCMSPeakBase.UnSortedPeakCurves.size(); final ArrayList<PeakCluster> resultClusters = new ArrayList<>(); //For each peak curve // for (PeakCurve Peakcurve : LCMSPeakBase.UnSortedPeakCurves) { for (int i = 0; i < end_idx; ++i) { final PeakCurve Peakcurve = LCMSPeakBase.UnSortedPeakCurves.get(i); if (Peakcurve.TargetMz >= mzRange.getX() && Peakcurve.TargetMz <= mzRange.getY()) { //Create a thread unit for doing isotope clustering given a peak curve as the monoisotope peak PeakCurveClusteringCorrKDtree unit = new PeakCurveClusteringCorrKDtree(Peakcurve, LCMSPeakBase.GetPeakCurveSearchTree(), parameter, IsotopePatternMap, LCMSPeakBase.StartCharge, LCMSPeakBase.EndCharge, LCMSPeakBase.MaxNoPeakCluster, LCMSPeakBase.MinNoPeakCluster); // ResultList.add(unit); ftemp.add(fjp.submit(unit)); }/* w ww . ja v a 2 s . c om*/ if (step_pccc == -1) step_pccc = fjp.getParallelism() * 32; final boolean last_iter = i + 1 == end_idx; if (ftemp.size() == step_pccc || last_iter) { final List<ForkJoinTask<ArrayList<PeakCluster>>> ftemp_sublist_view = last_iter ? ftemp : ftemp.subList(0, step_pccc / 2); for (final ForkJoinTask<ArrayList<PeakCluster>> fut : ftemp_sublist_view) try { resultClusters.addAll(fut.get()); } catch (InterruptedException | ExecutionException ex) { throw new RuntimeException(ex); } ftemp_sublist_view.clear(); if (!last_iter && fjp.getActiveThreadCount() < fjp.getParallelism()) { // System.out.println("PeakCurveSmoothingUnit: fjp.getActiveThreadCount()\t"+fjp.getActiveThreadCount()+"\t"+step_pccc); step_pccc *= 2; } } } assert ftemp.isEmpty() : "temp storage for futures should be empty by end of loop"; fjp.shutdown(); try { fjp.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { Logger.getRootLogger().info("interrupted.."); } for (final PeakCluster peakCluster : resultClusters) { //Check if the monoistope peak of cluster has been grouped in other isotope cluster, if yes, remove the peak cluster if (!parameter.RemoveGroupedPeaks || // !peakCluster.MonoIsotopePeak.ChargeGrouped.contains(peakCluster.Charge) !IonChargeHashSet.contains(peakCluster.MonoIsotopePeak.ChargeGrouped, peakCluster.Charge)) { peakCluster.Index = LCMSPeakBase.PeakClusters.size() + 1; peakCluster.GetConflictCorr(); LCMSPeakBase.PeakClusters.add(peakCluster); } } System.gc(); Logger.getRootLogger() .info("No of ion clusters:" + LCMSPeakBase.PeakClusters.size() + " (Memory usage:" + Math.round( (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576) + "MB)"); }
From source file:edu.cmu.tetrad.data.DataUtils.java
private static RealMatrix times(final RealMatrix m, final RealMatrix n) { if (m.getColumnDimension() != n.getRowDimension()) throw new IllegalArgumentException("Incompatible matrices."); final int rowDimension = m.getRowDimension(); final int columnDimension = n.getColumnDimension(); final RealMatrix out = new BlockRealMatrix(rowDimension, columnDimension); final int NTHREADS = Runtime.getRuntime().availableProcessors(); final int all = rowDimension; ForkJoinPool pool = ForkJoinPoolInstance.getInstance().getPool(); for (int t = 0; t < NTHREADS; t++) { final int _t = t; Runnable worker = new Runnable() { @Override//w w w . j av a 2 s . co m public void run() { int chunk = all / NTHREADS + 1; for (int row = _t * chunk; row < Math.min((_t + 1) * chunk, all); row++) { if ((row + 1) % 100 == 0) System.out.println(row + 1); for (int col = 0; col < columnDimension; ++col) { double sum = 0.0D; int commonDimension = m.getColumnDimension(); for (int i = 0; i < commonDimension; ++i) { sum += m.getEntry(row, i) * n.getEntry(i, col); } // double sum = m.getRowVector(row).dotProduct(n.getColumnVector(col)); out.setEntry(row, col, sum); } } } }; pool.submit(worker); } while (!pool.isQuiescent()) { } // for (int row = 0; row < rowDimension; ++row) { // if ((row + 1) % 100 == 0) System.out.println(row + 1); // // for (int col = 0; col < columnDimension; ++col) { // double sum = 0.0D; // // int commonDimension = m.getColumnDimension(); // // for (int i = 0; i < commonDimension; ++i) { // sum += m.getEntry(row, i) * n.getEntry(i, col); // } // // out.setEntry(row, col, sum); // } // } return out; }