List of usage examples for java.util.concurrent ExecutorService awaitTermination
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
From source file:org.trnltk.apps.morphology.contextless.parser.CachingMorphologicParserApp.java
@App("Parse sample TBMM Journal w/o bulk parse") public void parseTbmmJournal_b0241h_noBulkParse() throws Exception { final File tokenizedFile = new File("core/src/test/resources/tokenizer/tbmm_b0241h_tokenized.txt"); final List<String> lines = Files.readLines(tokenizedFile, Charsets.UTF_8); final LinkedList<String> words = new LinkedList<String>(); final HashSet<String> uniqueWords = new HashSet<String>(); for (String line : lines) { final ArrayList<String> strings = Lists .newArrayList(Splitter.on(" ").trimResults().omitEmptyStrings().split(line)); words.addAll(strings);/* ww w . java 2 s . c o m*/ uniqueWords.addAll(strings); } final int initialL1CacheSize = uniqueWords.size(); final int maxL1CacheSize = initialL1CacheSize; final MorphologicParserCache l1Cache = new LRUMorphologicParserCache(NUMBER_OF_THREADS, initialL1CacheSize, maxL1CacheSize); final ExecutorService pool = Executors.newFixedThreadPool(NUMBER_OF_THREADS); final MorphologicParser[] parsers = new MorphologicParser[NUMBER_OF_THREADS]; for (int i = 0; i < parsers.length; i++) { parsers[i] = new CachingMorphologicParser(new TwoLevelMorphologicParserCache(BULK_SIZE, l1Cache), contextlessMorphologicParser, true); } final StopWatch stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i < words.size(); i++) { final MorphologicParser parser = parsers[i % NUMBER_OF_THREADS]; final String word = words.get(i); final int wordIndex = i; pool.execute(new SingleParseCommand(parser, word, wordIndex, false)); } pool.shutdown(); while (!pool.isTerminated()) { System.out.println("Waiting pool to be terminated!"); pool.awaitTermination(500, TimeUnit.MILLISECONDS); } stopWatch.stop(); System.out.println("Total time :" + stopWatch.toString()); System.out.println("Nr of tokens : " + words.size()); System.out.println("Avg time : " + (stopWatch.getTime() * 1.0d) / (words.size() * 1.0d) + " ms"); }
From source file:com.dumontierlab.pdb2rdf.Pdb2Rdf.java
private static void load(CommandLine cmd, final Map<String, Double> stats) { String username = "dba"; String password = "dba"; String host = "localhost"; int port = 1111; DetailLevel detailLevel = null;//w w w . j a v a 2 s . c o m if (cmd.hasOption("detailLevel")) { try { detailLevel = Enum.valueOf(DetailLevel.class, cmd.getOptionValue("detailLevel")); } catch (IllegalArgumentException e) { LOG.fatal("Invalid argument value for detailLevel option", e); System.exit(1); } } final DetailLevel f_detailLevel = detailLevel; if (cmd.hasOption("username")) { username = cmd.getOptionValue("username"); } if (cmd.hasOption("password")) { password = cmd.getOptionValue("password"); } if (cmd.hasOption("host")) { host = cmd.getOptionValue("host"); } if (cmd.hasOption("port")) { try { port = Integer.parseInt(cmd.getOptionValue("port")); } catch (NumberFormatException e) { LOG.fatal("Invalid port number: " + cmd.getOptionValue("port")); System.exit(1); } } final VirtuosoDaoFactory factory = new VirtuosoDaoFactory(host, port, username, password); ExecutorService pool = getThreadPool(cmd); final ProgressMonitor monitor = getProgressMonitor(); final Pdb2RdfInputIterator i = processInput(cmd); final int inputSize = i.size(); final AtomicInteger progressCount = new AtomicInteger(); if (monitor != null) { monitor.setProgress(0, inputSize); } while (i.hasNext()) { final InputSource input = i.next(); pool.execute(new Runnable() { public void run() { PdbXmlParser parser = new PdbXmlParser(); UriBuilder uriBuilder = new UriBuilder(); PdbRdfModel model = null; try { model = new VirtPdbRdfModel(factory, Bio2RdfPdbUriPattern.PDB_GRAPH, uriBuilder, factory.getTripleStoreDao()); if (f_detailLevel != null) { parser.parse(input, model, f_detailLevel); } else { parser.parse(input, model); } if (stats != null) { updateStats(stats, model); } if (monitor != null) { monitor.setProgress(progressCount.incrementAndGet(), inputSize); } } catch (Exception e) { LOG.error("Uanble to parse input for pdb=" + (model != null ? model.getPdbId() : "null"), e); } } }); } pool.shutdown(); while (!pool.isTerminated()) { try { pool.awaitTermination(1, TimeUnit.SECONDS); } catch (InterruptedException e) { break; } } }
From source file:com.alibaba.cobar.client.CobarSqlMapClientTemplate.java
public void destroy() throws Exception { if (CollectionUtils.isNotEmpty(internalExecutorServiceRegistry)) { logger.info("shutdown executors of CobarSqlMapClientTemplate..."); for (ExecutorService executor : internalExecutorServiceRegistry) { if (executor != null) { try { executor.shutdown(); executor.awaitTermination(5, TimeUnit.MINUTES); executor = null;/*from w ww . ja v a 2 s . co m*/ } catch (InterruptedException e) { logger.warn("interrupted when shuting down the query executor:\n{}", e); } } } getDataSourceSpecificExecutors().clear(); logger.info("all of the executor services in CobarSqlMapClientTemplate are disposed."); } }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java
@Test public void testPublisherConfirmReceived() throws Exception { final CountDownLatch latch = new CountDownLatch(10000); final AtomicInteger acks = new AtomicInteger(); templateWithConfirmsEnabled.setConfirmCallback((correlationData, ack, cause) -> { acks.incrementAndGet();//from www .j a va 2s . co m latch.countDown(); }); ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < 100; i++) { exec.submit(() -> { try { for (int i1 = 0; i1 < 100; i1++) { templateWithConfirmsEnabled.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc")); } } catch (Throwable t) { t.printStackTrace(); } }); } exec.shutdown(); assertTrue(exec.awaitTermination(300, TimeUnit.SECONDS)); assertTrue("" + latch.getCount(), latch.await(60, TimeUnit.SECONDS)); assertNull(templateWithConfirmsEnabled.getUnconfirmed(-1)); this.templateWithConfirmsEnabled.execute(channel -> { Map<?, ?> listenerMap = TestUtils.getPropertyValue(((ChannelProxy) channel).getTargetChannel(), "listenerForSeq", Map.class); int n = 0; while (n++ < 100 && listenerMap.size() > 0) { Thread.sleep(100); } assertEquals(0, listenerMap.size()); return null; }); Log logger = spy(TestUtils.getPropertyValue(connectionFactoryWithConfirmsEnabled, "logger", Log.class)); new DirectFieldAccessor(connectionFactoryWithConfirmsEnabled).setPropertyValue("logger", logger); cleanUp(); verify(logger, never()).error(any()); }
From source file:org.apache.reef.io.network.NetworkServiceTest.java
@Test public void testMultithreadedSharedConnMessagingNetworkServiceRate() throws Exception { Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST)); LOG.log(Level.FINEST, name.getMethodName()); final IdentifierFactory factory = new StringIdentifierFactory(); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(NameServerParameters.NameServerIdentifierFactory.class, factory); injector.bindVolatileInstance(LocalAddressProvider.class, this.localAddressProvider); try (final NameServer server = injector.getInstance(NameServer.class)) { final int nameServerPort = server.getPort(); final int[] messageSizes = { 2000 }; // {1,16,32,64,512,64*1024,1024*1024}; for (final int size : messageSizes) { final int numMessages = 300000 / (Math.max(1, size / 512)); final int numThreads = 2; final int totalNumMessages = numMessages * numThreads; final Monitor monitor = new Monitor(); // network service final String name2 = "task2"; final String name1 = "task1"; final Configuration nameResolverConf = Tang.Factory.getTang() .newConfigurationBuilder(NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, this.localAddress) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServerPort).build()) .build();//from ww w .j a va 2 s. c o m final Injector injector2 = Tang.Factory.getTang().newInjector(nameResolverConf); LOG.log(Level.FINEST, "=== Test network service receiver start"); LOG.log(Level.FINEST, "=== Test network service sender start"); try (final NameResolver nameResolver = injector2.getInstance(NameResolver.class)) { injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceIdentifierFactory.class, factory); injector2.bindVolatileInstance(NameResolver.class, nameResolver); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceCodec.class, new StringCodec()); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceTransportFactory.class, injector.getInstance(MessagingTransportFactory.class)); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceExceptionHandler.class, new ExceptionHandler()); final Injector injectorNs2 = injector2.forkInjector(); injectorNs2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name2, monitor, totalNumMessages)); final NetworkService<String> ns2 = injectorNs2.getInstance(NetworkService.class); final Injector injectorNs1 = injector2.forkInjector(); injectorNs1.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name1, null, 0)); final NetworkService<String> ns1 = injectorNs1.getInstance(NetworkService.class); ns2.registerId(factory.getNewInstance(name2)); final int port2 = ns2.getTransport().getListeningPort(); server.register(factory.getNewInstance("task2"), new InetSocketAddress(this.localAddress, port2)); ns1.registerId(factory.getNewInstance(name1)); final int port1 = ns1.getTransport().getListeningPort(); server.register(factory.getNewInstance("task1"), new InetSocketAddress(this.localAddress, port1)); final Identifier destId = factory.getNewInstance(name2); try (final Connection<String> conn = ns1.newConnection(destId)) { conn.open(); final String message = StringUtils.repeat('1', size); final ExecutorService e = Executors.newCachedThreadPool(); final long start = System.currentTimeMillis(); for (int i = 0; i < numThreads; i++) { e.submit(new Runnable() { @Override public void run() { for (int i = 0; i < numMessages; i++) { conn.write(message); } } }); } e.shutdown(); e.awaitTermination(30, TimeUnit.SECONDS); monitor.mwait(); final long end = System.currentTimeMillis(); final double runtime = ((double) end - start) / 1000; LOG.log(Level.FINEST, "size: " + size + "; messages/s: " + totalNumMessages / runtime + " bandwidth(bytes/s): " + ((double) totalNumMessages * 2 * size) / runtime); // x2 for unicode chars } } } } }
From source file:de.unisb.cs.st.javalanche.mutation.runtime.testDriver.MutationTestDriver.java
/** * Runs given test in a new thread with specified timeout * (DEFAULT_TIMEOUT_IN_SECONDS) and stores the results in given testResult. * /*w ww.j av a 2 s .com*/ * @param r * the test to be run * @return the time needed for executing the test */ protected long runWithTimeoutOld(MutationTestRunnable r) { // ArrayList<Thread> threadsPre = ThreadUtil.getThreads(); ExecutorService service = Executors.newSingleThreadExecutor(); Future<?> future = service.submit(r); StopWatch stopWatch = new StopWatch(); stopWatch.start(); service.shutdown(); String exceptionMessage = null; Throwable capturedThrowable = null; try { logger.debug("Start test: "); boolean terminated = service.awaitTermination(timeout, TimeUnit.SECONDS); logger.debug("First timeout"); long time1 = stopWatch.getTime(); if (!terminated) { service.shutdownNow(); } future.get(1, TimeUnit.SECONDS); logger.debug("Second timeout"); long time2 = stopWatch.getTime(); if (time2 - time1 > 1000) { logger.info("Process got some extra time: " + (time2 - time1) + " " + time2); } future.cancel(true); } catch (InterruptedException e) { capturedThrowable = e; } catch (ExecutionException e) { capturedThrowable = e; } catch (TimeoutException e) { exceptionMessage = "Mutation causes test timeout"; capturedThrowable = e; } catch (Throwable t) { capturedThrowable = t; } finally { if (capturedThrowable != null) { if (exceptionMessage == null) { exceptionMessage = "Exception caught during test execution."; } r.setFailed(exceptionMessage, capturedThrowable); } } if (!future.isDone()) { r.setFailed("Mutated Thread is still running after timeout.", null); switchOfMutation(future); } stopWatch.stop(); if (!r.hasFinished()) { shutDown(r, stopWatch); } logger.debug("End timed test, it took " + stopWatch.getTime() + " ms"); return stopWatch.getTime(); }
From source file:org.biopax.validator.impl.ValidatorImpl.java
public void validate(final Validation validation) { assert (validation != null); if (validation == null || validation.getModel() == null) { throw new ValidatorException("Failed: no BioPAX model to validate " + "(have you successfully imported or created one already?)"); }//from www. j a va 2 s.co m // register the validation (if not done already) if (!getResults().contains(validation)) { getResults().add(validation); } // break if max.errors exceeded (- reported by AOP interceptors, while parsing a file, or - in previous runs) if (validation.isMaxErrorsSet() && validation.getNotFixedErrors() > validation.getMaxErrors()) { log.info("Errors limit (" + validation.getMaxErrors() + ") is exceeded; exitting..."); return; } Model model = (Model) validation.getModel(); log.debug("validating model: " + model + " that has " + model.getObjects().size() + " objects"); if (model.getLevel() != BioPAXLevel.L3) { model = (new LevelUpgrader()).filter(model); validation.setModel(model); log.info("Upgraded to BioPAX Level3 model: " + validation.getDescription()); } assert (model != null && model.getLevel() == BioPAXLevel.L3); // Check/fix Rule<? extends BioPAXElement> rules concurrently (low risk of getting CMEx), // because they normally do minor changes and simply cannot add/remove // elements in the Model (though, can alter a property of any biopax object) ExecutorService exec = Executors.newFixedThreadPool(100); // First, check/fix individual objects // (no need to copy; these rules cannot add/remove objects in model) for (BioPAXElement el : model.getObjects()) { // rules can check/fix specific elements // for (Rule rule : rules) { // Behavior behavior = utils.getRuleBehavior(rule.getClass().getName(), validation.getProfile()); // if (behavior == Behavior.IGNORE) // continue; // skip // execute(exec, rule, validation, (Object) el); // } //sequentially apply all (capable,enabled) rules to the object in a separate thread execute(exec, rules, validation, (Object) el); } exec.shutdown(); //end accepting new jobs try { exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new ValidatorException("Interrupted unexpectedly!"); } //Second, apply all Rule<Model> rules - //run Rule<Model> rules concurrently exec = Executors.newFixedThreadPool(50); for (Rule rule : rules) { Behavior behavior = utils.getRuleBehavior(rule.getClass().getName(), validation.getProfile()); if (behavior == Behavior.IGNORE) continue; // skip disabled rule execute(exec, rule, validation, model); } exec.shutdown(); //end accepting jobs try { exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new ValidatorException("Interrupted unexpectedly!"); } log.debug("All rules checked!"); if (validation.isFix()) { // discover, explicitly add child elements to the model model.repair(); // remove all dangling utility class objects ModelUtils.removeObjectsIfDangling(model, UtilityClass.class); } // add comments and some statistics validation.addComment("number of interactions : " + model.getObjects(Interaction.class).size()); validation.addComment("number of physical entities : " + model.getObjects(PhysicalEntity.class).size()); validation.addComment("number of genes : " + model.getObjects(Gene.class).size()); validation.addComment("number of pathways : " + model.getObjects(Pathway.class).size()); //update all error counts (total, fixed, notfixed) for (ErrorType errorType : validation.getError()) { errorType.setTotalCases(errorType.countErrors(null, null, false)); errorType.setNotFixedCases(errorType.countErrors(null, null, true)); } validation.setNotFixedProblems(validation.countErrors(null, null, null, null, false, true)); validation.setNotFixedErrors(validation.countErrors(null, null, null, null, true, true)); validation.setTotalProblemsFound(validation.countErrors(null, null, null, null, false, false)); validation.setSummary("different types of problem: " + validation.getError().size()); }
From source file:com.emc.ecs.sync.storage.CasStorageTest.java
private List<String> createTestClips(FPPool pool, int maxBlobSize, int thisMany, Writer summaryWriter) throws Exception { ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS); System.out.print("Creating clips"); List<String> clipIds = Collections.synchronizedList(new ArrayList<String>()); List<String> summaries = Collections.synchronizedList(new ArrayList<String>()); for (int clipIdx = 0; clipIdx < thisMany; clipIdx++) { service.submit(new ClipWriter(pool, clipIds, maxBlobSize, summaries)); }// w ww .j a v a 2 s . c om service.shutdown(); service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES); service.shutdownNow(); Collections.sort(summaries); for (String summary : summaries) { summaryWriter.append(summary); } System.out.println(); return clipIds; }
From source file:com.linemetrics.monk.api.ApiClient.java
public List<DataItem> getRangeOptimized(final Number dataStreamId, long time_from, long time_to, TDB tdb, TimeZone tz) throws ApiException { try {/*from w ww . j a v a 2s. c om*/ long timeDiff = time_to - time_from; long maxTimeRange = tdb.getQueryLimit(); long queryRange = tdb.getQueryRange(); if (timeDiff < maxTimeRange) { return this.getRange(dataStreamId, time_from, time_to, tdb, tz); } long millis = System.currentTimeMillis(); ExecutorService executorService = Executors.newSingleThreadExecutor(); Set<Future<List<DataItem>>> callables = new HashSet<Future<List<DataItem>>>(); long queryStart = time_from; long queryEnd = time_from + queryRange; while (queryStart < time_to) { callables.add(executorService .submit(new CallableRangeQuery(dataStreamId, queryStart, queryEnd, tdb, tz))); queryStart += queryRange; queryEnd += queryRange; } executorService.shutdown(); List<DataItem> list = new ArrayList<>(); for (Future<List<DataItem>> future : callables) { List<DataItem> slice = future.get(); if (slice == null) { throw new ApiException("Error while retrieving slice :("); } else { list.addAll(slice); } } executorService.awaitTermination(60 * 60 * 1000L, TimeUnit.MILLISECONDS); System.out.print("Optimized Range Query takes: " + (System.currentTimeMillis() - millis) + "ms "); // System.out.println(list.size()); // Collections.sort(list, DataItemComparator.getInstance()); DataItem prevItem = null, currItem; DataItem beginSlice = null; Iterator<DataItem> itemIterator = list.iterator(); while (itemIterator.hasNext()) { currItem = itemIterator.next(); if (prevItem != null) { if (prevItem.getTimestamp().equals(currItem.getTimestamp())) { itemIterator.remove(); continue; } if (beginSlice == null) { if (currItem.getTimestamp() - prevItem.getTimestamp() > tdb.getMilliseconds()) { beginSlice = prevItem; } } else { if (currItem.getTimestamp() - prevItem.getTimestamp() == tdb.getMilliseconds()) { System.out.println("TimeRange " + beginSlice.getTimestamp() + " - " + prevItem.getTimestamp() + " " + (prevItem.getTimestamp() - beginSlice.getTimestamp()) + " ms missing!"); beginSlice = null; } } } prevItem = currItem; } if (beginSlice != null) { System.out.println("TimeRange " + beginSlice.getTimestamp() + " - " + prevItem.getTimestamp() + " " + (prevItem.getTimestamp() - beginSlice.getTimestamp()) + " ms missing!"); } long expectedItems = ((time_to - time_from) / tdb.getMilliseconds()) - 1; System.out.println(" (" + (list.size() - expectedItems) + ")"); return list; } catch (Exception e) { throw new ApiException(e.getMessage()); } }
From source file:org.apache.hadoop.yarn.util.TestFSDownload.java
@Test(timeout = 10000) public void testUniqueDestinationPath() throws Exception { Configuration conf = new Configuration(); FileContext files = FileContext.getLocalFSFileContext(conf); final Path basedir = files.makeQualified(new Path("target", TestFSDownload.class.getSimpleName())); files.mkdir(basedir, null, true);/*from ww w. j a v a 2s . c o m*/ conf.setStrings(TestFSDownload.class.getName(), basedir.toString()); ExecutorService singleThreadedExec = Executors.newSingleThreadExecutor(); LocalDirAllocator dirs = new LocalDirAllocator(TestFSDownload.class.getName()); Path destPath = dirs.getLocalPathForWrite(basedir.toString(), conf); destPath = new Path(destPath, Long.toString(uniqueNumberGenerator.incrementAndGet())); Path p = new Path(basedir, "dir" + 0 + ".jar"); LocalResourceVisibility vis = LocalResourceVisibility.PRIVATE; LocalResource rsrc = createJar(files, p, vis); FSDownload fsd = new FSDownload(files, UserGroupInformation.getCurrentUser(), conf, destPath, rsrc); Future<Path> rPath = singleThreadedExec.submit(fsd); singleThreadedExec.shutdown(); while (!singleThreadedExec.awaitTermination(1000, TimeUnit.MILLISECONDS)) ; Assert.assertTrue(rPath.isDone()); // Now FSDownload will not create a random directory to localize the // resource. Therefore the final localizedPath for the resource should be // destination directory (passed as an argument) + file name. Assert.assertEquals(destPath, rPath.get().getParent()); }