Example usage for java.util.concurrent Executors newFixedThreadPool

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

Introduction

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

Prototype

public static ExecutorService newFixedThreadPool(int nThreads) 

Source Link

Document

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.

Usage

From source file:FullReindexer.java

public void reindex() throws IOException {
    ExecutorService executor = Executors.newFixedThreadPool(totalThreads);

    for (int i = 0; i < totalThreads; i++) {
        Worker worker = new Worker(i, totalThreads, readClient, writeClient);
        executor.execute(worker);//from  www  . j  av a2s .  com
    }

    executor.shutdown();

    while (!executor.isTerminated()) {
        // Wait until done
    }

    readClient.close();
    writeClient.close();
}

From source file:DFSBenchmark.java

public DFSBenchmark(int threads, Path basePath) {
    this.threads = threads;
    this.baseDirPath = basePath;
    greenSignal = new CountDownLatch(threads);
    service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threads));
    reporter.enable(metrics, 3000, TimeUnit.MILLISECONDS);
}

From source file:com.cloudera.oryx.ml.serving.als.LoadIT.java

@Test
public void testRecommendLoad() throws Exception {
    // Since latency is more important, and local machine will also be busy handling requests,
    // use few concurrent workers, like 1:
    int workers = 1;
    AtomicLong count = new AtomicLong();
    Mean meanReqTimeMS = new Mean();
    long start = System.currentTimeMillis();

    List<Callable<Void>> tasks = new ArrayList<>(workers);
    for (int i = 0; i < workers; i++) {
        tasks.add(new LoadCallable(Integer.toString(i), meanReqTimeMS, count, start));
    }//w ww .ja  v  a 2 s  .  c  o  m

    ExecutorService executor = Executors.newFixedThreadPool(workers);
    try {
        executor.invokeAll(tasks);
    } finally {
        executor.shutdown();
    }

    int totalRequests = workers * REQS_PER_WORKER;
    log(totalRequests, meanReqTimeMS, start);

    Assert.assertTrue(meanReqTimeMS.getResult() < 300.0);
}

From source file:cc.kave.commons.pointsto.evaluation.DefaultModule.java

@Override
protected void configure() {
    configCrossValidation();/*from  ww w .  jav  a  2s. com*/
    configOptions();

    configMeasure();

    bind(new TypeLiteral<Predicate<Usage>>() {
    }).annotatedWith(UsageFilter.class).to(PointsToUsageFilter.class);
    bind(ResultExporter.class).to(CSVExporter.class);

    // Executor for CVEvaluator
    int numThreads;
    if (System.getProperties().containsKey("evaluation.numthreads")) {
        numThreads = Integer.parseInt(System.getProperty("evaluation.numthreads"));
    } else {
        numThreads = Math.min(6, Runtime.getRuntime().availableProcessors());
    }
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    // ExecutorService executorService = Executors.newSingleThreadExecutor();
    bind(ExecutorService.class).toInstance(executorService);
}

From source file:com.jillesvangurp.httpclientfuture.HttpClientWithFutureTest.java

@BeforeClass
public void beforeClass() {
    webServerExecutor = Executors.newSingleThreadExecutor();
    webServer = new WebServer();
    webServerExecutor.execute(webServer);
    clientThreadPool = Executors.newFixedThreadPool(threads);
    PoolingClientConnectionManager conman = new PoolingClientConnectionManager();
    conman.setDefaultMaxPerRoute(threads);
    conman.setMaxTotal(threads);//from   ww  w .  j  av  a  2s. com
    DefaultHttpClient httpclient = new DefaultHttpClient(conman);
    client = new HttpClientWithFuture<Boolean>(httpclient, clientThreadPool, new ResponseHandler<Boolean>() {
        @Override
        public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            return response.getStatusLine().getStatusCode() == 200;
        }
    });
}

From source file:gr.demokritos.iit.cru.creativity.reasoning.semantic.WebMiner.java

