Example usage for java.util.concurrent Executors newCachedThreadPool

List of usage examples for java.util.concurrent Executors newCachedThreadPool

Introduction

In this page you can find the example usage for java.util.concurrent Executors newCachedThreadPool.

Prototype

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) 

Source Link

Document

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available, and uses the provided ThreadFactory to create new threads when needed.

Usage

From source file:com.bigdata.journal.jini.ha.AbstractHAJournalServerTestCase.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    if (log.isInfoEnabled())
        log.info("---- TEST START " + getName() + "----");
    executorService = Executors.newCachedThreadPool(new DaemonThreadFactory(getName()));

    ccm = DefaultClientConnectionManagerFactory.getInstance().newInstance();

    /*//from  w  ww .  j a  va  2 s .c  om
     * Override the log level for the ProcessHelper to ensure that we
     * observe all output from the child process in the console of the
     * process running the unit test. This will allow us to observe any
     * failures in the test startup.
     */
    final Logger tmp = Logger.getLogger(ProcessHelper.class);

    oldProcessHelperLevel = tmp.getLevel();

    // Must be at least INFO to see all output of the child processes.
    tmp.setLevel(Level.INFO);

}

From source file:com.quancheng.saluki.core.grpc.GrpcEngine.java

private NioEventLoopGroup createBossEventLoopGroup() {
    ThreadFactory threadFactory = new NamedThreadFactory("grpc-default-boss-ELG", true);
    return new NioEventLoopGroup(1, Executors.newCachedThreadPool(threadFactory));
}

From source file:com.ctriposs.r2.transport.http.client.HttpClientFactory.java

/**
 * Construct a new instance with a specified callback executor.
 *
 * @param callbackExecutor an optional executor to invoke user callbacks that otherwise
 *          will be invoked by scheduler executor.
 * @param shutdownCallbackExecutor if true, the callback executor will be shut down when
 *          this factory is shut down/*from   ww  w  .j a  va  2 s  .c  om*/
 */
public HttpClientFactory(ExecutorService callbackExecutor, boolean shutdownCallbackExecutor) {
    this(FilterChains.empty(),
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(new NamedThreadFactory("R2 Netty IO Boss")),
                    Executors.newCachedThreadPool(new NamedThreadFactory("R2 Netty IO Worker"))),
            true, Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler")),
            true, callbackExecutor, shutdownCallbackExecutor);
}

From source file:gobblin.ingestion.google.webmaster.GoogleWebmasterDataFetcherImpl.java

private int getPagesSize(final String startDate, final String endDate, final String country,
        final List<Dimension> requestedDimensions, final List<ApiDimensionFilter> apiDimensionFilters)
        throws IOException {
    final ExecutorService es = Executors.newCachedThreadPool(ExecutorsUtils
            .newDaemonThreadFactory(Optional.of(log), Optional.of(this.getClass().getSimpleName())));

    int startRow = 0;
    long groupSize = Math.max(1, Math.round(API_REQUESTS_PER_SECOND));
    List<Future<Integer>> results = new ArrayList<>((int) groupSize);

    while (true) {
        for (int i = 0; i < groupSize; ++i) {
            startRow += GoogleWebmasterClient.API_ROW_LIMIT;
            final int start = startRow;
            final String interruptedMsg = String.format(
                    "Interrupted while trying to get the size of all pages for %s. Current start row is %d.",
                    country, start);/*  w w  w  .  j  a v a2s .  c  om*/

            Future<Integer> submit = es.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    log.info(String.format("Getting page size from %s...", start));
                    while (true) {
                        try {
                            LIMITER.acquirePermits(1);
                        } catch (InterruptedException e) {
                            log.error("RateBasedLimiter: " + interruptedMsg, e);
                            return -1;
                        }

                        if (Thread.interrupted()) {
                            log.error(interruptedMsg);
                            return -1;
                        }

                        try {
                            List<String> pages = _client.getPages(_siteProperty, startDate, endDate, country,
                                    GoogleWebmasterClient.API_ROW_LIMIT, requestedDimensions,
                                    apiDimensionFilters, start);
                            if (pages.size() < GoogleWebmasterClient.API_ROW_LIMIT) {
                                return pages.size() + start; //Figured out the size
                            } else {
                                return -1;
                            }
                        } catch (IOException e) {
                            log.info(String.format("Getting page size from %s failed. Retrying...", start));
                        }
                    }
                }
            });
            results.add(submit);
        }
        //Check the results group in order. The first non-negative count indicates the size of total pages.
        for (Future<Integer> result : results) {
            try {
                Integer integer = result.get(GET_PAGE_SIZE_TIME_OUT, TimeUnit.MINUTES);
                if (integer >= 0) {
                    es.shutdownNow();
                    return integer;
                }
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            } catch (TimeoutException e) {
                throw new RuntimeException(String.format(
                        "Exceeding the timeout of %d minutes while getting the total size of all pages.",
                        GET_PAGE_SIZE_TIME_OUT), e);
            }
        }
        results.clear();
    }
}

