List of usage examples for java.util.concurrent ExecutorService submit
Future<?> submit(Runnable task);
From source file:edu.iu.kmeans.regroupallgather.KMUtil.java
/** * Generate data and upload to the data dir. * /*from w ww . ja va 2s .c o m*/ * @param numOfDataPoints * @param vectorSize * @param numPointFiles * @param localInputDir * @param fs * @param dataDir * @throws IOException * @throws InterruptedException * @throws ExecutionException */ static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir, FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException { int pointsPerFile = numOfDataPoints / numPointFiles; System.out.println("Writing " + pointsPerFile + " vectors to a file"); // Check data directory if (fs.exists(dataDir)) { fs.delete(dataDir, true); } // Check local directory File localDir = new File(localInputDir); // If existed, regenerate data if (localDir.exists() && localDir.isDirectory()) { for (File file : localDir.listFiles()) { file.delete(); } localDir.delete(); } boolean success = localDir.mkdir(); if (success) { System.out.println("Directory: " + localInputDir + " created"); } if (pointsPerFile == 0) { throw new IOException("No point to write."); } // Create random data points int poolSize = Runtime.getRuntime().availableProcessors(); ExecutorService service = Executors.newFixedThreadPool(poolSize); List<Future<?>> futures = new LinkedList<Future<?>>(); for (int k = 0; k < numPointFiles; k++) { Future<?> f = service .submit(new DataGenRunnable(pointsPerFile, localInputDir, Integer.toString(k), vectorSize)); futures.add(f); // add a new thread } for (Future<?> f : futures) { f.get(); } // Shut down the executor service so that this // thread can exit service.shutdownNow(); // Wrap to path object Path localInput = new Path(localInputDir); fs.copyFromLocalFile(localInput, dataDir); }
From source file:fi.luontola.cqrshotel.framework.EventStoreContract.java
private static void repeatInParallel(int iterations, Runnable task, Runnable invariantChecker) throws Exception { final int PARALLELISM = 10; ExecutorService executor = Executors.newFixedThreadPool(PARALLELISM + 1); Future<?> checker;/*from w w w .j a v a2 s. c om*/ try { checker = executor.submit(() -> { while (!Thread.interrupted()) { invariantChecker.run(); Thread.yield(); } }); List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < iterations; i++) { futures.add(executor.submit(task)); } for (Future<?> future : futures) { // will throw ExecutionException if there was a problem future.get(); } } finally { executor.shutdownNow(); executor.awaitTermination(10, TimeUnit.SECONDS); } // will throw ExecutionException if there was a problem checker.get(10, TimeUnit.SECONDS); }
From source file:mase.conillon.ConillonMasterProblem.java
public static void runWithTimeout(Runnable runnable, long timeout, TimeUnit timeUnit) throws Exception { final ExecutorService executor = Executors.newSingleThreadExecutor(); final Future future = executor.submit(runnable); executor.shutdown(); // This does not cancel the already-scheduled task. try {/* ww w. j ava 2 s . c om*/ future.get(timeout, timeUnit); } catch (InterruptedException | ExecutionException | TimeoutException e) { future.cancel(true); throw e; } }
From source file:mase.conillon.ConillonMasterProblem.java
public static <T> T runWithTimeout(Callable<T> callable, long timeout, TimeUnit timeUnit) throws Exception { final ExecutorService executor = Executors.newSingleThreadExecutor(); final Future<T> future = executor.submit(callable); executor.shutdown(); // This does not cancel the already-scheduled task. try {//from ww w. j a v a 2s . c o m return future.get(timeout, timeUnit); } catch (InterruptedException | ExecutionException | TimeoutException e) { future.cancel(true); throw e; } }
From source file:Main.java
public static <T, R> ListenableFuture<List<R>> parallelTransform(Collection<T> input, final Function<T, R> function, final ExecutorService pool) { final List<ListenableFuture<R>> futures = Lists.newLinkedList(); // make futures for (final T in : input) { ListenableFutureTask<R> task = ListenableFutureTask.create(new Callable<R>() { @Override//from ww w.jav a 2s . c om public R call() throws Exception { return function.apply(in); } }); pool.submit(task); futures.add(task); } return Futures.successfulAsList(futures); }
From source file:joachimeichborn.geotag.handlers.OpenTracksHandler.java
public static void openTracks(final String aPath, final String[] aFiles, final TracksRepo aTracksRepo) { final Job job = new Job("Reading tracks") { @Override/*from ww w . j av a 2s . c om*/ protected IStatus run(final IProgressMonitor aMonitor) { aMonitor.beginTask("Reading " + aFiles.length + " tracks", aFiles.length); int threads = 2 * Runtime.getRuntime().availableProcessors(); logger.fine("Using " + threads + " cores for loading tracks"); final ExecutorService threadPool = Executors.newFixedThreadPool(threads); final List<Future<?>> futures = new LinkedList<>(); for (final String file : aFiles) { final Path trackFile = Paths.get(aPath, file); futures.add(threadPool.submit(new TrackReader(aMonitor, trackFile, aTracksRepo))); } final IStatus status = waitForAllTracksToBeRead(futures); aMonitor.done(); return status; } private IStatus waitForAllTracksToBeRead(final List<Future<?>> futures) { for (final Future<?> future : futures) { try { future.get(); } catch (InterruptedException e) { logger.log(Level.FINE, "Waiting for track to be loaded was interrupted", e); Thread.currentThread().interrupt(); return Status.CANCEL_STATUS; } catch (ExecutionException e) { logger.log(Level.FINE, "Reading track failed", e); Thread.currentThread().interrupt(); return Status.CANCEL_STATUS; } } logger.info("Reading " + futures.size() + " tracks completed"); return Status.OK_STATUS; } }; job.setUser(true); job.schedule(); }
From source file:edu.iu.daal_kmeans.regroupallgather.KMUtil.java
/** * Generate data and upload to the data dir. * /*from w ww. j a v a 2 s .c om*/ * @param numOfDataPoints * @param vectorSize * @param numPointFiles * @param localInputDir * @param fs * @param dataDir * @throws IOException * @throws InterruptedException * @throws ExecutionException */ static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir, FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException { int pointsPerFile = numOfDataPoints / numPointFiles; System.out.println("Writing " + pointsPerFile + " vectors to a file"); // Check data directory if (fs.exists(dataDir)) { fs.delete(dataDir, true); } // Check local directory File localDir = new File(localInputDir); // If existed, regenerate data if (localDir.exists() && localDir.isDirectory()) { for (File file : localDir.listFiles()) { file.delete(); } localDir.delete(); } boolean success = localDir.mkdir(); if (success) { System.out.println("Directory: " + localInputDir + " created"); } if (pointsPerFile == 0) { throw new IOException("No point to write."); } // Create random data points int poolSize = Runtime.getRuntime().availableProcessors(); ExecutorService service = Executors.newFixedThreadPool(poolSize); List<Future<?>> futures = new LinkedList<Future<?>>(); for (int k = 0; k < numPointFiles; k++) { Future<?> f = service .submit(new DataGenMMDense(pointsPerFile, localInputDir, Integer.toString(k), vectorSize)); futures.add(f); // add a new thread } for (Future<?> f : futures) { f.get(); } // Shut down the executor service so that this // thread can exit service.shutdownNow(); // Wrap to path object Path localInput = new Path(localInputDir); fs.copyFromLocalFile(localInput, dataDir); DeleteFileFolder(localInputDir); }
From source file:edu.iu.daal_naive.NaiveUtil.java
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, int nClasses, String localInputDir, FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException { int pointsPerFile = numOfDataPoints / numPointFiles; System.out.println("Writing " + pointsPerFile + " vectors to a file"); // Check data directory if (fs.exists(dataDir)) { fs.delete(dataDir, true);//from w w w.j a va 2s . c o m } // Check local directory File localDir = new File(localInputDir); // If existed, regenerate data if (localDir.exists() && localDir.isDirectory()) { for (File file : localDir.listFiles()) { file.delete(); } localDir.delete(); } boolean success = localDir.mkdir(); if (success) { System.out.println("Directory: " + localInputDir + " created"); } if (pointsPerFile == 0) { throw new IOException("No point to write."); } // Create random data points int poolSize = Runtime.getRuntime().availableProcessors(); ExecutorService service = Executors.newFixedThreadPool(poolSize); List<Future<?>> futures = new LinkedList<Future<?>>(); for (int k = 0; k < numPointFiles; k++) { Future<?> f = service.submit( new DataGenNaiveBayes(localInputDir, Integer.toString(k), pointsPerFile, vectorSize, nClasses)); futures.add(f); // add a new thread } for (Future<?> f : futures) { f.get(); } // Shut down the executor service so that this // thread can exit service.shutdownNow(); // Wrap to path object Path localInput = new Path(localInputDir); fs.copyFromLocalFile(localInput, dataDir); }
From source file:com.dattack.dbtools.drules.engine.DrulesEngine.java
private static SourceResultGroup getSourceResultsList(final List<SourceBean> sourceList) throws DrulesNestableException { final ExecutorService executorService = Executors.newCachedThreadPool(createThreadFactory()); final List<Future<SourceResult>> futureList = new ArrayList<>(); for (final SourceBean sourceBean : sourceList) { futureList.add(executorService.submit(new SourceExecutor(sourceBean, ConfigurationUtils.cloneConfiguration(ThreadContext.getInstance().getConfiguration())))); }/*from w w w. j av a 2s . c om*/ final SourceResultGroup sourceResultList = new SourceResultGroup(); for (final Future<SourceResult> future : futureList) { try { sourceResultList.add(future.get()); } catch (InterruptedException | ExecutionException e) { throw new DrulesNestableException(e); } } executorService.shutdown(); return sourceResultList; }
From source file:com.netsteadfast.greenstep.util.SystemExpressionJobUtils.java
public static void executeJobs() throws ServiceException, Exception { List<ExpressionJobObj> jobObjList = getExpressionJobs(); if (jobObjList == null || jobObjList.size() < 1) { return;/*from w ww . j av a2s. c o m*/ } ExecutorService exprJobPool = Executors .newFixedThreadPool(SimpleUtils.getAvailableProcessors(jobObjList.size())); for (ExpressionJobObj jobObj : jobObjList) { jobObj = exprJobPool.submit(new ExpressionJobExecuteCallable(jobObj)).get(); } exprJobPool.shutdown(); }