List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
From source file:net.solarnetwork.node.backup.DefaultBackupManager.java
private static ExecutorService defaultExecutorService() { // we want at most one backup happening at a time by default return new ThreadPoolExecutor(0, 1, 5, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(3, true)); }
From source file:de.th.wildau.dsc.sne.webserver.WebServer.java
/** * Web server / main constructor./*from w w w . java2 s . co 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:org.apache.http.impl.client.cache.AsynchronousValidator.java
/** * Create AsynchronousValidator which will make revalidation requests * using the supplied {@link CachingHttpClient}, and * a {@link ThreadPoolExecutor} generated according to the thread * pool settings provided in the given {@link CacheConfig}. * @param cachingClient used to execute asynchronous requests * @param config specifies thread pool settings. See * {@link CacheConfig#getAsynchronousWorkersMax()}, * {@link CacheConfig#getAsynchronousWorkersCore()}, * {@link CacheConfig#getAsynchronousWorkerIdleLifetimeSecs()}, * and {@link CacheConfig#getRevalidationQueueSize()}. *//* w w w . ja v a 2 s . c o m*/ public AsynchronousValidator(CachingHttpClient cachingClient, CacheConfig config) { this(cachingClient, new ThreadPoolExecutor(config.getAsynchronousWorkersCore(), config.getAsynchronousWorkersMax(), config.getAsynchronousWorkerIdleLifetimeSecs(), TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(config.getRevalidationQueueSize()))); }
From source file:org.apache.http.impl.client.cache.AsynchronousAsyncValidator.java
/** * Create AsynchronousValidator which will make revalidation requests using * the supplied {@link CachingHttpAsyncClient}, and a {@link ThreadPoolExecutor} * generated according to the thread pool settings provided in the given * {@link CacheConfig}./* w ww . j a v a2 s . c o m*/ * * @param cachingClient * used to execute asynchronous requests * @param config * specifies thread pool settings. See * {@link CacheConfig#getAsynchronousWorkersMax()}, * {@link CacheConfig#getAsynchronousWorkersCore()}, * {@link CacheConfig#getAsynchronousWorkerIdleLifetimeSecs()}, * and {@link CacheConfig#getRevalidationQueueSize()}. */ public AsynchronousAsyncValidator(final CachingHttpAsyncClient cachingClient, final CacheConfig config) { this(cachingClient, new ThreadPoolExecutor(config.getAsynchronousWorkersCore(), config.getAsynchronousWorkersMax(), config.getAsynchronousWorkerIdleLifetimeSecs(), TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(config.getRevalidationQueueSize()))); }
From source file:cf.randers.scd.CommandLineInterface.java
private void run() { if (params == null) return;// w w w. j ava2 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:com.fusesource.forge.jmstest.threading.SteppablePool.java
public ExecutorService getExecutor() { if (executor == null) { executor = new ThreadPoolExecutor(minPoolSize, maxPoolSize, getIdleTimeout(), TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }// w ww . j a v a 2 s.c om return executor; }
From source file:org.geoserver.wps.executor.DefaultProcessManager.java
public void setMaxSynchronousProcesses(int maxSynchronousProcesses) { if (synchService == null) { // create a fixed size pool. If we allow a delta between core and max // the pool will create new threads only if the queue is full, but the linked queue never is synchService = new ThreadPoolExecutor(maxSynchronousProcesses, maxSynchronousProcesses, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } else {// w ww . j av a 2 s .c o m synchService.setCorePoolSize(maxSynchronousProcesses); synchService.setMaximumPoolSize(maxSynchronousProcesses); } }
From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java
@Test public void testAssignSameClusterConflict() throws Exception { Semaphore idleWorkersSemaphore = new Semaphore(0); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() { });//from w ww. j a v a 2 s . c o m Task task = getSleepIncrementTask(); workerPool.assignTask(task); Thread.sleep(2000); Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue()); workerPool.assignTask(task); Assert.assertEquals(1, idleWorkersSemaphore.availablePermits()); Thread.sleep(100); Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue()); Thread.sleep(1000); }
From source file:org.apache.eagle.alert.engine.publisher.impl.AlertEmailPublisher.java
@Override @SuppressWarnings("rawtypes") public void init(Config config, Publishment publishment, Map conf) throws Exception { super.init(config, publishment, conf); this.serverHost = config.hasPath(MetadataServiceClientImpl.EAGLE_CORRELATION_SERVICE_HOST) ? config.getString(MetadataServiceClientImpl.EAGLE_CORRELATION_SERVICE_HOST) : "localhost"; this.serverPort = config.hasPath(MetadataServiceClientImpl.EAGLE_CORRELATION_SERVICE_PORT) ? config.getInt(MetadataServiceClientImpl.EAGLE_CORRELATION_SERVICE_PORT) : 80;/* w w w . j a v a 2s . c o m*/ this.mailClientProperties = parseMailClientConfig(config); executorPool = new ThreadPoolExecutor(DEFAULT_THREAD_POOL_CORE_SIZE, DEFAULT_THREAD_POOL_MAX_SIZE, DEFAULT_THREAD_POOL_SHRINK_TIME, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); LOG.info(" Creating Email Generator... "); if (publishment.getProperties() != null) { emailConfig = new HashMap<>(publishment.getProperties()); emailGenerator = createEmailGenerator(emailConfig); } }
From source file:it.infn.ct.futuregateway.apiserver.utils.ThreadPoolFactory.java
/** * Create a new ExecutorService.// w w w . j a v a2s .co m * The ExecutorService is based on the ThreadPoolExecutor but only * a subset of parameter can be specified. * * @param threadPoolSize The initial and minimum size of the pool * @param maxThreadPoolSize The maximum size of the pool * @param maxThreadIdleTime The time in milliseconds a thread can be idle * @return The new ExecutorService */ public static ExecutorService getThreadPool(final int threadPoolSize, final int maxThreadPoolSize, final int maxThreadIdleTime) { return new ThreadPoolExecutor(threadPoolSize, maxThreadPoolSize, maxThreadIdleTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }