Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

Introduction

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

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters, the default thread factory and the default rejected execution handler.

Usage

From source file:org.paxle.filter.robots.impl.RobotsTxtManager.java

void init(Map<String, Object> props) {
    // configure caching manager
    this.manager = CacheManager.getInstance();

    /* =================================================================================
     * init a new cache//from  ww w .j a  v a 2 s.c  o m
     * ================================================================================= */
    Integer maxCacheSize = (Integer) props.get(PROP_MAX_CACHE_SIZE);
    if (maxCacheSize == null)
        maxCacheSize = Integer.valueOf(1000);
    this.cache = new Cache(CACHE_NAME, maxCacheSize.intValue(), false, false, 60 * 60, 30 * 60);
    this.manager.addCache(this.cache);

    /* =================================================================================
     * init threadpool
     * ================================================================================= */
    Integer maxIdle = (Integer) props.get(PROP_WORKER_MAX_IDLE);
    if (maxIdle == null)
        maxIdle = Integer.valueOf(20);

    Integer maxAlive = (Integer) props.get(PROP_WORKER_MAX_ALIVE);
    if (maxAlive == null)
        maxAlive = Integer.valueOf(20);
    if (maxAlive.compareTo(maxIdle) < 0)
        maxAlive = maxIdle;

    this.execService = new ThreadPoolExecutor(maxIdle.intValue(), maxAlive.intValue(), 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

    /* =================================================================================
     * init http-client
     * ================================================================================= */
    this.connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = this.connectionManager.getParams();

    final Integer connectionTimeout = (Integer) props.get(PROP_CONNECTION_TIMEOUT);
    if (connectionTimeout != null)
        params.setConnectionTimeout(connectionTimeout.intValue());
    final Integer socketTimeout = (Integer) props.get(PROP_SOCKET_TIMEOUT);
    if (socketTimeout != null)
        params.setSoTimeout(socketTimeout.intValue());
    final Integer maxConnections = (Integer) props.get(PROP_MAXCONNECTIONS_TOTAL);
    if (maxConnections != null)
        params.setMaxTotalConnections(maxConnections.intValue());

    this.httpClient = new HttpClient(this.connectionManager);

    /* =================================================================================
     * proxy configuration
     * ================================================================================= */
    final Boolean useProxyVal = (Boolean) props.get(PROP_PROXY_USE);
    final boolean useProxy = (useProxyVal == null) ? false : useProxyVal.booleanValue();
    final String host = (String) props.get(PROP_PROXY_HOST);
    final Integer portVal = (Integer) props.get(PROP_PROXY_PORT);

    if (useProxy && host != null && host.length() > 0 && portVal != null) {
        final int port = portVal.intValue();
        this.logger.info(String.format("Proxy is enabled: %s:%d", host, Integer.valueOf(port)));
        final ProxyHost proxyHost = new ProxyHost(host, port);
        this.httpClient.getHostConfiguration().setProxyHost(proxyHost);

        final String user = (String) props.get(PROP_PROXY_HOST);
        final String pwd = (String) props.get(PROP_PROXY_PASSWORD);

        if (user != null && user.length() > 0 && pwd != null && pwd.length() > 0)
            this.httpClient.getState().setProxyCredentials(new AuthScope(host, port),
                    new UsernamePasswordCredentials(user, pwd));
    } else {
        this.logger.info("Proxy is disabled");
        this.httpClient.getHostConfiguration().setProxyHost(null);
        this.httpClient.getState().clearCredentials();
    }

    /* =================================================================================
     * the user-agent name that should be used
     * ================================================================================= */
    final String userAgent = (String) props.get(PROP_USER_AGENT);
    if (userAgent != null) {
        final StringBuffer buf = new StringBuffer();
        Pattern pattern = Pattern.compile("\\$\\{[^\\}]*}");
        Matcher matcher = pattern.matcher(userAgent);

        // replacing property placeholders with system-properties
        while (matcher.find()) {
            String placeHolder = matcher.group();
            String propName = placeHolder.substring(2, placeHolder.length() - 1);
            String propValue = System.getProperty(propName);
            if (propValue != null)
                matcher.appendReplacement(buf, propValue);
        }
        matcher.appendTail(buf);

        this.userAgent = buf.toString();
    } else {
        // Fallback
        this.userAgent = "PaxleFramework";
    }

    this.logger
            .info(String.format("Robots.txt manager initialized. Using '%s' rule-store with %d stored entries.",
                    loader.getClass().getSimpleName(), Integer.valueOf(loader.size())));
}

From source file:eu.edisonproject.training.wsd.Wikidata.java

private Map<CharSequence, List<CharSequence>> getbroaderIDS(Set<Term> terms)
        throws MalformedURLException, InterruptedException, ExecutionException {
    Map<CharSequence, List<CharSequence>> map = new HashMap<>();
    if (terms.size() > 0) {
        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());

        Set<Future<Map<CharSequence, List<CharSequence>>>> set1 = new HashSet<>();
        String prop = "P31";
        for (Term t : terms) {
            URL url = new URL(
                    PAGE + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + t.getUid());
            Logger.getLogger(Wikidata.class.getName()).log(Level.FINE, url.toString());
            WikiRequestor req = new WikiRequestor(url, t.getUid().toString(), 1);
            Future<Map<CharSequence, List<CharSequence>>> future = pool.submit(req);
            set1.add(future);//from   w w w .ja  v  a 2s  .c o  m
        }
        pool.shutdown();

        for (Future<Map<CharSequence, List<CharSequence>>> future : set1) {
            while (!future.isDone()) {
                //                Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet....");
                Thread.currentThread().sleep(10);
            }
            Map<CharSequence, List<CharSequence>> c = future.get();
            if (c != null) {
                map.putAll(c);
            }
        }
    }

    return map;
}