public static String WebMiner(String seed, int difficulty, String language, boolean compactForm)
        throws ClassNotFoundException, SQLException, IOException, InstantiationException,
        IllegalAccessException {/*w  w  w . jav  a  2 s.co  m*/
    Gson gson = new Gson();
    Connect c = new Connect(language);
    RandomWordGenerator r = new RandomWordGenerator(c);
    String randomPhrase = r.selectRandomWord(seed, difficulty).replace(",", " ");
    InfoSummarization inf = new InfoSummarization(c);
    LinkedHashMap<String, Double> TagCloud = new LinkedHashMap<String, Double>();

    Set<String> pages = new HashSet<String>();
    ArrayList<String> urls = new ArrayList<String>();
    ArrayList<String> urls_temp = new ArrayList<String>();
    if (language.equalsIgnoreCase("en")) {
        if (randomPhrase.length() == 0) {
            randomPhrase = seed;
        }
        String bingAppId = c.getBingAppId();
        BingCrawler bc = new BingCrawler(bingAppId, language);
        urls_temp = bc.crawl(randomPhrase);
        int url_loop = 0;
        while ((url_loop < 5) && (url_loop < urls_temp.size())) {
            urls.add(urls_temp.get(url_loop));
            url_loop++;
        }
    } else if (language.equalsIgnoreCase("el")) {
        String bingAppId = c.getBingAppId();
        BingCrawler bc = new BingCrawler(bingAppId, language);
        urls_temp = bc.crawl(randomPhrase);
        int url_loop = 0;
        while ((url_loop < 5) && (url_loop < urls_temp.size())) {
            urls.add(urls_temp.get(url_loop));
            url_loop++;
        }
    } else if (language.equalsIgnoreCase("de")) {//keep only the first word of the random phrase for search
        if (randomPhrase.length() == 0) {
            randomPhrase = seed;
        }
        urls_temp = HTMLUtilities.linkExtractor(
                "http://www.fragfinn.de/kinderliste/suche?start=0&query=" + randomPhrase.split(" ")[0], "UTF-8",
                0);

        for (String url : urls_temp) {
            urls.add(StringEscapeUtils.unescapeHtml4(url));
            if (urls.size() == 5) {
                break;
            }
        }
    }
    String delims = "[{} .,;?!():\"]+";

    String[] words = randomPhrase.split(",");
    String[] user_keywords = seed.split(delims);
    if (urls.size() > 0) {
        ExecutorService threadPool = Executors.newFixedThreadPool(urls.size());
        for (String url : urls) {
            threadPool.submit(new HTMLPages(url, pages, language)); //stopWordSet, tokensHashMap,language));
            // threadPool.submit(HTMLTokenizer());
        }
        threadPool.shutdown();
        while (!threadPool.isTerminated()) {

        }

        LinkedHashMap<ArrayList<String>, Double> temp = inf.TopTermsBing(pages, compactForm);
        HashMap<String, Double> temp2 = new HashMap<String, Double>();
        for (ArrayList<String> stems : temp.keySet()) {
            for (int j = 0; j < stems.size(); j++) {
                String s = stems.get(j).split("\\{")[0];
                s = s.replace(",", " ");
                s = s.trim();

                boolean wordnet = true;
                //if term is not one of the initial random phrase
                for (int i = 0; i < words.length; i++) {
                    if (s.equalsIgnoreCase(words[i])) {
                        wordnet = false;
                    }
                }
                //and if it 's not in the initial words of user
                for (int i = 0; i < user_keywords.length; i++) {
                    if (s.equalsIgnoreCase(user_keywords[i])) {
                        wordnet = false;
                    }
                }
                //in german or greek, ignore english words from search english words
                if (language.equalsIgnoreCase("de") || language.equalsIgnoreCase("el")) {
                    if (c.getWn().getCommonPos(s) != null) {
                        continue;
                    }
                }
                //return it with its stem's weight
                if (wordnet) {
                    //for every stem, put each of its corresponding terms to tagCloud with the stem's tf
                    temp2.put(stems.get(j), temp.get(stems));
                }
            }
        }
        TagCloud = inf.sortHashMapByValues(temp2);
        threadPool.shutdownNow();
    }
    String json = gson.toJson(TagCloud);
    c.CloseConnection();
    return json;
}

From source file:com.otway.picasasync.syncutil.SyncManager.java

public SyncManager(Settings settings) {
    this.settings = settings;

    log.info("Initialising Thread Pool");
    executor = Executors.newFixedThreadPool(1);

    log.info("Initialising SyncState");
    syncState = new SyncState();

    log.info("Initialising OAuth");
    auth = new GoogleOAuth();

    log.info("SyncManager initialised successfully.");
}

From source file:info.pancancer.arch3.coordinator.Coordinator.java

public void doWork() throws InterruptedException, ExecutionException {
    ExecutorService pool = Executors.newFixedThreadPool(DEFAULT_THREADS);
    CoordinatorOrders coordinatorOrders = new CoordinatorOrders(this.configFile,
            this.options.has(this.endlessSpec));
    CleanupJobs cleanupJobs = new CleanupJobs(this.configFile, this.options.has(this.endlessSpec));
    FlagJobs flagJobs = new FlagJobs(this.configFile, this.options.has(this.endlessSpec));
    List<Future<?>> futures = new ArrayList<>();
    futures.add(pool.submit(coordinatorOrders));
    futures.add(pool.submit(cleanupJobs));
    futures.add(pool.submit(flagJobs));/*  w w  w  .j av a2 s.  c o  m*/
    try {
        for (Future<?> future : futures) {
            future.get();
        }
    } catch (InterruptedException | ExecutionException ex) {
        log.error(ex.toString());
        throw new RuntimeException(ex);
    } finally {
        pool.shutdown();
    }
}

From source file:com.solace.demos.cloudfoundry.scaling.aggregator.controller.SolaceController.java

@PostConstruct
public void init() {
    // Connect to Solace
    trace.info("************* Init Called ************");

    try {/*www .j a va2s.  c o  m*/
        session = jcsmpFactory.createSession();
        session.connect();

        destForPublish = JCSMPFactory.onlyInstance().createQueue("Q/demo/requests");

        isSolaceConnected = true;
        System.out.println("************* Solace initialized correctly!! ************");

    } catch (Exception e) {
        trace.error("Error connecting and setting up session.", e);
    }

    executor = Executors.newFixedThreadPool(NUM_PUBLISH_THREADS);

}

From source file:it.infn.ct.futuregateway.apiserver.inframanager.MonitorQueue.java

/**
 * Build a monitor queue./* ww w .  ja  va 2  s .  c  o  m*/
 * A monitor queue is made of two elements: queue and monitor thread. Both
 * are created.
 *
 * @param bufferSize  A queue to store the activities
 * @param threadPoolSize A thread pool for the monitoring
 * @param checkInterval The minimum interval between checks in milliseconds
 */
public MonitorQueue(final int bufferSize, final int threadPoolSize, final int checkInterval) {
    log.debug("Creating the monitor queue");
    monitorQueue = new ArrayBlockingQueue<>(bufferSize);
    log.debug("Creating the monitor thread pool");
    monitorPool = Executors.newFixedThreadPool(threadPoolSize);
    for (int i = 0; i < threadPoolSize; i++) {
        monitorPool.submit(new Monitor(monitorQueue, checkInterval));
    }
}