List of usage examples for java.util.concurrent ExecutorService submit
Future<?> submit(Runnable task);
From source file:io.pravega.test.integration.MultiReadersEndToEndTest.java
private Collection<Integer> readAllEvents(final int numParallelReaders, ClientFactory clientFactory, final String readerGroupName, final int numSegments) { ConcurrentLinkedQueue<Integer> read = new ConcurrentLinkedQueue<>(); final ExecutorService executorService = Executors.newFixedThreadPool(numParallelReaders, new ThreadFactoryBuilder().setNameFormat("testreader-pool-%d").build()); List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < numParallelReaders; i++) { futures.add(executorService.submit(() -> { final String readerId = UUID.randomUUID().toString(); @Cleanup/*from ww w . ja v a 2s.c o m*/ final EventStreamReader<Integer> reader = clientFactory.createReader(readerId, readerGroupName, new IntegerSerializer(), ReaderConfig.builder().build()); int emptyCount = 0; while (emptyCount <= numSegments) { try { final Integer integerEventRead = reader.readNextEvent(100).getEvent(); if (integerEventRead != null) { read.add(integerEventRead); emptyCount = 0; } else { emptyCount++; } } catch (ReinitializationRequiredException e) { throw new RuntimeException(e); } } })); } // Wait until all readers are done. futures.forEach(f -> FutureHelpers.getAndHandleExceptions(f, RuntimeException::new)); executorService.shutdownNow(); return read; }
From source file:com.byteatebit.nbserver.simple.TestSimpleNbServer.java
@Test public void testBlockingProcessWithSingleThread() throws IOException, ExecutionException, InterruptedException { SimpleNbServer simpleNbServer = SimpleNbServer.Builder.builder().withConfig(SimpleNbServerConfig.builder() .withNbServiceConfig(NbServiceConfig.Builder.builder().withIoTaskTimeoutMs(20000l).withNumThreads(1) .withNumIoThreads(1).build()) .withListenAddress("localhost").withListenPort(1111).build()) .withConnectorFactory(TcpConnectorFactory.Builder.builder() .withConnectedSocketTask(new TcpConnectTestHandler()).build()) .build();/*from w ww .j ava 2 s . c o m*/ simpleNbServer.start(); ExecutorService executorService = Executors.newFixedThreadPool(2); String message1 = "sleep"; String message2 = "this is message2"; try { long beginTime = System.currentTimeMillis(); executorService.submit(() -> sendMessage(message1)); Thread.sleep(1000); Future<String> future = executorService.submit(() -> sendMessage(message2)); String response = future.get(); long elapsed = System.currentTimeMillis() - beginTime; Assert.assertEquals(message2, response); // the sleep command in the echo service sleeps for 5 seconds Assert.assertTrue(elapsed >= 5000); } finally { simpleNbServer.shutdown(); } }
From source file:broadwick.Broadwick.java
/** * Run the Broadwick framework./*from www.jav a2 s. com*/ */ @SuppressWarnings("squid:S1147") public void run() { if (project != null) { final StopWatch sw = new StopWatch(); sw.start(); // initialise the data, by reading the data files and/or the database. log.info("Running broadwick {}", BroadwickVersion.getVersionAndTimeStamp()); try (DataReader dr = new DataReader(project.getData())) { final Map<String, Model> registeredModels = registerModels(project, dr.getLookup()); log.info("Running broadwick for the following models {}", registeredModels.keySet()); // Run the models, each on a separate thread. // TODO in a single-threaded grid environment we cannot do this - need to think again here.... final int poolSize = registeredModels.size(); if (poolSize > 0) { final ThreadFactory threadFactory = new ThreadFactoryBuilder() .setNameFormat("BroadwickModels-%d").setDaemon(true).build(); final ExecutorService es = Executors.newFixedThreadPool(poolSize, threadFactory); //final StopWatch sw = new StopWatch(); for (final Entry<String, Model> entry : registeredModels.entrySet()) { es.submit(new Runnable() { @Override public void run() { final String modelName = entry.getKey(); final Model model = entry.getValue(); try { log.info("Running {} [{}]", modelName, model.getClass().getCanonicalName()); model.init(); model.run(); model.finalise(); } catch (Exception ex) { log.error("Error running model {}. see stack trace from details.", modelName); log.error("{}", Throwables.getStackTraceAsString(ex)); } } }); } es.shutdown(); while (!es.isTerminated()) { es.awaitTermination(10, TimeUnit.SECONDS); } //sw.stop(); //log.trace("Finished {} simulations in {}.", maxSimulations, sw); } } catch (Exception ex) { log.error("{}", ex.getLocalizedMessage()); log.error("{}", Throwables.getStackTraceAsString(ex)); log.error("Something went wrong. See previous messages for details."); } log.info("Simulation complete. {}", sw.toString()); // In rare circumstances, where exceptions are caught and the simulation has completed but // there are still tasks being submitted to the executor, we need to force the progam to quit. Runtime.getRuntime().exit(0); } }
From source file:com.byteatebit.nbserver.simple.TestSimpleNbServer.java
@Test public void testBlockingProcessWithSeparateIoThread() throws IOException, ExecutionException, InterruptedException { SimpleNbServer simpleNbServer = SimpleNbServer.Builder.builder().withConfig(SimpleNbServerConfig.builder() .withNbServiceConfig(NbServiceConfig.Builder.builder().withIoTaskTimeoutMs(20000l).withNumThreads(3) .withNumIoThreads(1).build()) .withListenAddress("localhost").withListenPort(1111).build()) .withConnectorFactory(TcpConnectorFactory.Builder.builder() .withConnectedSocketTask(new TcpConnectTestHandler()).build()) .build();/* w ww. j a v a 2s . c o m*/ simpleNbServer.start(); ExecutorService executorService = Executors.newFixedThreadPool(2); String message1 = "sleep"; String message2 = "this is message2"; try { long beginTime = System.currentTimeMillis(); executorService.submit(() -> sendMessage(message1)); Thread.sleep(1000); Future<String> future = executorService.submit(() -> sendMessage(message2)); String response = future.get(); long elapsed = System.currentTimeMillis() - beginTime; Assert.assertEquals(message2, response); // the sleep command in the echo service sleeps for 2 seconds Assert.assertTrue(elapsed < 5000); } finally { simpleNbServer.shutdown(); } }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessMutexBase.java
@Test public void testReentrantSingleLock() throws Exception { final int THREAD_QTY = 10; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/*from w w w.java 2 s . co m*/ try { final AtomicBoolean hasLock = new AtomicBoolean(false); final AtomicBoolean isFirst = new AtomicBoolean(true); final Semaphore semaphore = new Semaphore(1); final InterProcessLock mutex = makeLock(client); List<Future<Object>> threads = Lists.newArrayList(); ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < THREAD_QTY; ++i) { Future<Object> t = service.submit(new Callable<Object>() { @Override public Object call() throws Exception { semaphore.acquire(); mutex.acquire(); Assert.assertTrue(hasLock.compareAndSet(false, true)); try { if (isFirst.compareAndSet(true, false)) { semaphore.release(THREAD_QTY - 1); while (semaphore.availablePermits() > 0) { Thread.sleep(100); } } else { Thread.sleep(100); } } finally { mutex.release(); hasLock.set(false); } return null; } }); threads.add(t); } for (Future<Object> t : threads) { t.get(); } } finally { client.close(); } }
From source file:io.undertow.server.handlers.accesslog.AccessLogFileTestCase.java
@Test public void testLogLotsOfThreads() throws IOException, InterruptedException, ExecutionException { Path directory = logDirectory; Path logFileName = directory.resolve("server2.log"); DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), directory, "server2."); CompletionLatchHandler latchHandler; DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(NUM_REQUESTS * NUM_THREADS, new AccessLogHandler(HELLO_HANDLER, logReceiver, "REQ %{i,test-header}", AccessLogFileTestCase.class.getClassLoader()))); ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); try {/*w w w . j a va 2 s .c o m*/ final List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < NUM_THREADS; ++i) { final int threadNo = i; futures.add(executor.submit(new Runnable() { @Override public void run() { TestHttpClient client = new TestHttpClient(); try { for (int i = 0; i < NUM_REQUESTS; ++i) { HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path"); get.addHeader("test-header", "thread-" + threadNo + "-request-" + i); HttpResponse result = client.execute(get); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); final String response = HttpClientUtils.readResponse(result); Assert.assertEquals("Hello", response); } } catch (IOException e) { throw new RuntimeException(e); } finally { client.getConnectionManager().shutdown(); } } })); } for (Future<?> future : futures) { future.get(); } } finally { executor.shutdown(); } latchHandler.await(); logReceiver.awaitWrittenForTest(); String completeLog = new String(Files.readAllBytes(logFileName)); for (int i = 0; i < NUM_THREADS; ++i) { for (int j = 0; j < NUM_REQUESTS; ++j) { Assert.assertTrue(completeLog.contains("REQ thread-" + i + "-request-" + j)); } } }
From source file:com.graphhopper.jsprit.analysis.toolbox.ConcurrentBenchmarker.java
public void run() { System.out.println("start benchmarking [nuOfInstances=" + benchmarkInstances.size() + "][runsPerInstance=" + runs + "]"); double startTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1); List<Future<BenchmarkResult>> futures = new ArrayList<Future<BenchmarkResult>>(); for (final BenchmarkInstance p : benchmarkInstances) { Future<BenchmarkResult> futureResult = executor.submit(new Callable<BenchmarkResult>() { @Override/*w w w. j a va 2 s . c o m*/ public BenchmarkResult call() throws Exception { return runAlgoAndGetResult(p); } }); futures.add(futureResult); } try { int count = 1; for (Future<BenchmarkResult> f : futures) { BenchmarkResult r = f.get(); print(r, count); results.add(f.get()); count++; } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } executor.shutdown(); print(results); System.out.println("done [time=" + (System.currentTimeMillis() - startTime) / 1000 + "sec]"); }
From source file:be.fgov.kszbcss.rhq.websphere.WebSphereServerPlugin.java
private void checkAndMarkUnconfiguredResources(LinkedList<Resource> resources, final Tag unconfiguredTag) { final Subject user = LookupUtil.getSubjectManager().getOverlord(); final TagManagerLocal tagManager = LookupUtil.getTagManager(); final ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); // To accelerate things, we schedule a certain number of operations in parallel. // JBoss 6 has not yet ManagedExecutorService, so we're using unmanaged threads. ExecutorService executorService = Executors.newFixedThreadPool(6); for (final Resource resource : resources) { if (resource.getResourceType().isSupportsMissingAvailabilityType()) { executorService.submit(new Runnable() { @Override//from w w w.j av a2s .com public void run() { try { // the synchronous calls to retrieve availability don't convert AvailabilityType.MISSING to // DOWN, so we can make use of it to detect unconfigured resources if (resource.getCurrentAvailability() .getAvailabilityType() != AvailabilityType.DISABLED) { AvailabilityType currentAvailability = getCurrentAvailaibility(resource); if (currentAvailability == AvailabilityType.MISSING) { LOG.info("Tagging " + resource.getName() + " (" + resource.getId() + ") as unconfigured"); Set<Tag> tags = resource.getTags(); tags.add(unconfiguredTag); // using synchronized, because at least resourceManager didn't seem threadsafe synchronized (tagManager) { tagManager.updateResourceTags(user, resource.getId(), tags); } synchronized (resourceManager) { resourceManager.disableResources(user, new int[] { resource.getId() }); } } } else { AvailabilityType currentAvailability = getCurrentAvailaibility(resource); if (currentAvailability != AvailabilityType.UNKNOWN && currentAvailability != AvailabilityType.MISSING) { LOG.info(resource.getName() + " (" + resource.getId() + ") has reappeared in the WebSphere configuration; reenabling it"); Set<Tag> tags = resource.getTags(); tags.remove(unconfiguredTag); synchronized (tagManager) { tagManager.updateResourceTags(user, resource.getId(), tags); } synchronized (resourceManager) { resourceManager.enableResources(user, new int[] { resource.getId() }); } } } } catch (RuntimeException e) { LOG.error("Exception during availability check of resource " + resource.getName() + "(" + resource.getId() + ")"); } } }); } } try { executorService.shutdown(); executorService.awaitTermination(5, TimeUnit.MINUTES); } catch (InterruptedException e) { throw new IllegalStateException("Interrupted during availability check of autoUninventory", e); } }
From source file:com.ebay.jetstream.event.processor.esper.raw.EsperTest.java
@Test public void aggregationTest() { Configuration configuration = new Configuration(); configuration.configure(/*from www. j a v a2 s . c om*/ new File("src/test/java/com/ebay/jetstream/event/processor/esper/raw/EsperTestConfig.xml")); EPServiceProvider epService = EPServiceProviderManager.getProvider("EsperTest", configuration); EsperTestAggregationStatement esperStmt = new EsperTestAggregationStatement(epService.getEPAdministrator()); EsperTestAggregationListener listener = new EsperTestAggregationListener(); esperStmt.addListener(listener); ExecutorService threadPool = Executors.newCachedThreadPool(new EsperTestThreadFactory()); EsperTestAggregationRunnable runnables[] = new EsperTestAggregationRunnable[THREADS_NUM_AGGRTEST]; try { for (int i = 0; i < THREADS_NUM_AGGRTEST; i++) { runnables[i] = new EsperTestAggregationRunnable(epService, i); threadPool.submit(runnables[i]); } threadPool.shutdown(); threadPool.awaitTermination(200, TimeUnit.SECONDS); } catch (InterruptedException e) { fail("InterruptedException: " + e.getMessage()); } assertTrue("ExecutorService failed to shut down properly", threadPool.isShutdown()); assertEquals(THREADS_NUM_AGGRTEST * 2, listener.getCount()); assertEquals(THREADS_NUM_AGGRTEST, m_aggregationResults.size()); // only one result per oroginal event for (int i = 0; i < THREADS_NUM_AGGRTEST; i++) { assertEquals(11.0 + 4. * i, m_aggregationResults.get(i), 1.e-06); } assertEquals(THREADS_NUM_AGGRTEST, m_aggregationAvgResults.size()); // only one result per oroginal event for (int i = 0; i < THREADS_NUM_AGGRTEST; i++) { assertEquals((11.0 + 4. * i) / 4., m_aggregationAvgResults.get(i), 1.e-06); } }
From source file:com.tyndalehouse.step.tools.modules.ConvertXmlToOSISModule.java
private void convert() throws Exception { final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(1024); final ExecutorService executorService = new ThreadPoolExecutor(3, 3, 1, TimeUnit.DAYS, queue); final File[] files = SOURCE_DIRECTORY.listFiles(); for (final File f : files) { if (f.isDirectory()) { final File[] unzippedFiles = f.listFiles(); for (final File unzipped : unzippedFiles) { if (unzipped.getName().endsWith(".xml")) { executorService.submit(new Runnable() { @Override public void run() { try { convertToXml(f.getName(), unzipped); LOGGER.debug("Finished [{}], [{}] remaining", f.getName(), queue.size()); } catch (Exception e) { LOGGER.error("Failed to convert [{}]", f.getName(), e); }/* w w w . j a va2s . c o m*/ } }); break; } } // break; } } executorService.shutdown(); }