From source file:org.jasig.cas.util.http.SimpleHttpClientFactoryBean.java

/**
 * Build a {@link FutureRequestExecutionService} from the current properties and a HTTP client.
 *
 * @param httpClient the provided HTTP client
 * @return the built request executor service
 *///  w  w w .  j  a va 2 s.  com
private FutureRequestExecutionService buildRequestExecutorService(final CloseableHttpClient httpClient) {

    final ExecutorService definedExecutorService;
    // no executor service provided -> create a default one
    if (this.executorService == null) {
        definedExecutorService = new ThreadPoolExecutor(this.threadsNumber, this.threadsNumber, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(this.queueSize));
    } else {
        definedExecutorService = this.executorService;
    }

    return new FutureRequestExecutionService(httpClient, definedExecutorService);
}

From source file:com.safi.asterisk.handler.SafletEngine.java

private void initThreadPool() {
    //     loadPoolProperties();
    if (log.isDebugEnabled())
        log.debug("Starting threadpool executor with poolsize " + fastAgiMaxPoolsize + " and queuesize "
                + fastAgiPoolsize);//from  w  w w  .j a  v  a2s .c  o m
    threadPool = new ThreadPoolExecutor(fastAgiMaxPoolsize, fastAgiMaxPoolsize, 0L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(fastAgiMaxPoolsize + fastAgiPoolsize));
}

From source file:org.apache.activemq.usecases.RequestReplyToTopicViaThreeNetworkHopsTest.java