From source file:com.quancheng.saluki.core.grpc.GrpcEngine.java

private NioEventLoopGroup createWorkEventLoopGroup() {
    ThreadFactory threadFactory = new NamedThreadFactory("grpc-default-worker-ELG", true);
    return new NioEventLoopGroup(0, Executors.newCachedThreadPool(threadFactory));
}

From source file:com.lurencun.cfuture09.androidkit.http.async.AsyncHttp.java

public AsyncHttp() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, socketTimeout);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams, Version.ANDROIDKIT_NAME);

    // ??/*from   w  w  w .  jav  a 2 s.  com*/
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override
        public void process(HttpRequest request, HttpContext context) {
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }

            for (Entry<String, String> entry : clientHeaderMap.entrySet()) {
                request.addHeader(entry.getKey(), entry.getValue());
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            if (entity == null) {
                return;
            }
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));

    threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(new AKThreadFactory());

    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
    clientHeaderMap = new HashMap<String, String>();
}

From source file:com.baidu.asynchttpclient.AsyncHttpClient.java

/**
 * Creates a new AsyncHttpClient.//  w w w.  j  a v a  2  s  .c  o  m
 */
public AsyncHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, socketTimeout);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams,
            String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION));
    HttpProtocolParams.setUseExpectContinue(httpParams, false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(HttpRequest request, HttpContext context) {
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
            for (String header : clientHeaderMap.keySet()) {
                request.addHeader(header, clientHeaderMap.get(header));
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));

    threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {

            Thread t = new Thread(r, "AsyncHttpClient #" + mCount.getAndIncrement());
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != (Thread.NORM_PRIORITY - 1))
                t.setPriority((Thread.NORM_PRIORITY - 1));
            return t;
        }
    });

    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
    clientHeaderMap = new HashMap<String, String>();
}

From source file:com.ctriposs.r2.transport.http.client.HttpClientFactory.java

/**
 * Construct a new instance using the specified filter chain.
 *
 * @param filters the {@link FilterChain} shared by all Clients created by this factory.
 *//*from  w  w  w.j av  a 2 s.com*/
public HttpClientFactory(FilterChain filters) {
    // TODO Disable Netty's thread renaming so that the names below are the ones that actually
    // show up in log messages; need to coordinate with Espresso team (who also have netty threads)
    this(filters,
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(new NamedThreadFactory("R2 Netty IO Boss")),
                    Executors.newCachedThreadPool(new NamedThreadFactory("R2 Netty IO Worker"))),
            true, Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler")),
            true);
}

From source file:com.opengamma.examples.livedata.ExampleLiveDataServer.java

@Override
protected void doConnect() {
    addSubscriptionListener(new SubscriptionListener() {

        @Override/*from  w w w .j  a v a2 s.  co  m*/
        public void unsubscribed(Subscription subscription) {
            Set<String> activeSubscriptionIds = getActiveSubscriptionIds();
            if (activeSubscriptionIds.isEmpty()) {
                _marketDataSimulatorJob.terminate();
                if (_executorService != null) {
                    _executorService.shutdown();
                }
            }
        }

        @Override
        public void subscribed(Subscription subscription) {
            if (!_marketDataSimulatorJob.isStarted()) {
                _executorService = Executors
                        .newCachedThreadPool(new NamedThreadPoolFactory("ExampleLiveDataServer"));
                _executorService.submit(_marketDataSimulatorJob);
            }
        }
    });

}

From source file:com.ebay.jetstream.event.processor.esper.ESPTest.java

public void testProcessor() {
    EsperProcessor processor = getProcessor("ESPTestProcessor");
    ESPTestSink sink = new ESPTestSink();
    List<EventSink> sinks = new ArrayList<EventSink>();
    sinks.add(sink);/* w w  w .  j a va2s .co  m*/
    processor.setEventSinks(sinks);
    // TODO: start not exposed - processor.start(); // it was stopped while running previous test

    ExecutorService threadPool = Executors.newCachedThreadPool(new ESPTestThreadFactory());
    Runnable runnables[] = new ESPTestRunnable[THREADS_NUM];
    try {
        for (int i = 0; i < THREADS_NUM; i++) {
            runnables[i] = new ESPTestRunnable(processor, i);
            threadPool.submit(runnables[i]);
        }
        threadPool.shutdown();
        threadPool.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("InterruptedException: " + e.getMessage());
    }
    assertTrue("ExecutorService failed to shut down properly", threadPool.isShutdown());

    // processor.stop();
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    assertEquals(THREADS_NUM, sink.getCount());
    testLogger.info("sink first, last = [" + sink.getIds().first() + "," + sink.getIds().last() + "]");
}