List of usage examples for java.util.concurrent ExecutorService shutdown
void shutdown();
From source file:org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServerTest.java
@Test public void testConcurrentUpdate() throws Exception { TestServlet.clear();//from w w w . j a v a 2 s . c o m String serverUrl = jetty.getBaseUrl().toString() + "/cuss/foo"; int cussThreadCount = 2; int cussQueueSize = 100; // for tracking callbacks from CUSS final AtomicInteger successCounter = new AtomicInteger(0); final AtomicInteger errorCounter = new AtomicInteger(0); final StringBuilder errors = new StringBuilder(); @SuppressWarnings("serial") ConcurrentUpdateSolrServer cuss = new ConcurrentUpdateSolrServer(serverUrl, cussQueueSize, cussThreadCount) { @Override public void handleError(Throwable ex) { errorCounter.incrementAndGet(); errors.append(" " + ex); } @Override public void onSuccess(HttpResponse resp) { successCounter.incrementAndGet(); } }; cuss.setParser(new BinaryResponseParser()); cuss.setRequestWriter(new BinaryRequestWriter()); cuss.setPollQueueTime(0); // ensure it doesn't block where there's nothing to do yet cuss.blockUntilFinished(); int poolSize = 5; ExecutorService threadPool = Executors.newFixedThreadPool(poolSize, new SolrjNamedThreadFactory("testCUSS")); int numDocs = 100; int numRunnables = 5; for (int r = 0; r < numRunnables; r++) threadPool.execute(new SendDocsRunnable(String.valueOf(r), numDocs, cuss)); // ensure all docs are sent threadPool.awaitTermination(5, TimeUnit.SECONDS); threadPool.shutdown(); // wait until all requests are processed by CUSS cuss.blockUntilFinished(); cuss.shutdownNow(); assertEquals("post", TestServlet.lastMethod); // expect all requests to be successful int expectedSuccesses = TestServlet.numReqsRcvd.get(); assertTrue(expectedSuccesses > 0); // at least one request must have been sent assertTrue("Expected no errors but got " + errorCounter.get() + ", due to: " + errors.toString(), errorCounter.get() == 0); assertTrue("Expected " + expectedSuccesses + " successes, but got " + successCounter.get(), successCounter.get() == expectedSuccesses); int expectedDocs = numDocs * numRunnables; assertTrue("Expected CUSS to send " + expectedDocs + " but got " + TestServlet.numDocsRcvd.get(), TestServlet.numDocsRcvd.get() == expectedDocs); }
From source file:net.darkmist.clf.Main.java
private void handleFiles(String fileNames[], int off, int len) { DirTraverser traverser;// w w w . j a v a 2 s . c om Queue<File> files; ExecutorService executor; // convert fileNames to Files and put them in a Queue files = Util.newQueue(Util.getStringToFileConverter(), fileNames, off, len); //executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); executor = MoreExecutors.newCurrentThreadPool(); traverser = new DirTraverser(files, new ExecutorFileHandler(executor, this)); // let her rip traverser.run(); // all done traversing... shutdown the executor executor.shutdown(); // and wait for it while (!executor.isTerminated()) { try { executor.awaitTermination(STATUS_TIME, STATUS_UNIT); } catch (InterruptedException e) { logger.warn("Ignoring InterruptedException until thread pool executor stops", e); } if (logger.isDebugEnabled() && executor instanceof ThreadPoolExecutor) { ThreadPoolExecutor pool = (ThreadPoolExecutor) executor; logger.debug("ThreadPool size=" + pool.getPoolSize() + " active=" + pool.getActiveCount() + " queue=" + pool.getQueue().size()); } } executor = null; logger.debug("handleFiles done..."); }
From source file:com.graphhopper.jsprit.core.algorithm.io.VehicleRoutingAlgorithms.java
private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator, final StateManager stateManager, ConstraintManager constraintManager, boolean addDefaultCostCalculators) { // map to store constructed modules TypedMap definedClasses = new TypedMap(); // algorithm listeners Set<PrioritizedVRAListener> algorithmListeners = new HashSet<PrioritizedVRAListener>(); // insertion listeners List<InsertionListener> insertionListeners = new ArrayList<InsertionListener>(); //threading//from w ww.jav a 2 s. com final ExecutorService executorService; if (nuOfThreads > 0) { log.debug("setup executor-service with " + nuOfThreads + " threads"); executorService = Executors.newFixedThreadPool(nuOfThreads); algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new AlgorithmEndsListener() { @Override public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) { log.debug("shutdown executor-service"); executorService.shutdown(); } })); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread arg0, Throwable arg1) { System.err.println(arg1.toString()); } }); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { if (!executorService.isShutdown()) { System.err.println("shutdowHook shuts down executorService"); executorService.shutdown(); } } }); } else executorService = null; //create fleetmanager final VehicleFleetManager vehicleFleetManager = createFleetManager(vrp); String switchString = config.getString("construction.insertion.allowVehicleSwitch"); final boolean switchAllowed; if (switchString != null) { switchAllowed = Boolean.parseBoolean(switchString); } else switchAllowed = true; ActivityTimeTracker.ActivityPolicy activityPolicy; if (stateManager.timeWindowUpdateIsActivated()) { UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows( stateManager, vrp.getTransportCosts(), vrp.getActivityCosts()); timeWindowUpdater .setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() { Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>(); @Override public Collection<Vehicle> get(VehicleRoute vehicleRoute) { if (uniqueTypes.isEmpty()) { for (Vehicle v : vrp.getVehicles()) { if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) { uniqueTypes.put(v.getVehicleTypeIdentifier(), v); } } } Collection<Vehicle> vehicles = new ArrayList<Vehicle>(); vehicles.addAll(uniqueTypes.values()); return vehicles; } }); stateManager.addStateUpdater(timeWindowUpdater); activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS; } else { activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED; } stateManager.addStateUpdater( new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy, vrp.getActivityCosts())); stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy)); final SolutionCostCalculator costCalculator; if (solutionCostCalculator == null) costCalculator = getDefaultCostCalculator(stateManager); else costCalculator = solutionCostCalculator; PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager, stateManager, constraintManager); //construct initial solution creator final InsertionStrategy initialInsertionStrategy = createInitialSolution(config, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, costCalculator, constraintManager, addDefaultCostCalculators); if (initialInsertionStrategy != null) prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy, costCalculator); //construct algorithm, i.e. search-strategies and its modules int solutionMemory = config.getInt("strategy.memory"); List<HierarchicalConfiguration> strategyConfigs = config .configurationsAt("strategy.searchStrategies.searchStrategy"); for (HierarchicalConfiguration strategyConfig : strategyConfigs) { String name = getName(strategyConfig); SolutionAcceptor acceptor = getAcceptor(strategyConfig, vrp, algorithmListeners, definedClasses, solutionMemory); SolutionSelector selector = getSelector(strategyConfig, vrp, algorithmListeners, definedClasses); SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator); strategy.setName(name); List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module"); for (HierarchicalConfiguration moduleConfig : modulesConfig) { SearchStrategyModule module = buildModule(moduleConfig, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, constraintManager, addDefaultCostCalculators); strategy.addModule(module); } prettyAlgorithmBuilder.withStrategy(strategy, strategyConfig.getDouble("probability")); } //construct algorithm VehicleRoutingAlgorithm metaAlgorithm = prettyAlgorithmBuilder.build(); int maxIterations = getMaxIterations(config); if (maxIterations > -1) metaAlgorithm.setMaxIterations(maxIterations); //define prematureBreak PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config, algorithmListeners); if (prematureAlgorithmTermination != null) metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination); else { List<HierarchicalConfiguration> terminationCriteria = config .configurationsAt("terminationCriteria.termination"); for (HierarchicalConfiguration terminationConfig : terminationCriteria) { PrematureAlgorithmTermination termination = getTerminationCriterion(terminationConfig, algorithmListeners); if (termination != null) metaAlgorithm.addTerminationCriterion(termination); } } for (PrioritizedVRAListener l : algorithmListeners) { metaAlgorithm.getAlgorithmListeners().add(l); } return metaAlgorithm; }
From source file:io.wcm.caravan.pipeline.impl.JsonPipelineMultipleSubscriptionsTest.java
@Test public void subscribeConcurrentlyToTransformedPipelineOutputs() throws InterruptedException { // this test verifies that pipelines actions are only executed once, even if there are multiple concurrent subscribers firstStep = newPipelineWithResponseBody("{id:123}"); secondStep = firstStep.applyAction(action); when(action.execute(any(), any())).thenReturn(firstStep.getOutput()); // create multiple simultaneous threads that subscribe to the same pipeline output // and use a CountDownLatch to delay the subscription until all threads have been started CountDownLatch countDown = new CountDownLatch(100); ExecutorService executorService = Executors.newCachedThreadPool(); while (countDown.getCount() > 0) { executorService.submit(() -> { countDown.await();/*from www . j a va 2 s. c o m*/ secondStep.getOutput().subscribe(Subscribers.empty()); return null; // this is required for the lambda to be considered a Callable<Void> and therefore be allowed to throw exceptions }); countDown.countDown(); } executorService.shutdown(); executorService.awaitTermination(1, TimeUnit.MINUTES); verify(action, times(1)).execute(any(), any()); }
From source file:jsprit.core.algorithm.io.VehicleRoutingAlgorithms.java
private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator, final StateManager stateManager, ConstraintManager constraintManager, boolean addDefaultCostCalculators) { // map to store constructed modules TypedMap definedClasses = new TypedMap(); // algorithm listeners Set<PrioritizedVRAListener> algorithmListeners = new HashSet<PrioritizedVRAListener>(); // insertion listeners List<InsertionListener> insertionListeners = new ArrayList<InsertionListener>(); //threading//w w w . j a v a 2 s. co m final ExecutorService executorService; if (nuOfThreads > 0) { log.debug("setup executor-service with " + nuOfThreads + " threads"); executorService = Executors.newFixedThreadPool(nuOfThreads); algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new AlgorithmEndsListener() { @Override public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) { log.debug("shutdown executor-service"); executorService.shutdown(); } })); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread arg0, Throwable arg1) { System.err.println(arg1.toString()); } }); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { if (!executorService.isShutdown()) { System.err.println("shutdowHook shuts down executorService"); executorService.shutdown(); } } }); } else executorService = null; //create fleetmanager final VehicleFleetManager vehicleFleetManager = createFleetManager(vrp); String switchString = config.getString("construction.insertion.allowVehicleSwitch"); final boolean switchAllowed; if (switchString != null) { switchAllowed = Boolean.parseBoolean(switchString); } else switchAllowed = true; ActivityTimeTracker.ActivityPolicy activityPolicy; if (stateManager.timeWindowUpdateIsActivated()) { UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows( stateManager, vrp.getTransportCosts()); timeWindowUpdater .setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() { Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>(); @Override public Collection<Vehicle> get(VehicleRoute vehicleRoute) { if (uniqueTypes.isEmpty()) { for (Vehicle v : vrp.getVehicles()) { if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) { uniqueTypes.put(v.getVehicleTypeIdentifier(), v); } } } Collection<Vehicle> vehicles = new ArrayList<Vehicle>(); vehicles.addAll(uniqueTypes.values()); return vehicles; } }); stateManager.addStateUpdater(timeWindowUpdater); activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS; } else { activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED; } stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy)); stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy)); final SolutionCostCalculator costCalculator; if (solutionCostCalculator == null) costCalculator = getDefaultCostCalculator(stateManager); else costCalculator = solutionCostCalculator; PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager, stateManager, constraintManager); //construct initial solution creator final InsertionStrategy initialInsertionStrategy = createInitialSolution(config, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, costCalculator, constraintManager, addDefaultCostCalculators); if (initialInsertionStrategy != null) prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy, costCalculator); //construct algorithm, i.e. search-strategies and its modules int solutionMemory = config.getInt("strategy.memory"); List<HierarchicalConfiguration> strategyConfigs = config .configurationsAt("strategy.searchStrategies.searchStrategy"); for (HierarchicalConfiguration strategyConfig : strategyConfigs) { String name = getName(strategyConfig); SolutionAcceptor acceptor = getAcceptor(strategyConfig, vrp, algorithmListeners, definedClasses, solutionMemory); SolutionSelector selector = getSelector(strategyConfig, vrp, algorithmListeners, definedClasses); SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator); strategy.setName(name); List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module"); for (HierarchicalConfiguration moduleConfig : modulesConfig) { SearchStrategyModule module = buildModule(moduleConfig, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, constraintManager, addDefaultCostCalculators); strategy.addModule(module); } prettyAlgorithmBuilder.withStrategy(strategy, strategyConfig.getDouble("probability")); } //construct algorithm VehicleRoutingAlgorithm metaAlgorithm = prettyAlgorithmBuilder.build(); int maxIterations = getMaxIterations(config); if (maxIterations > -1) metaAlgorithm.setMaxIterations(maxIterations); //define prematureBreak PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config, algorithmListeners); if (prematureAlgorithmTermination != null) metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination); else { List<HierarchicalConfiguration> terminationCriteria = config .configurationsAt("terminationCriteria.termination"); for (HierarchicalConfiguration terminationConfig : terminationCriteria) { PrematureAlgorithmTermination termination = getTerminationCriterion(terminationConfig, algorithmListeners); if (termination != null) metaAlgorithm.addTerminationCriterion(termination); } } for (PrioritizedVRAListener l : algorithmListeners) { metaAlgorithm.getAlgorithmListeners().add(l); } return metaAlgorithm; }
From source file:com.amazonaws.services.kinesis.producer.KinesisProducerTest.java
@Test public void multipleInstances() throws Exception { int N = 8;//from ww w .jav a 2 s . com final KinesisProducer[] kps = new KinesisProducer[N]; ExecutorService exec = Executors.newFixedThreadPool(N); for (int i = 0; i < N; i++) { final int n = i; exec.submit(new Runnable() { @Override public void run() { try { kps[n] = getProducer(null, null); } catch (Exception e) { log.error("Error starting KPL", e); } } }); } exec.shutdown(); exec.awaitTermination(30, TimeUnit.SECONDS); Thread.sleep(10000); for (int i = 0; i < N; i++) { assertNotNull(kps[i]); assertNotNull(kps[i].getMetrics()); kps[i].destroy(); } }
From source file:com.example.AzureADResponseFilter.java
private AuthenticationResult getAccessToken(AuthorizationCode authorizationCode, String currentUri) throws Throwable { String authCode = authorizationCode.getValue(); ClientCredential credential = new ClientCredential(clientId, clientSecret); AuthenticationContext context = null; AuthenticationResult result = null;/*w w w. j a v a2 s.c om*/ ExecutorService service = null; try { service = Executors.newFixedThreadPool(1); context = new AuthenticationContext(authority + tenant + "/", true, service); Future<AuthenticationResult> future = context.acquireTokenByAuthorizationCode(authCode, new URI(currentUri), credential, null); result = future.get(); } catch (ExecutionException e) { throw e.getCause(); } finally { service.shutdown(); } if (result == null) { throw new ServiceUnavailableException("authentication result was null"); } return result; }
From source file:com.linkedin.pinot.perf.QueryRunner.java
/** * Use multiple threads to run queries as fast as possible. * * Start {numThreads} worker threads to send queries (blocking call) back to back, and use the main thread to collect * the statistic information and log them periodically. * * @param conf perf benchmark driver config. * @param queryFile query file./*from w ww .j ava 2 s. co m*/ * @param numThreads number of threads sending queries. * @throws Exception */ @SuppressWarnings("InfiniteLoopStatement") public static void multiThreadedsQueryRunner(PerfBenchmarkDriverConf conf, String queryFile, final int numThreads) throws Exception { final long randomSeed = 123456789L; final Random random = new Random(randomSeed); final int reportIntervalMillis = 3000; final List<String> queries; try (FileInputStream input = new FileInputStream(new File(queryFile))) { queries = IOUtils.readLines(input); } final int numQueries = queries.size(); final PerfBenchmarkDriver driver = new PerfBenchmarkDriver(conf); final AtomicInteger counter = new AtomicInteger(0); final AtomicLong totalResponseTime = new AtomicLong(0L); final ExecutorService executorService = Executors.newFixedThreadPool(numThreads); final DescriptiveStatistics stats = new DescriptiveStatistics(); final CountDownLatch latch = new CountDownLatch(numThreads); for (int i = 0; i < numThreads; i++) { executorService.submit(new Runnable() { @Override public void run() { for (int j = 0; j < numQueries; j++) { String query = queries.get(random.nextInt(numQueries)); long startTime = System.currentTimeMillis(); try { driver.postQuery(query); long clientTime = System.currentTimeMillis() - startTime; synchronized (stats) { stats.addValue(clientTime); } counter.getAndIncrement(); totalResponseTime.getAndAdd(clientTime); } catch (Exception e) { LOGGER.error("Caught exception while running query: {}", query, e); return; } } latch.countDown(); } }); } executorService.shutdown(); int iter = 0; long startTime = System.currentTimeMillis(); while (latch.getCount() > 0) { Thread.sleep(reportIntervalMillis); double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND; int count = counter.get(); double avgResponseTime = ((double) totalResponseTime.get()) / count; LOGGER.info("Time Passed: {}s, Query Executed: {}, QPS: {}, Avg Response Time: {}ms", timePassedSeconds, count, count / timePassedSeconds, avgResponseTime); iter++; if (iter % 10 == 0) { printStats(stats); } } printStats(stats); }
From source file:oz.hadoop.yarn.api.core.LocalApplicationLaunchTests.java
@Test(timeout = 5000) public void validateInfiniteJavaContainerLaunchForcedShutdown() throws Exception { final YarnApplication<Void> yarnApplication = YarnAssembly .forApplicationContainer(InfiniteContainer.class, ByteBuffer.wrap("Hello".getBytes())) .containerCount(4).memory(512).withApplicationMaster().maxAttempts(2).priority(2) .build("sample-yarn-application"); ExecutorService executor = Executors.newCachedThreadPool(); executor.execute(new Runnable() { @Override/* ww w . ja v a2 s.c om*/ public void run() { yarnApplication.launch(); } }); Thread.sleep(4000); assertTrue(yarnApplication.isRunning()); yarnApplication.terminate(); assertFalse(yarnApplication.isRunning()); executor.shutdown(); }
From source file:com.topekalabs.synchronization.LockTest.java
@Test public void testLockAndUnlock() { final long timeout = 500; ExecutorService es = Executors.newSingleThreadExecutor(); Future<Integer> future = es.submit(new Callable<Integer>() { @Override// ww w. j a v a 2s .co m public Integer call() throws Exception { Lock lock = lockClass.newInstance(); lock.lock(); lock.unlock(); return 1; } }); try { future.get(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException ex) { ErrorThrower.kill(ex); } es.shutdown(); }