List of usage examples for java.util.concurrent ExecutorService submit
Future<?> submit(Runnable task);
From source file:com.netflix.curator.framework.recipes.cache.TestPathChildrenCache.java
@Test public void testRebuildAgainstOtherProcesses() throws Exception { final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();//from w w w .ja v a2 s . co m try { client.create().forPath("/test"); client.create().forPath("/test/foo"); client.create().forPath("/test/bar"); client.create().forPath("/test/snafu", "original".getBytes()); final CountDownLatch addedLatch = new CountDownLatch(2); final PathChildrenCache cache = new PathChildrenCache(client, "/test", true); cache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) { if (event.getData().getPath().equals("/test/test")) { addedLatch.countDown(); } } else if (event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) { if (event.getData().getPath().equals("/test/snafu")) { addedLatch.countDown(); } } } }); cache.rebuildTestExchanger = new Exchanger<Object>(); ExecutorService service = Executors.newSingleThreadExecutor(); final AtomicReference<String> deletedPath = new AtomicReference<String>(); Future<Object> future = service.submit(new Callable<Object>() { @Override public Object call() throws Exception { cache.rebuildTestExchanger.exchange(new Object()); // simulate another process adding a node while we're rebuilding client.create().forPath("/test/test"); List<ChildData> currentData = cache.getCurrentData(); Assert.assertTrue(currentData.size() > 0); // simulate another process removing a node while we're rebuilding client.delete().forPath(currentData.get(0).getPath()); deletedPath.set(currentData.get(0).getPath()); cache.rebuildTestExchanger.exchange(new Object()); ChildData childData = null; while (childData == null) { childData = cache.getCurrentData("/test/snafu"); Thread.sleep(1000); } Assert.assertEquals(childData.getData(), "original".getBytes()); client.setData().forPath("/test/snafu", "grilled".getBytes()); cache.rebuildTestExchanger.exchange(new Object()); return null; } }); cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); future.get(); Assert.assertTrue(addedLatch.await(10, TimeUnit.SECONDS)); Assert.assertNotNull(cache.getCurrentData("/test/test")); Assert.assertNull(cache.getCurrentData(deletedPath.get())); Assert.assertEquals(cache.getCurrentData("/test/snafu").getData(), "grilled".getBytes()); cache.close(); } finally { client.close(); } }
From source file:com.github.c77.base_driver.kobuki.KobukiBaseDevice.java
public KobukiBaseDevice(UsbSerialDriver driver) throws Exception { if (driver == null) { throw new Exception("null USB driver provided"); }/*from w w w.j a va 2 s .co m*/ serialDriver = driver; try { serialDriver.open(); serialDriver.setParameters(115200, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE); } catch (IOException e) { log.info("Error setting up device: " + e.getMessage(), e); try { serialDriver.close(); } catch (IOException e1) { e1.printStackTrace(); } } final ExecutorService executorService = Executors.newSingleThreadExecutor(); SerialInputOutputManager serialInputOutputManager; final SerialInputOutputManager.Listener listener = new SerialInputOutputManager.Listener() { @Override public void onRunError(Exception e) { } @Override public void onNewData(final byte[] data) { KobukiBaseDevice.this.updateReceivedData(data); } }; serialInputOutputManager = new SerialInputOutputManager(serialDriver, listener); executorService.submit(serialInputOutputManager); }
From source file:io.fabric8.kubernetes.pipeline.BuildImageStepExecution.java
@Override protected ImageInspect run() throws Exception { return workspace.getChannel().call(new MasterToSlaveCallable<ImageInspect, Exception>() { @Override//from w w w . ja va 2 s.c om public ImageInspect call() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(2); try { Future<Boolean> createTarFuture; Future<ImageInspect> buildImageFuture; try (PipedInputStream pin = new PipedInputStream(); PipedOutputStream pout = new PipedOutputStream(pin)) { createTarFuture = executorService.submit(new CreateTarTask(pout)); buildImageFuture = executorService.submit(new BuildImageTask(pin)); } //Wait for the two tasks to complete. if (!createTarFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS)) { listener.getLogger().println("Failed to create docker image tarball."); } ImageInspect imageInspect = buildImageFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS); if (imageInspect == null) { throw new RuntimeException("Failed to build docker image."); } else { return imageInspect; } } finally { executorService.shutdown(); if (executorService.awaitTermination(30, TimeUnit.SECONDS)) { executorService.shutdownNow(); } } } }); }
From source file:com.github.c77.base_driver.husky.HuskyBaseDevice.java
public HuskyBaseDevice(UsbSerialDriver driver) { // Initialize timestamp for messages to be written to the Husky base initialTime = System.currentTimeMillis(); // Open and initialize the underlying USB-serial driver serialDriver = driver;/*from w w w . ja v a 2 s .co m*/ try { serialDriver.open(); serialDriver.setParameters(115200, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE); log.info("Serial device opened correctly"); } catch (IOException e) { log.error("Error setting up device: " + e.getMessage(), e); try { serialDriver.close(); } catch (Throwable t) { } throw new RuntimeException("Couldn't open USB device driver", e); } // Listen for USB-serial input events and call updateReceivedData whenever new data // is received final ExecutorService executorService = Executors.newSingleThreadExecutor(); SerialInputOutputManager serialInputOutputManager; final SerialInputOutputManager.Listener listener = new SerialInputOutputManager.Listener() { @Override public void onRunError(Exception e) { } @Override public void onNewData(final byte[] data) { HuskyBaseDevice.this.updateReceivedData(data); } }; serialInputOutputManager = new SerialInputOutputManager(serialDriver, listener); executorService.submit(serialInputOutputManager); }
From source file:ga.rugal.jpt.common.tracker.common.Torrent.java
private static String hashFiles(List<File> files, int pieceLenght) throws InterruptedException, IOException { int threads = getHashingThreadsCount(); ExecutorService executor = Executors.newFixedThreadPool(threads); ByteBuffer buffer = ByteBuffer.allocate(pieceLenght); List<Future<String>> results = new LinkedList<>(); StringBuilder hashes = new StringBuilder(); long length = 0L; int pieces = 0; long start = System.nanoTime(); for (File file : files) { LOG.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(), threads, (int) (Math.ceil((double) file.length() / pieceLenght)) }); length += file.length();//from w w w . j a v a2 s.c o m FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel(); int step = 10; try { while (channel.read(buffer) > 0) { if (buffer.remaining() == 0) { buffer.clear(); results.add(executor.submit(new CallableChunkHasher(buffer))); } if (results.size() >= threads) { pieces += accumulateHashes(hashes, results); } if (channel.position() / (double) channel.size() * 100f > step) { LOG.info(" ... {}% complete", step); step += 10; } } } finally { channel.close(); fis.close(); } } // Hash the last bit, if any if (buffer.position() > 0) { buffer.limit(buffer.position()); buffer.position(0); results.add(executor.submit(new CallableChunkHasher(buffer))); } pieces += accumulateHashes(hashes, results); // Request orderly executor shutdown and wait for hashing tasks to // complete. executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(10); } long elapsed = System.nanoTime() - start; int expectedPieces = (int) (Math.ceil((double) length / pieceLenght)); LOG.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(), length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), }); return hashes.toString(); }
From source file:org.ulyssis.ipp.reader.Reader.java
private void initSpeedway() { ExecutorService executor = Executors.newSingleThreadExecutor(); try {//from w ww. j a v a2s .c om while (!speedwayInitialized && !Thread.currentThread().isInterrupted()) { // We're doing this in another thread because LLRPReader doesn't // interrupt properly. Callable<Boolean> runCallable = () -> llrpReader .run(Config.getCurrentConfig().getSpeedwayURI(options.getId())); Future<Boolean> initFuture = executor.submit(runCallable); try { speedwayInitialized = initFuture.get(); } catch (ExecutionException e) { LOG.error("Starting the Speedway caused an exception.", e); speedwayInitialized = false; } if (!speedwayInitialized) { LOG.error("Couldn't start the Speedway! Retrying in {} ms.", Config.getCurrentConfig().getRetryInterval()); statusReporter.broadcast(new StatusMessage(StatusMessage.MessageType.STARTUP_FAILURE, "Failed to start the Speedway!")); Thread.sleep(Config.getCurrentConfig().getRetryInterval()); } } } catch (InterruptedException e) { // Shutting down } }
From source file:info.pancancer.arch3.coordinator.Coordinator.java
public void doWork() throws InterruptedException, ExecutionException { ExecutorService pool = Executors.newFixedThreadPool(DEFAULT_THREADS); CoordinatorOrders coordinatorOrders = new CoordinatorOrders(this.configFile, this.options.has(this.endlessSpec)); CleanupJobs cleanupJobs = new CleanupJobs(this.configFile, this.options.has(this.endlessSpec)); FlagJobs flagJobs = new FlagJobs(this.configFile, this.options.has(this.endlessSpec)); List<Future<?>> futures = new ArrayList<>(); futures.add(pool.submit(coordinatorOrders)); futures.add(pool.submit(cleanupJobs)); futures.add(pool.submit(flagJobs));//from ww w . j a va 2 s . c om try { for (Future<?> future : futures) { future.get(); } } catch (InterruptedException | ExecutionException ex) { log.error(ex.toString()); throw new RuntimeException(ex); } finally { pool.shutdown(); } }
From source file:eu.itesla_project.modules.validation.OfflineValidationTool.java
@Override public void run(CommandLine line) throws Exception { OfflineConfig config = OfflineConfig.load(); String rulesDbName = line.hasOption("rules-db-name") ? line.getOptionValue("rules-db-name") : OfflineConfig.DEFAULT_RULES_DB_NAME; String workflowId = line.getOptionValue("workflow"); Path outputDir = Paths.get(line.getOptionValue("output-dir")); double purityThreshold = line.hasOption("purity-threshold") ? Double.parseDouble(line.getOptionValue("purity-threshold")) : DEFAULT_PURITY_THRESHOLD;//w w w . java2s. c o m Set<Country> countries = Arrays.stream(line.getOptionValue("base-case-countries").split(",")) .map(Country::valueOf).collect(Collectors.toSet()); Interval histoInterval = Interval.parse(line.getOptionValue("history-interval")); boolean mergeOptimized = line.hasOption("merge-optimized"); CaseType caseType = CaseType.valueOf(line.getOptionValue("case-type")); CaseRepositoryFactory caseRepositoryFactory = config.getCaseRepositoryFactoryClass().newInstance(); RulesDbClientFactory rulesDbClientFactory = config.getRulesDbClientFactoryClass().newInstance(); ContingenciesAndActionsDatabaseClient contingencyDb = config.getContingencyDbClientFactoryClass() .newInstance().create(); SimulatorFactory simulatorFactory = config.getSimulatorFactoryClass().newInstance(); LoadFlowFactory loadFlowFactory = config.getLoadFlowFactoryClass().newInstance(); MergeOptimizerFactory mergeOptimizerFactory = config.getMergeOptimizerFactoryClass().newInstance(); SimulationParameters simulationParameters = SimulationParameters.load(); try (ComputationManager computationManager = new LocalComputationManager(); RulesDbClient rulesDb = rulesDbClientFactory.create(rulesDbName); CsvMetricsDb metricsDb = new CsvMetricsDb(outputDir, true, "metrics")) { CaseRepository caseRepository = caseRepositoryFactory.create(computationManager); Queue<DateTime> dates = Queues.synchronizedDeque( new ArrayDeque<>(caseRepository.dataAvailable(caseType, countries, histoInterval))); Map<String, Map<RuleId, ValidationStatus>> statusPerRulePerCase = Collections .synchronizedMap(new TreeMap<>()); Map<String, Map<RuleId, Map<HistoDbAttributeId, Object>>> valuesPerRulePerCase = Collections .synchronizedMap(new TreeMap<>()); int cores = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(cores); try { List<Future<?>> tasks = new ArrayList<>(cores); for (int i = 0; i < cores; i++) { tasks.add(executorService.submit((Runnable) () -> { while (dates.size() > 0) { DateTime date = dates.poll(); try { Network network = MergeUtil.merge(caseRepository, date, caseType, countries, loadFlowFactory, 0, mergeOptimizerFactory, computationManager, mergeOptimized); System.out.println("case " + network.getId() + " loaded"); System.out.println("running simulation on " + network.getId() + "..."); network.getStateManager().allowStateMultiThreadAccess(true); String baseStateId = network.getId(); network.getStateManager().cloneState(StateManager.INITIAL_STATE_ID, baseStateId); network.getStateManager().setWorkingState(baseStateId); Map<RuleId, ValidationStatus> statusPerRule = new HashMap<>(); Map<RuleId, Map<HistoDbAttributeId, Object>> valuesPerRule = new HashMap<>(); LoadFlow loadFlow = loadFlowFactory.create(network, computationManager, 0); LoadFlowResult loadFlowResult = loadFlow.run(); System.err.println("load flow terminated (" + loadFlowResult.isOk() + ") on " + network.getId()); if (loadFlowResult.isOk()) { Stabilization stabilization = simulatorFactory.createStabilization(network, computationManager, 0); ImpactAnalysis impactAnalysis = simulatorFactory.createImpactAnalysis(network, computationManager, 0, contingencyDb); Map<String, Object> context = new HashMap<>(); stabilization.init(simulationParameters, context); impactAnalysis.init(simulationParameters, context); StabilizationResult stabilizationResult = stabilization.run(); System.err.println("stabilization terminated (" + stabilizationResult.getStatus() + ") on " + network.getId()); metricsDb.store(workflowId, network.getId(), "STABILIZATION", stabilizationResult.getMetrics()); if (stabilizationResult.getStatus() == StabilizationStatus.COMPLETED) { ImpactAnalysisResult impactAnalysisResult = impactAnalysis .run(stabilizationResult.getState()); System.err.println("impact analysis terminated on " + network.getId()); metricsDb.store(workflowId, network.getId(), "IMPACT_ANALYSIS", impactAnalysisResult.getMetrics()); System.out.println("checking rules on " + network.getId() + "..."); for (SecurityIndex securityIndex : impactAnalysisResult .getSecurityIndexes()) { for (RuleAttributeSet attributeSet : RuleAttributeSet.values()) { statusPerRule.put(new RuleId(attributeSet, securityIndex.getId()), new ValidationStatus(null, securityIndex.isOk())); } } } } Map<HistoDbAttributeId, Object> values = IIDM2DB .extractCimValues(network, new IIDM2DB.Config(null, false)) .getSingleValueMap(); for (RuleAttributeSet attributeSet : RuleAttributeSet.values()) { for (Contingency contingency : contingencyDb.getContingencies(network)) { List<SecurityRule> securityRules = rulesDb.getRules(workflowId, attributeSet, contingency.getId(), null); for (SecurityRule securityRule : securityRules) { SecurityRuleExpression securityRuleExpression = securityRule .toExpression(purityThreshold); SecurityRuleCheckReport checkReport = securityRuleExpression .check(values); valuesPerRule.put(securityRule.getId(), ExpressionAttributeList .list(securityRuleExpression.getCondition()).stream() .collect(Collectors.toMap(attributeId -> attributeId, new Function<HistoDbAttributeId, Object>() { @Override public Object apply( HistoDbAttributeId attributeId) { Object value = values.get(attributeId); return value != null ? value : Float.NaN; } }))); ValidationStatus status = statusPerRule.get(securityRule.getId()); if (status == null) { status = new ValidationStatus(null, null); statusPerRule.put(securityRule.getId(), status); } if (checkReport.getMissingAttributes().isEmpty()) { status.setRuleOk(checkReport.isSafe()); } } } } statusPerRulePerCase.put(network.getId(), statusPerRule); valuesPerRulePerCase.put(network.getId(), valuesPerRule); } catch (Exception e) { LOGGER.error(e.toString(), e); } } })); } for (Future<?> task : tasks) { task.get(); } } finally { executorService.shutdown(); executorService.awaitTermination(1, TimeUnit.MINUTES); } writeCsv(statusPerRulePerCase, valuesPerRulePerCase, outputDir); } }
From source file:guru.nidi.languager.check.LinkChecker.java
public List<FindResult<String>> findBrokenLinks() { ExecutorService executor = Executors.newCachedThreadPool(); final List<FindResult<String>> res = Collections.synchronizedList(new ArrayList<FindResult<String>>()); final Set<String> urls = new HashSet<>(); int lineNum = 1; for (MessageLine line : contents.subList(1, contents.size())) { lineNum++;//from w w w. ja v a 2 s. c om int col = 1; int elemNum = 0; for (String element : line) { final Matcher matcher = LINK_PATTERN.matcher(element); while (matcher.find()) { final String url = matcher.group(); if (!urls.contains(url)) { urls.add(url); executor.submit(new LinkValidator(res, url, new SourcePosition(file, lineNum, col + elemNum + matcher.start()))); } } elemNum++; col += element.length(); } } executor.shutdown(); try { executor.awaitTermination(20, TimeUnit.SECONDS); } catch (InterruptedException e) { //ignore } return res; }
From source file:com.emc.ecs.sync.CasMigrationTest.java
protected void delete(FPPool pool, List<String> clipIds) throws Exception { ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS); System.out.print("Deleting clips"); for (String clipId : clipIds) { service.submit(new ClipDeleter(pool, clipId)); }/*from w w w .ja va2s.com*/ service.shutdown(); service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES); service.shutdownNow(); System.out.println(); }