@Test
public void runWithTempTopicReplyTo() throws Exception {
    EchoService echo_svc;/*from w  ww .  java  2  s. c  o m*/
    TopicTrafficGenerator traffic_gen;
    Thread start1;
    Thread start2;
    Thread start3;
    Thread start4;
    ThreadPoolExecutor clientExecPool;
    final CountDownLatch clientCompletionLatch;
    int iter;

    fatalTestError = false;
    testError = false;

    //
    // Execute up to 20 clients at a time to simulate that load.
    //

    clientExecPool = new ThreadPoolExecutor(CONCURRENT_CLIENT_COUNT, CONCURRENT_CLIENT_COUNT, 0,
            TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000));
    clientCompletionLatch = new CountDownLatch(TOTAL_CLIENT_ITER);

    // Use threads to avoid startup deadlock since the first broker started waits until
    // it knows the name of the remote broker before finishing its startup, which means
    // the remote must already be running.

    start1 = new Thread() {
        @Override
        public void run() {
            try {
                edge1.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start2 = new Thread() {
        @Override
        public void run() {
            try {
                edge2.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start3 = new Thread() {
        @Override
        public void run() {
            try {
                core1.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start4 = new Thread() {
        @Override
        public void run() {
            try {
                core2.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start1.start();
    start2.start();
    start3.start();
    start4.start();

    start1.join();
    start2.join();
    start3.join();
    start4.join();

    traffic_gen = new TopicTrafficGenerator(edge1.getConnectionUrl(), edge2.getConnectionUrl());
    traffic_gen.start();

    //
    // Now start the echo service with that queue.
    //
    echo_svc = new EchoService("echo", edge1.getConnectionUrl());
    echo_svc.start();

    //
    // Run the tests on Temp Topics.
    //

    LOG.info("** STARTING TEMP TOPIC TESTS");
    iter = 0;
    while ((iter < TOTAL_CLIENT_ITER) && (!fatalTestError)) {
        clientExecPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    RequestReplyToTopicViaThreeNetworkHopsTest.this.testTempTopic(edge1.getConnectionUrl(),
                            edge2.getConnectionUrl());
                } catch (Exception exc) {
                    LOG.error("test exception", exc);
                    fatalTestError = true;
                    testError = true;
                }

                clientCompletionLatch.countDown();
            }
        });

        iter++;
    }

    boolean allDoneOnTime = clientCompletionLatch.await(20, TimeUnit.MINUTES);

    LOG.info("** FINISHED TEMP TOPIC TESTS AFTER " + iter + " ITERATIONS, testError:" + testError + ", fatal: "
            + fatalTestError + ", onTime:" + allDoneOnTime);

    Thread.sleep(100);

    echo_svc.shutdown();
    traffic_gen.shutdown();

    shutdown();

    assertTrue("test completed in time", allDoneOnTime);
    assertTrue("no errors", !testError);
}

From source file:com.obviousengine.android.focus.ZslFocusCamera.java

/**
 * Instantiates a new camera based on Camera 2 API.
 *
 * @param device The underlying Camera 2 device.
 * @param characteristics The device's characteristics.
 * @param pictureSize the size of the final image to be taken.
 *///w w w  . ja v a 2  s  .c  om
ZslFocusCamera(CameraDevice device, CameraCharacteristics characteristics, Size pictureSize) {
    Timber.v("Creating new ZslFocusCamera");

    this.device = device;
    this.characteristics = characteristics;
    fullSizeAspectRatio = calculateFullSizeAspectRatio(characteristics);

    cameraThread = new HandlerThread("FocusCamera");
    // If this thread stalls, it will delay viewfinder frames.
    cameraThread.setPriority(Thread.MAX_PRIORITY);
    cameraThread.start();
    cameraHandler = new Handler(cameraThread.getLooper());

    cameraListenerThread = new HandlerThread("FocusCamera-Listener");
    cameraListenerThread.start();
    cameraListenerHandler = new Handler(cameraListenerThread.getLooper());

    // TODO: Encoding on multiple cores results in preview jank due to
    // excessive GC.
    int numEncodingCores = Utils.getNumCpuCores();
    imageSaverThreadPool = new ThreadPoolExecutor(numEncodingCores, numEncodingCores, 10, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());

    captureManager = new ImageCaptureManager(MAX_CAPTURE_IMAGES, cameraListenerHandler, imageSaverThreadPool);
    captureManager.setCaptureReadyListener(new ImageCaptureManager.CaptureReadyListener() {
        @Override
        public void onReadyStateChange(boolean capturePossible) {
            readyStateManager.setInput(ReadyStateRequirement.CAPTURE_MANAGER_READY, capturePossible);
        }
    });

    // Listen for changes to auto focus state and dispatch to
    // focusStateListener.
    captureManager.addMetadataChangeListener(CaptureResult.CONTROL_AF_STATE,
            new ImageCaptureManager.MetadataChangeListener() {
                @Override
                public void onImageMetadataChange(Key<?> key, Object oldValue, Object newValue,
                        CaptureResult result) {
                    if (focusStateListener == null) {
                        return;
                    }
                    focusStateListener.onFocusStatusUpdate(
                            AutoFocusHelper.stateFromCamera2State(result.get(CaptureResult.CONTROL_AF_STATE)),
                            result.getFrameNumber());
                }
            });

    // Allocate the image reader to store all images received from the
    // camera.
    if (pictureSize == null) {
        // TODO The default should be selected by the caller, and
        // pictureSize should never be null.
        pictureSize = getDefaultPictureSize();
    }
    captureImageReader = ImageReader.newInstance(pictureSize.getWidth(), pictureSize.getHeight(),
            CAPTURE_IMAGE_FORMAT, MAX_CAPTURE_IMAGES);

    captureImageReader.setOnImageAvailableListener(captureManager, cameraHandler);
    mediaActionSound.load(MediaActionSound.SHUTTER_CLICK);
}

From source file:microsoft.exchange.webservices.data.core.request.HangingServiceRequestBase.java

/**
 * Perform any bookkeeping needed when we connect
 * @throws XMLStreamException the XML stream exception
 *//*from   w w  w. j ava2  s  .c  o  m*/
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:com.offbynull.portmapper.upnpigd.UpnpIgdDiscovery.java

private static Map<UpnpIgdServiceReference, byte[]> getServiceDescriptions(
        Set<UpnpIgdServiceReference> services) throws InterruptedException {
    Map<UpnpIgdServiceReference, byte[]> serviceXmls = new HashMap();

    ExecutorService executorService = null;
    try {//from w  w  w .  ja  v a2s. c  om
        int maximumPoolSize = (int) ((double) Runtime.getRuntime().availableProcessors() / (1.0 - 0.95));
        executorService = new ThreadPoolExecutor(0, maximumPoolSize, 1, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());

        List<HttpRequestCallable<UpnpIgdServiceReference>> tasks = new LinkedList<>();
        for (UpnpIgdServiceReference service : services) {
            tasks.add(new HttpRequestCallable<>(service.getScpdUrl(), service));
        }

        List<Future<Pair<UpnpIgdServiceReference, byte[]>>> results = executorService.invokeAll(tasks);

        for (Future<Pair<UpnpIgdServiceReference, byte[]>> result : results) {
            try {
                Pair<UpnpIgdServiceReference, byte[]> data = result.get();
                serviceXmls.put(data.getKey(), data.getValue());
            } catch (InterruptedException | ExecutionException | CancellationException e) { // NOPMD
                // do nothing, skip
            }
        }
    } finally {
        if (executorService != null) {
            executorService.shutdownNow();
        }
    }

    return serviceXmls;
}

From source file:com.emarsys.dyson.storage.DefaultDysonStorage.java

/**
 * Creates and starts the {@link #storageService 
 * storage's executor service} as well as the 
 * {@link #deliveredMailProcessor} in its own {@link Thread}.
 * /*from   w  w  w . j a va 2s  .  c  o m*/
 */
protected void startStorageServices() {
    if (this.storageService == null) {
        this.storageService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());
    }
    if (this.deliveredMailProcessor == null) {
        this.deliveredMailProcessor = new DeliveredMailProcessor();
    }
    if (!this.deliveredMailProcessor.isRunning()) {
        Thread th = new Thread(this.deliveredMailProcessor, "DeliveredMailProcessor");
        th.start();
    }
}

From source file:nl.uva.sne.disambiguators.Wikidata.java

private Map<String, List<String>> getCategories(Set<Term> terms)
        throws MalformedURLException, InterruptedException, ExecutionException {
    Map<String, List<String>> cats = new HashMap<>();

    if (terms.size() > 0) {
        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());
        Set<Future<Map<String, List<String>>>> set1 = new HashSet<>();
        String prop = "P910";
        for (Term t : terms) {
            URL url = new URL(
                    page + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + t.getUID());
            System.err.println(url);
            WikiRequestor req = new WikiRequestor(url, t.getUID(), 1);
            Future<Map<String, List<String>>> future = pool.submit(req);
            set1.add(future);//from ww w. j a v a2 s. c o  m
        }
        pool.shutdown();

        Map<String, List<String>> map = new HashMap<>();
        for (Future<Map<String, List<String>>> future : set1) {
            while (!future.isDone()) {
                //                Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet....");
                Thread.currentThread().sleep(10);
            }
            Map<String, List<String>> c = future.get();
            if (c != null) {
                map.putAll(c);
            }
        }
        workQueue = new ArrayBlockingQueue(maxT);
        pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue);

        //            pool = new ThreadPoolExecutor(maxT, maxT,
        //                    5000L, TimeUnit.MILLISECONDS,
        //                    new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy());
        Set<Future<Map<String, List<String>>>> set2 = new HashSet<>();
        for (Term t : terms) {
            List<String> catIDs = map.get(t.getUID());
            for (String catID : catIDs) {
                URL url = new URL(
                        page + "?action=wbgetentities&format=json&props=labels&languages=en&ids=" + catID);
                System.err.println(url);
                WikiRequestor req = new WikiRequestor(url, t.getUID(), 2);
                Future<Map<String, List<String>>> future = pool.submit(req);
                set2.add(future);
            }
        }
        pool.shutdown();

        for (Future<Map<String, List<String>>> future : set2) {
            while (!future.isDone()) {
                //                Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet....");
                Thread.currentThread().sleep(10);
            }
            Map<String, List<String>> c = future.get();
            if (c != null) {
                cats.putAll(c);
            }
        }
    }

    return cats;
}