List of usage examples for java.util.concurrent ExecutorService awaitTermination
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
From source file:org.hibernate.search.test.performance.scenario.TestExecutor.java
private void scheduleTasksAndStart(TestScenarioContext ctx, long cyclesCount) { ExecutorService executor = newAutoStoppingErrorReportingThreadPool(ctx); for (int i = 0; i < cyclesCount; i++) { for (Runnable task : ctx.tasks) { executor.execute(task);//from w w w. j a va2 s . c o m } } try { ctx.executionStopWatch.start(); ctx.startSignal.countDown(); executor.shutdown(); executor.awaitTermination(1, TimeUnit.DAYS); ctx.executionStopWatch.stop(); } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:com.espertech.esper.multithread.TestMTStmtNamedWindowUpdate.java
private void trySend(int numThreads, int numEventsPerThread) throws Exception { Configuration config = SupportConfigFactory.getConfiguration(); config.addEventType("SupportBean", SupportBean.class); engine = EPServiceProviderManager.getDefaultProvider(config); engine.initialize();/*w w w . ja v a 2 s . co m*/ // setup statements engine.getEPAdministrator().createEPL( "create window MyWindow.std:unique(theString, intPrimitive) as select * from SupportBean"); engine.getEPAdministrator() .createEPL("insert into MyWindow select * from SupportBean(boolPrimitive = true)"); engine.getEPAdministrator().createEPL("on SupportBean(boolPrimitive = false) sb " + "update MyWindow win set intBoxed = win.intBoxed + 1, doublePrimitive = win.doublePrimitive + sb.doublePrimitive" + " where sb.theString = win.theString and sb.intPrimitive = win.intPrimitive"); // send primer events, initialize totals Map<MultiKeyUntyped, UpdateTotals> totals = new HashMap<MultiKeyUntyped, UpdateTotals>(); for (int i = 0; i < NUM_STRINGS; i++) { for (int j = 0; j < NUM_INTS; j++) { SupportBean primer = new SupportBean(Integer.toString(i), j); primer.setBoolPrimitive(true); primer.setIntBoxed(0); primer.setDoublePrimitive(0); engine.getEPRuntime().sendEvent(primer); MultiKeyUntyped key = new MultiKeyUntyped(primer.getTheString(), primer.getIntPrimitive()); totals.put(key, new UpdateTotals(0, 0)); } } // execute long startTime = System.currentTimeMillis(); ExecutorService threadPool = Executors.newFixedThreadPool(numThreads); Future<StmtNamedWindowUpdateCallable.UpdateResult> future[] = new Future[numThreads]; for (int i = 0; i < numThreads; i++) { future[i] = threadPool .submit(new StmtNamedWindowUpdateCallable("Thread" + i, engine, numEventsPerThread)); } threadPool.shutdown(); threadPool.awaitTermination(10, TimeUnit.SECONDS); long endTime = System.currentTimeMillis(); // total up result long deltaCumulative = 0; for (int i = 0; i < numThreads; i++) { StmtNamedWindowUpdateCallable.UpdateResult result = future[i].get(); deltaCumulative += result.getDelta(); for (StmtNamedWindowUpdateCallable.UpdateItem item : result.getUpdates()) { MultiKeyUntyped key = new MultiKeyUntyped(item.getTheString(), item.getIntval()); UpdateTotals total = totals.get(key); if (total == null) { throw new RuntimeException("Totals not found for key " + key); } total.setNum(total.getNum() + 1); total.setSum(total.getSum() + item.getDoublePrimitive()); } } // compare EventBean[] rows = engine.getEPRuntime().executeQuery("select * from MyWindow").getArray(); assertEquals(rows.length, totals.size()); long totalUpdates = 0; for (EventBean row : rows) { UpdateTotals total = totals.get(new MultiKeyUntyped(row.get("theString"), row.get("intPrimitive"))); assertEquals(total.getNum(), row.get("intBoxed")); assertEquals(total.getSum(), row.get("doublePrimitive")); totalUpdates += total.getNum(); } assertEquals(totalUpdates, numThreads * numEventsPerThread); //long deltaTime = endTime - startTime; //System.out.println("Totals updated: " + totalUpdates + " Delta cumu: " + deltaCumulative + " Delta pooled: " + deltaTime); }
From source file:org.openhab.binding.plclogo.internal.discovery.PLCDiscoveryService.java
@Override protected void startScan() { stopScan();//ww w . j a v a2 s. c o m logger.debug("Start scan for LOGO! bridge"); Enumeration<NetworkInterface> devices = null; try { devices = NetworkInterface.getNetworkInterfaces(); } catch (SocketException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } Set<String> addresses = new TreeSet<>(); while ((devices != null) && devices.hasMoreElements()) { NetworkInterface device = devices.nextElement(); try { if (!device.isUp() || device.isLoopback()) { continue; } } catch (SocketException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } for (InterfaceAddress iface : device.getInterfaceAddresses()) { InetAddress address = iface.getAddress(); if (address instanceof Inet4Address) { String prefix = String.valueOf(iface.getNetworkPrefixLength()); SubnetUtils utilities = new SubnetUtils(address.getHostAddress() + "/" + prefix); addresses.addAll(Arrays.asList(utilities.getInfo().getAllAddresses())); } } } ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); for (String address : addresses) { try { executor.execute(new Runner(address)); } catch (RejectedExecutionException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } } try { executor.awaitTermination(CONNECTION_TIMEOUT * addresses.size(), TimeUnit.MILLISECONDS); } catch (InterruptedException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } executor.shutdown(); stopScan(); }
From source file:com.norconex.committer.AbstractFileQueueCommitterTest.java
@Test public void testMultipleCommitThread() throws Exception { final AtomicInteger counter = new AtomicInteger(); final AbstractFileQueueCommitter committer = new AbstractFileQueueCommitter() { @Override//www .jav a2s .c o m protected void commitAddition(IAddOperation operation) throws IOException { counter.incrementAndGet(); operation.delete(); } @Override protected void commitDeletion(IDeleteOperation operation) throws IOException { counter.incrementAndGet(); operation.delete(); } @Override protected void commitComplete() { } }; File queue = temp.newFolder(); committer.setQueueDir(queue.getPath()); // Use a bigger number to make sure the files are not // committed while they are added. committer.setQueueSize(1000); // Queue 50 files for additions for (int i = 0; i < 50; i++) { Properties metadata = new Properties(); committer.add(Integer.toString(i), IOUtils.toInputStream("hello world!"), metadata); } // Queue 50 files for deletions for (int i = 50; i < 100; i++) { Properties metadata = new Properties(); committer.remove(Integer.toString(i), metadata); } ExecutorService pool = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { pool.submit(new Runnable() { @Override public void run() { try { committer.commit(); } catch (Exception e) { e.printStackTrace(); } } }); } pool.shutdown(); pool.awaitTermination(10, TimeUnit.SECONDS); // Each file should have been processed exactly once assertEquals(100, counter.intValue()); // All files should have been processed Collection<File> files = FileUtils.listFiles(queue, null, true); assertTrue(files.isEmpty()); }
From source file:org.mimacom.maven.plugins.liferay.prepare.MultithreadedDownloader.java
public long download(String url, File dest, int progressStep, ProgressObserver progressObserver) throws IOException { dest.getParentFile().mkdirs();//www . ja v a 2 s. c o m if (dest.exists()) { dest.delete(); } Counter counter = new Counter(progressStep, progressObserver); GetMethod download = new GetMethod(url); if (canPartialDownload(download)) { ExecutorService es = Executors.newFixedThreadPool(threads); es.submit(new Download(httpClient, url, dest, 1000000, counter, download)); for (int i = 1; i < threads; i++) { es.submit(new Download(httpClient, url, dest, 1000000, counter, null)); } es.shutdown(); try { es.awaitTermination(60 * 60 * 10, TimeUnit.SECONDS); } catch (InterruptedException e) { //ignore } counter.notifyObserver(); if (!counter.isSuccess()) { throw new IOException(counter.getErrorMessage()); } } else { new Download(httpClient, url, dest, 1000000, counter, download).call(); counter.notifyObserver(); } return counter.getTotal(); }
From source file:edu.cmu.lti.oaqa.bioasq.concept.retrieval.GoPubMedConceptRetrievalExecutor.java
@Override public void process(JCas jcas) throws AnalysisEngineProcessException { AbstractQuery aquery = TypeUtil.getAbstractQueries(jcas).stream().findFirst().get(); String queryString = bopQueryStringConstructor.construct(aquery).replaceAll("[^A-Za-z0-9_\\-\"]+", " "); LOG.info("Query string: {}", queryString); List<ConceptSearchResult> concepts = Collections.synchronizedList(new ArrayList<>()); ExecutorService es = Executors.newCachedThreadPool(); for (BioASQUtil.Ontology ontology : BioASQUtil.Ontology.values()) { es.execute(() -> {/*from ww w.ja v a2 s.co m*/ try { concepts.addAll(BioASQUtil.searchOntology(service, jcas, queryString, pages, hits, ontology)); } catch (IOException e) { throw new RuntimeException(e); } }); } es.shutdown(); try { if (!es.awaitTermination(timeout, TimeUnit.MINUTES)) { LOG.warn("Timeout occurs for one or some concept retrieval services."); } } catch (InterruptedException e) { throw new AnalysisEngineProcessException(e); } Map<String, List<ConceptSearchResult>> onto2concepts = concepts.stream() .collect(groupingBy(ConceptSearchResult::getSearchId)); for (Map.Entry<String, List<ConceptSearchResult>> entry : onto2concepts.entrySet()) { List<ConceptSearchResult> results = entry.getValue(); LOG.info("Retrieved {} concepts from {}", results.size(), entry.getKey()); if (LOG.isDebugEnabled()) { results.stream().limit(3).forEach(c -> LOG.debug(" - {}", TypeUtil.toString(c))); } } TypeUtil.rankedSearchResultsByScore(concepts, limit).forEach(ConceptSearchResult::addToIndexes); }
From source file:jenkins.plugins.elanceodesk.workplace.notifier.HttpWorkerTest.java
@Test public void testSendingMultipleWebhooks() throws IOException, InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); HttpWorker worker1 = new HttpWorker("http://localhost:8000/test1", "test1body", 30000, 1, Mockito.mock(PrintStream.class)); HttpWorker worker2 = new HttpWorker("http://localhost:8000/test2", "test2body", 30000, 1, Mockito.mock(PrintStream.class)); executorService.submit(worker1);/* ww w.j a v a 2 s .c om*/ executorService.submit(worker2); executorService.shutdown(); executorService.awaitTermination(5, TimeUnit.SECONDS); Assert.assertTrue(MyHandler.getTest1Result()); Assert.assertTrue(MyHandler.getTest2Result()); }
From source file:com.sastix.cms.server.services.cache.UIDServiceTest.java
@Test public void massiveUIDCreatorTest() throws InterruptedException { String region1 = "r1"; String region2 = "r2"; regionIdsMap.put(region1, new HashMap<>()); regionIdsMap.put(region2, new HashMap<>()); ExecutorService executor = Executors.newFixedThreadPool(NTHREDS); for (int i = 0; i < numberOfTasks; i++) { String region = region1;/* w w w . j a va 2 s . c o m*/ if (i % 2 == 0) { region = region2; } Runnable worker = new UIDRunnable(region); executor.execute(worker); } try { latch.await(); } catch (InterruptedException E) { // handle } executor.shutdown(); executor.awaitTermination(5, TimeUnit.SECONDS); assertEquals(numberOfTasks, ids.size()); assertEquals(numberOfTasks / 2, regionIdsMap.get(region1).size()); assertEquals(numberOfTasks / 2, regionIdsMap.get(region2).size()); assertTrue(!duplicateFound); LOG.info("Finished all threads"); }
From source file:org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServerTest.java
@Test public void testConcurrentUpdate() throws Exception { TestServlet.clear();/*from ww w . j av a 2s . 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); }