List of usage examples for java.util.concurrent ThreadPoolExecutor execute
public void execute(Runnable command)
From source file:org.apache.cxf.systest.jaxrs.JAXRSCxfContinuationsTest.java
private void doTestContinuation(String pathSegment) throws Exception { ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(1); executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/1", "1", "CXF in Action1", startSignal, doneSignal)); startSignal.countDown();//from w w w . j a va 2 s. c o m doneSignal.await(60, TimeUnit.SECONDS); executor.shutdownNow(); assertEquals("Not all invocations have completed", 0, doneSignal.getCount()); }
From source file:com.espertech.esper.example.stockticker.TestStockTickerMultithreaded.java
public void performTest(int numberOfThreads, int numberOfTicksToSend, int ratioPriceOutOfLimit, int numberOfSecondsWaitForCompletion) { final int totalNumTicks = numberOfTicksToSend + 2 * TestStockTickerGenerator.NUM_STOCK_NAMES; log.info(".performTest Generating data, numberOfTicksToSend=" + numberOfTicksToSend + " ratioPriceOutOfLimit=" + ratioPriceOutOfLimit); StockTickerEventGenerator generator = new StockTickerEventGenerator(); LinkedList stream = generator.makeEventStream(numberOfTicksToSend, ratioPriceOutOfLimit, TestStockTickerGenerator.NUM_STOCK_NAMES, StockTickerRegressionConstants.PRICE_LIMIT_PCT_LOWER_LIMIT, StockTickerRegressionConstants.PRICE_LIMIT_PCT_UPPER_LIMIT, StockTickerRegressionConstants.PRICE_LOWER_LIMIT, StockTickerRegressionConstants.PRICE_UPPER_LIMIT, true);// www. ja v a 2 s .com log.info(".performTest Send limit and initial tick events - singlethreaded"); for (int i = 0; i < TestStockTickerGenerator.NUM_STOCK_NAMES * 2; i++) { Object theEvent = stream.removeFirst(); epService.getEPRuntime().sendEvent(theEvent); } log.info(".performTest Loading thread pool work queue, numberOfRunnables=" + stream.size()); ThreadPoolExecutor pool = new ThreadPoolExecutor(0, numberOfThreads, 99999, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); for (Object theEvent : stream) { SendEventRunnable runnable = new SendEventRunnable(epService, theEvent); pool.execute(runnable); } log.info(".performTest Starting thread pool, threads=" + numberOfThreads); pool.setCorePoolSize(numberOfThreads); log.info(".performTest Listening for completion"); EPRuntimeUtil.awaitCompletion(epService.getEPRuntime(), totalNumTicks, numberOfSecondsWaitForCompletion, 1, 10); pool.shutdown(); // Check results : make sure the given ratio of out-of-limit stock prices was reported int expectedNumEmitted = (numberOfTicksToSend / ratioPriceOutOfLimit) + 1; assertTrue(listener.getSize() == expectedNumEmitted); log.info(".performTest Done test"); }
From source file:dk.dma.ais.lib.FileConvert.java
/** {@inheritDoc} */ @Override/*from w ww . ja va 2 s .c om*/ protected void run(Injector injector) throws Exception { configureFileEnding(); final EConsumer<String> consumer = new EConsumer<String>() { @Override public void accept(String s) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException, InterruptedException { Path path = Paths.get(s); LOG.debug("Started processing file " + path); Path endPath; if (keepFileStructure) { Path relative; relative = path; endPath = Paths.get(Paths.get(convertTo).toString(), relative.toString()); new File(endPath.toString()).mkdirs(); } else { endPath = Paths.get(""); } String filename = path.getFileName().toString(); if (!filename.endsWith(fileEnding)) filename = FilenameUtils.removeExtension(filename) + fileEnding; Path filePath = Paths.get(endPath.toString(), filename); LOG.debug("Output File: " + filePath.toString()); final OutputStream fos = new FileOutputStream(filePath.toString()); // 2 final boolean createSituationFolder = !StringUtils.isBlank(kmzSnapshotAt); final long snapshotAtEpochMillis = createSituationFolder ? LocalDateTime.parse(kmzSnapshotAt, formatter).toInstant(ZoneOffset.UTC).toEpochMilli() : -1; OutputStreamSink<AisPacket> sink; if ("kmz".equals(outputSinkFormat)) { //AisPacketKMZOutputSink(filter, createSituationFolder, createMovementsFolder, createTracksFolder, isPrimaryTarget, isSecondaryTarget, triggerSnapshot, snapshotDescriptionSupplier, movementInterpolationStep, supplyTitle, supplyDescription, iconHrefSupplier); sink = AisPacketOutputSinks.newKmzSink(e -> true, // this.filter = e -> true; createSituationFolder, // this.createSituationFolder = true; true, // createMovementsFolder = true; true, // this.createTracksFolder = true; e -> kmzPrimaryMmsi <= 0 ? false : e.tryGetAisMessage().getUserId() == kmzPrimaryMmsi, // this.isPrimaryTarget = e -> false; e -> kmzSecondaryMmsi <= 0 ? false : e.tryGetAisMessage().getUserId() == kmzSecondaryMmsi, // this.isSecondaryTarget = e -> false; e -> e.getBestTimestamp() >= snapshotAtEpochMillis, // this.triggerSnapshot = e -> false; () -> "Situation at " + kmzSnapshotAt, // this.snapshotDescriptionSupplier = null; () -> 10, // this.title = defaultTitleSupplier; () -> "description", // this.description = defaultDescriptionSupplier; () -> "10", //this.movementInterpolationStep = defaultMovementInterpolationStepSupplier; (shipTypeCargo, navigationalStatus) -> "" // this.iconHrefSupplier = defaultIconHrefSupplier; ); } else sink = AisPacketOutputSinks.getOutputSink(outputSinkFormat, columns); sink.closeWhenFooterWritten(); AisPacketReader apis = AisPacketReader.createFromFile(path, false); apis.writeTo(fos, sink); apis.close(); fos.close(); } }; /* * Creates a pool of executors, 4 threads. Each thread will open a file using an aispacket reader, 10000 files can be * submitted to the queue, afterwards the calling thread will execute the job instead. */ ThreadPoolExecutor threadpoolexecutor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy()); for (final String s : sources) { threadpoolexecutor.execute(() -> { try { consumer.accept(s); } catch (Exception e) { e.printStackTrace(); } }); } threadpoolexecutor.shutdown(); threadpoolexecutor.awaitTermination(999, TimeUnit.DAYS); }
From source file:microsoft.exchange.webservices.data.core.request.HangingServiceRequestBase.java
/** * Perform any bookkeeping needed when we connect * @throws XMLStreamException the XML stream exception */// www .jav a 2s. c om private void internalOnConnect() throws XMLStreamException, IOException, EWSHttpException { if (!this.isConnected()) { this.isConnected = true; if (this.getService().isTraceEnabledFor(TraceFlags.EwsResponseHttpHeaders)) { // Trace Http headers this.getService().processHttpResponseHeaders(TraceFlags.EwsResponseHttpHeaders, this.response); } int poolSize = 1; int maxPoolSize = 1; long keepAliveTime = 10; final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(1); ThreadPoolExecutor threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue); threadPool.execute(new Runnable() { public void run() { parseResponses(); } }); threadPool.shutdown(); } }
From source file:cf.randers.scd.CommandLineInterface.java
private void run() { if (params == null) return;/*from ww w. j av a 2 s . c om*/ LOGGER.info("Making temp dir..."); File tmpDir = new File("tmp/"); File outDir = new File(outputDirectory); //noinspection ResultOfMethodCallIgnored tmpDir.mkdirs(); //noinspection ResultOfMethodCallIgnored outDir.mkdirs(); BlockingQueue<Runnable> tasks = new ArrayBlockingQueue<>(params.size()); maximumConcurrentConnections = Math.min(params.size(), maximumConcurrentConnections > params.size() ? params.size() : maximumConcurrentConnections); ThreadPoolExecutor executor = new ThreadPoolExecutor(maximumConcurrentConnections, maximumConcurrentConnections, 0, TimeUnit.NANOSECONDS, tasks); LOGGER.info("Starting to execute " + params.size() + " thread(s)..."); for (String param : params) { executor.execute(() -> { LOGGER.info("Started thread for " + param); Map json; byte[] artworkBytes = new byte[0]; List<Track> toProcess = new ArrayList<>(); LOGGER.info("Resolving and querying track info..."); try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(new HttpGet(new URIBuilder() .setScheme("https").setHost("api.soundcloud.com").setPath("/resolve") .addParameter("url", param).addParameter("client_id", clientID).build())); InputStreamReader inputStreamReader = new InputStreamReader( response.getEntity().getContent())) { final int bufferSize = 1024; final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); for (;;) { int rsz = inputStreamReader.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz); } String rawJson = out.toString(); Album a = new Gson().fromJson(rawJson, Album.class); if (a.getTrackCount() == null) { Track tr = new Gson().fromJson(rawJson, Track.class); toProcess.add(tr); } toProcess.addAll(a.getTracks()); EntityUtils.consumeQuietly(response.getEntity()); } catch (Exception e) { e.printStackTrace(); return; } for (Track track : toProcess) { System.out.println(track.getId()); System.out.println(track.getTitle()); } for (Track track : toProcess) { LOGGER.info("Downloading mp3 to file..."); File tmpFile = new File("tmp/" + String.format("%d", track.getId()) + ".mp3"); try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client .execute(new HttpGet(track.getStreamUrl() + "?client_id=" + clientID))) { IOUtils.copy(response.getEntity().getContent(), new FileOutputStream(tmpFile)); EntityUtils.consumeQuietly(response.getEntity()); } catch (Exception e) { e.printStackTrace(); return; } boolean hasArtwork = track.getArtworkUrl() != null; if (hasArtwork) { LOGGER.info("Downloading artwork jpg into memory..."); try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute( new HttpGet(track.getArtworkUrl().replace("-large.jpg", "-t500x500.jpg") + "?client_id=" + clientID))) { artworkBytes = IOUtils.toByteArray(response.getEntity().getContent()); EntityUtils.consumeQuietly(response.getEntity()); } catch (Exception e) { e.printStackTrace(); return; } } try { LOGGER.info("Reading temp file into AudioFile object..."); // Read audio file from tmp directory AudioFile audioFile = AudioFileIO.read(tmpFile); // Set Artwork Tag tag = audioFile.getTagAndConvertOrCreateAndSetDefault(); if (hasArtwork) { StandardArtwork artwork = new StandardArtwork(); artwork.setBinaryData(artworkBytes); artwork.setImageFromData(); tag.addField(artwork); } tag.addField(FieldKey.TITLE, track.getTitle()); tag.addField(FieldKey.ARTIST, track.getUser().getUsername()); LOGGER.info("Saving audio file..."); System.out.println( outDir.getAbsolutePath() + "/" + String.format(outputformat, track.getId())); new AudioFileIO().writeFile(audioFile, outDir.getAbsolutePath() + "/" + String.format(outputformat, track.getId())); tmpFile.deleteOnExit(); } catch (Exception e) { e.printStackTrace(); } } File[] listFiles = tmpDir.listFiles(); if (listFiles == null) { return; } for (File file : listFiles) { file.delete(); } }); } executor.shutdown(); }
From source file:de.th.wildau.dsc.sne.webserver.WebServer.java
/** * Web server / main constructor.//w ww. ja v a 2 s . c o m * * @param startArguments */ public WebServer(String[] startArguments) { loadConfiguration(startArguments); Log.debug(Configuration.getInstance().toString()); Log.debug("Information about the OS: " + System.getProperty("os.name") + " - " + System.getProperty("os.version") + " - " + System.getProperty("os.arch")); if (Configuration.getConfig().getProxyHost() != null) { Log.debug("setup proxy configuration"); System.setProperty("http.proxyHost", Configuration.getConfig().getProxyHost()); System.setProperty("http.proxyPort", String.valueOf(Configuration.getConfig().getProxyPort())); } Log.debug("find supported scripting languages"); supportedScriptLanguages = Collections.unmodifiableList(ScriptExecutor.getSupportedScriptLanguages()); Log.debug("Supported Script Languages " + Arrays.toString(supportedScriptLanguages.toArray())); Log.info("instantiating web server"); try { ServerSocket server = new ServerSocket(Configuration.getConfig().getServerPort()); Log.debug("bound port " + Configuration.getConfig().getServerPort()); int corePoolSize = Runtime.getRuntime().availableProcessors(); int maxPoolSize = (2 * corePoolSize) + 1; Log.debug("core/max pool size: " + corePoolSize + "/" + maxPoolSize); LinkedBlockingQueue<Runnable> workerQueue = new LinkedBlockingQueue<Runnable>(); long keepAliveTime = 30; /* * keepAliveTime - If the pool currently has more than corePoolSize * threads, excess threads will be terminated if they have been idle * for more than the keepAliveTime. */ ThreadPoolExecutor threadPool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, workerQueue); threadPool.prestartAllCoreThreads(); Socket socket = null; while (true) { try { socket = server.accept(); Log.info(socket.getInetAddress().getHostName() + " client request"); threadPool.execute(new HttpHandler(socket)); Log.debug("current threads: " + threadPool.getActiveCount()); } catch (final IOException ex) { Log.error("Connection failed!", ex); } catch (final RejectedExecutionException ex) { // XXX [sne] RejectedExecutionException // http://stackoverflow.com/questions/1519725/why-does-executors-newcachedthreadpool-throw-java-util-concurrent-rejectedexecut // http://www.javamex.com/tutorials/threads/thread_pools_queues.shtml // http://stackoverflow.com/questions/2001086/how-to-make-threadpoolexecutors-submit-method-block-if-it-is-saturated Log.error("RejectedExecutionException", ex); socket.close(); } catch (final Exception ex) { Log.fatal("Unknown error!", ex); } } } catch (final IOException ex) { Log.fatal("Can not start the server!", ex); System.err.println("Can not start the server! " + ex.getMessage()); } catch (final Exception ex) { Log.fatal("Unknown error!", ex); } }
From source file:com.espertech.esper.filter.TestIndexTreeBuilderMultithreaded.java
private void performMultithreadedTest(FilterHandleSetNode topNode, int numberOfThreads, int numberOfRunnables, int numberOfSecondsSleep) throws Exception { log.info(".performMultithreadedTest Loading thread pool work queue,numberOfRunnables=" + numberOfRunnables); ThreadPoolExecutor pool = new ThreadPoolExecutor(0, numberOfThreads, 99999, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); for (int i = 0; i < numberOfRunnables; i++) { IndexTreeBuilderRunnable runnable = new IndexTreeBuilderRunnable(eventType, topNode, testFilterSpecs, matchedEvents, unmatchedEvents); pool.execute(runnable); }/*from w ww .ja v a 2 s . c o m*/ log.info(".performMultithreadedTest Starting thread pool, threads=" + numberOfThreads); pool.setCorePoolSize(numberOfThreads); // Sleep X seconds sleep(numberOfSecondsSleep); log.info(".performMultithreadedTest Completed, numberOfRunnables=" + numberOfRunnables + " numberOfThreads=" + numberOfThreads + " completed=" + pool.getCompletedTaskCount()); pool.shutdown(); pool.awaitTermination(1, TimeUnit.SECONDS); assertTrue(pool.getCompletedTaskCount() == numberOfRunnables); }
From source file:com.intel.hadoopRPCBenchmark.HadoopRPCBenchmarkEngine.java
public void testRPCThoughput() throws Exception { // Start RPC engine enginePreparer.prepareServer();/* w w w . j a va2 s . c o m*/ startRPCServer(); enginePreparer.prepareClient(); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); List<DoThoughputTest> tasks = new ArrayList<DoThoughputTest>(clientNum); long start = System.currentTimeMillis(); for (int i = 0; i <= clientNum; i++) { DoThoughputTest task = new DoThoughputTest(); executor.execute(task); tasks.add(task); } while (true) { long end = System.currentTimeMillis(); if (end - start <= execTime * 1000) { Thread.sleep(1000); } else { break; } } long totalPingCount = 0; for (DoThoughputTest task : tasks) { totalPingCount += task.getPingCount(); } System.out.println("Total ping count: " + totalPingCount); executor.shutdown(); // Stop RPC engine enginePreparer.cleanup(); stopRPCServer(); }
From source file:io.anserini.index.IndexCollection.java
public void run() throws IOException, InterruptedException { final long start = System.nanoTime(); LOG.info("Starting indexer..."); int numThreads = args.threads; final Directory dir = FSDirectory.open(indexPath); final EnglishAnalyzer analyzer = args.keepStopwords ? new EnglishAnalyzer(CharArraySet.EMPTY_SET) : new EnglishAnalyzer(); final IndexWriterConfig config = new IndexWriterConfig(analyzer); config.setSimilarity(new BM25Similarity()); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); config.setRAMBufferSizeMB(args.memorybufferSize); config.setUseCompoundFile(false);/*from w w w . j av a 2 s .c o m*/ config.setMergeScheduler(new ConcurrentMergeScheduler()); final IndexWriter writer = new IndexWriter(dir, config); final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numThreads); final List<Path> segmentPaths = collection.getFileSegmentPaths(); final int segmentCnt = segmentPaths.size(); LOG.info(segmentCnt + " files found in " + collectionPath.toString()); for (int i = 0; i < segmentCnt; i++) { executor.execute(new IndexerThread(writer, collection, segmentPaths.get(i))); } executor.shutdown(); try { // Wait for existing tasks to terminate while (!executor.awaitTermination(1, TimeUnit.MINUTES)) { LOG.info(String.format("%.2f percent completed", (double) executor.getCompletedTaskCount() / segmentCnt * 100.0d)); } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted executor.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } if (segmentCnt != executor.getCompletedTaskCount()) { throw new RuntimeException("totalFiles = " + segmentCnt + " is not equal to completedTaskCount = " + executor.getCompletedTaskCount()); } int numIndexed = writer.maxDoc(); try { writer.commit(); if (args.optimize) writer.forceMerge(1); } finally { try { writer.close(); } catch (IOException e) { // It is possible that this happens... but nothing much we can do at this point, // so just log the error and move on. LOG.error(e); } } LOG.info("Indexed documents: " + counters.indexedDocuments.get()); LOG.info("Empty documents: " + counters.emptyDocuments.get()); LOG.info("Errors: " + counters.errors.get()); final long durationMillis = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS); LOG.info("Total " + numIndexed + " documents indexed in " + DurationFormatUtils.formatDuration(durationMillis, "HH:mm:ss")); }
From source file:bigbird.benchmark.HttpBenchmark.java
public void execute() { params = getHttpParams(socketTimeout, useHttp1_0); for (RequestGenerator g : requestGenerators) { g.setParameters(params);/*from www .java 2 s. co m*/ } host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); ThreadPoolExecutor workerPool = new ThreadPoolExecutor(threads, threads, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { public Thread newThread(Runnable r) { return new Thread(r, "ClientPool"); } }); workerPool.prestartAllCoreThreads(); BenchmarkWorker[] workers = new BenchmarkWorker[threads]; for (int i = 0; i < threads; i++) { workers[i] = new BenchmarkWorker(params, verbosity, requestGenerators[i], host, requests, keepAlive); workerPool.execute(workers[i]); } while (workerPool.getCompletedTaskCount() < threads) { Thread.yield(); try { Thread.sleep(1000); } catch (InterruptedException ignore) { } } workerPool.shutdown(); ResultProcessor.printResults(workers, host, url.toString(), contentLength); }