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:groovyx.net.http.AsyncHTTPBuilder.java
/** * Initializes threading parameters for the HTTPClient's * {@link ThreadSafeClientConnManager}, and this class' ThreadPoolExecutor. *///from w w w. j a v a2 s . c om protected void initThreadPools(final int poolSize, final ExecutorService threadPool) { if (poolSize < 1) throw new IllegalArgumentException("poolSize may not be < 1"); // Create and initialize HTTP parameters HttpParams params = client != null ? client.getParams() : new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, poolSize); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(poolSize)); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); super.client = new DefaultHttpClient(cm, params); this.threadPool = threadPool != null ? threadPool : new ThreadPoolExecutor(poolSize, poolSize, 120, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); }
From source file:eu.edisonproject.training.wsd.WikipediaOnline.java
private Map<CharSequence, List<CharSequence>> getCategories(Set<Term> terms) throws MalformedURLException, InterruptedException, ExecutionException { int maxT = 2; BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(maxT); ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue); // ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, // 5000L, TimeUnit.MILLISECONDS, // new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy()); Map<CharSequence, List<CharSequence>> cats = new HashMap<>(); Set<Future<Map<CharSequence, List<CharSequence>>>> set = new HashSet<>(); for (Term t : terms) { URL url = new URL(PAGE + "?action=query&format=json&prop=categories&pageids=" + t.getUid()); LOGGER.log(Level.FINE, url.toString()); WikiRequestor req = new WikiRequestor(url, t.getUid().toString(), 0); Future<Map<CharSequence, List<CharSequence>>> future = pool.submit(req); set.add(future);// ww w. j av a 2 s.c o m } pool.shutdown(); for (Future<Map<CharSequence, List<CharSequence>>> future : set) { while (!future.isDone()) { // LOGGER.log(Level.INFO, "Task is not completed yet...."); Thread.currentThread().sleep(10); } Map<CharSequence, List<CharSequence>> c = future.get(); if (c != null) { cats.putAll(c); } } return cats; }
From source file:org.kaaproject.kaa.server.verifiers.facebook.verifier.FacebookUserVerifier.java
@Override public void start() { LOG.info("facebook user verifier started"); tokenVerifiersPool = new ThreadPoolExecutor(0, configuration.getMaxParallelConnections(), MAX_SEC_FACEBOOK_REQUEST_TIME, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); httpClient = HttpClients.custom().setConnectionManager(connectionManager).build(); // Increase max total connection connectionManager.setMaxTotal(configuration.getMaxParallelConnections()); }
From source file:org.apache.drill.sql.client.ref.DrillRefImpl.java
public Enumerator<E> enumerator() { // TODO: use a completion service from the container final ExecutorCompletionService<Collection<RunOutcome>> service = new ExecutorCompletionService<Collection<RunOutcome>>( new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(10))); // Run the plan using an executor. It runs in a different thread, writing // results to our queue. ////from www. j av a 2 s. c om // TODO: use the result of task, and check for exceptions final Future<Collection<RunOutcome>> task = runRefInterpreterPlan(service); return new JsonEnumerator(task, queue, fields); }
From source file:org.apache.streams.threaded.controller.ThreadingController.java
private void checkSetup() { synchronized (this) { if (this.threadPoolExecutor == null) { this.threadPoolExecutor = new ThreadPoolExecutor(this.numThreads.get(), this.numThreads.get(), 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); this.threadPoolExecutor.setThreadFactory(new BasicThreadFactory.Builder().priority(this.priority) .namingPattern(this.name + "- %d").build()); this.listeningExecutorService = MoreExecutors.listeningDecorator(this.threadPoolExecutor); }//w w w . j av a 2 s .co m } }
From source file:org.obm.opush.PingHandlerTest.java
@Test @Ignore("OBMFULL-4125") public void test3BlockingClient() throws Exception { prepareMockNoChange(Arrays.asList(users.jaures)); opushServer.start();/*from w ww . j ava 2 s . c o m*/ OPClient opClient = testUtils.buildWBXMLOpushClient(users.jaures, opushServer.getHttpPort(), httpClient); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(20, 20, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>()); Stopwatch stopwatch = Stopwatch.createStarted(); List<Future<Document>> futures = new ArrayList<Future<Document>>(); for (int i = 0; i < 4; ++i) { futures.add(queuePingCommand(opClient, users.jaures, threadPoolExecutor)); } for (Future<Document> f : futures) { Document response = f.get(); checkNoChangeResponse(response); } checkExecutionTime(2, 5, stopwatch); }
From source file:org.lilyproject.testclientfw.BaseTestTool.java
public void startExecutor() { executor = new ThreadPoolExecutor(workers, workers, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100)); executor.setRejectedExecutionHandler(new WaitPolicy()); }
From source file:com.pinterest.rocksplicator.controller.DispatcherTest.java
@Test public void testChainedTaskWithError() throws Exception { TaskBase task = new SleepIncrementTask(100).andThen(new ThrowingTask("Oops...")) .andThen(new SleepIncrementTask(150)).getEntity(); final CountDownLatch latch = new CountDownLatch(2); FIFOTaskQueue tq = new FIFOTaskQueue(10) { @Override//from w ww .ja v a 2 s . c o m public boolean finishTask(final long id, final String output) { latch.countDown(); return super.finishTask(id, output); } @Override public boolean failTask(final long id, final String reason) { latch.countDown(); return super.failTask(id, reason); } @Override public long finishTaskAndEnqueueRunningTask(final long id, final String output, final TaskBase newTask, final String worker) { latch.countDown(); return super.finishTaskAndEnqueueRunningTask(id, output, newTask, worker); } }; tq.enqueueTask(task, Integer.toString(++nameCounter), 0); Semaphore idleWorkersSemaphore = new Semaphore(2); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2)); WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, tq); TaskDispatcher dispatcher = new TaskDispatcher(2, idleWorkersSemaphore, workerPool, tq); dispatcher.start(); Assert.assertTrue(latch.await(30, TimeUnit.SECONDS)); Assert.assertEquals(SleepIncrementTask.executionCounter.intValue(), 1); Assert.assertEquals(tq.getResult(0), "0"); Assert.assertEquals(tq.getResult(1), "Oops..."); dispatcher.stop(); }
From source file:com.bleum.canton.jms.scheduler.AbstractJMSScheduler.java
/** * Run the tasks using threads./* ww w . j a va 2 s . co m*/ * * @param tasks */ private void excuteTasks(List<JMSTask> tasks) { int tSize = tasks.size(); if (tSize <= 0) { return; } ThreadPoolExecutor executor = new ThreadPoolExecutor(1, threads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(maxTasksPerThread * threads)); for (final JMSTask task : tasks) { final int mRetry = this.maxRetry; final int mAckRetry = this.maxAckRetry; runFutures.add(executor.submit(new Runnable() { @Override public void run() { try { sendMessage(task); if (clientAck == JMSTaskConstant.NO_ACKNOWLEDGE) { jmsTaskDao.updateTaskCompeleted(task.getId()); } else if (clientAck == JMSTaskConstant.CLIENT_ACKNOWLEDGE) { // if sent maxRetry times, won't wait for // acknowledge, just complete it. jmsTaskDao.updateTaskProcessed(task, mAckRetry); } } catch (Exception e) { // if retried sending maxRetry times, make it fatal, and // no longer retry. task.setLastError(e.getClass().getSimpleName() + ":" + e.getMessage()); jmsTaskDao.updateErrorTask(task, mRetry); } } })); } }
From source file:com.offbynull.portmapper.upnpigd.UpnpIgdDiscovery.java
private static Map<UpnpIgdDevice, byte[]> getRootXmlForEachDevice(Set<UpnpIgdDevice> devices) throws InterruptedException { Map<UpnpIgdDevice, byte[]> serviceRoots = new HashMap(); ExecutorService executorService = null; try {/*from w ww .ja va 2 s . co m*/ int maximumPoolSize = (int) ((double) Runtime.getRuntime().availableProcessors() / (1.0 - 0.95)); executorService = new ThreadPoolExecutor(0, maximumPoolSize, 1, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); List<HttpRequestCallable<UpnpIgdDevice>> tasks = new LinkedList<>(); for (UpnpIgdDevice device : devices) { tasks.add(new HttpRequestCallable<>(device.getUrl(), device)); } List<Future<Pair<UpnpIgdDevice, byte[]>>> results = executorService.invokeAll(tasks); for (Future<Pair<UpnpIgdDevice, byte[]>> result : results) { try { Pair<UpnpIgdDevice, byte[]> data = result.get(); serviceRoots.put(data.getKey(), data.getValue()); } catch (InterruptedException | ExecutionException | CancellationException e) { // NOPMD // do nothing, skip } } } finally { if (executorService != null) { executorService.shutdownNow(); } } return serviceRoots; }