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:com.voa.weixin.task.TaskManager.java

private TaskManager() {
    pool = Executors.newFixedThreadPool(30);
    pauseTasks = new Stack<Task>();
    isGetToken = false;
}

From source file:org.keycloak.testsuite.saml.ConcurrentAuthnRequestTest.java

private static void loginRepeatedly(UserRepresentation user, URI samlEndpoint, Document samlRequest,
        String relayState, Binding requestBinding) {
    CloseableHttpResponse response = null;
    SamlClient.RedirectStrategyWithSwitchableFollowRedirect strategy = new SamlClient.RedirectStrategyWithSwitchableFollowRedirect();
    ExecutorService threadPool = Executors.newFixedThreadPool(CONCURRENT_THREADS);

    try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(strategy).build()) {
        HttpUriRequest post = requestBinding.createSamlUnsignedRequest(samlEndpoint, relayState, samlRequest);

        Collection<Callable<Void>> futures = new LinkedList<>();
        for (int i = 0; i < ITERATIONS; i++) {
            final int j = i;
            Callable<Void> f = () -> {
                performLogin(post, samlEndpoint, relayState, samlRequest, response, client, user, strategy);
                return null;
            };//from  www.  java2 s  .c o  m
            futures.add(f);
        }

        threadPool.invokeAll(futures);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.cloudera.impala.catalog.TestLoadHdfsMetadataPerf.java

@BeforeClass
public static void beforeClass() {
    executorService_ = Executors.newFixedThreadPool(THREAD_NUM_PER_REQUEST);
}

From source file:com.enigmastation.ml.bayes.CorpusTest.java

@Test(groups = { "fulltest" })
public void testCorpus() throws URISyntaxException, IOException, InterruptedException {
    final Classifier classifier = new FisherClassifierImpl();
    ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    // first we expand the test dataset
    URL resource = this.getClass().getResource("/src/test/resources/publiccorpus");
    File resourceFile = new File(resource.toURI());
    String[] dataFileNames = resourceFile.list(new FilenameFilter() {
        @Override//from ww w  . java  2 s  . co m
        public boolean accept(File dir, String name) {
            return name.endsWith(".bz2");
        }
    });

    List<String> directories = new ArrayList<String>();
    final List<File> trainingFiles = new ArrayList<>();

    for (String fileName : dataFileNames) {
        directories.add(expandFile(fileName));
    }
    // collect every name, plus mark to delete on exit
    for (String inputDirectory : directories) {
        URL url = this.getClass().getResource(inputDirectory);
        File[] dataFiles = new File(url.toURI()).listFiles();
        for (File f : dataFiles) {
            handleFiles(f, trainingFiles);
        }
    }
    long startTime = System.currentTimeMillis();
    final int[] counter = { 0 };
    final int[] marker = { 0 };
    // now let's walk through a training cycle
    for (final File file : trainingFiles) {
        service.submit(new Runnable() {
            @Override
            public void run() {
                if ((++marker[0]) % 100 == 0) {
                    System.out.println("Progress training: " + marker[0] + " of " + trainingFiles.size());
                }
                if (counter[0] > 2) {
                    try {
                        trainWith(classifier, file);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                counter[0] = (counter[0] + 1) % 10;
            }
        });
    }
    service.shutdown();
    service.awaitTermination(2, TimeUnit.HOURS);
    service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    long endTime = System.currentTimeMillis();
    System.out.printf("Training took %d ms%n", (endTime - startTime));
    startTime = System.currentTimeMillis();
    marker[0] = 0;
    // now test against the training data
    for (final File file : trainingFiles) {
        service.submit(new Runnable() {
            public void run() {
                if ((++marker[0]) % 100 == 0) {
                    System.out.println("Progress evaluating: " + marker[0] + " of " + trainingFiles.size());
                }
                if (counter[0] < 3) {
                    try {
                        classify(classifier, file);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                counter[0] = (counter[0] + 1) % 10;
            }
        });
    }
    service.shutdown();
    service.awaitTermination(2, TimeUnit.HOURS);
    endTime = System.currentTimeMillis();
    System.out.printf("Training accuracy: %d tests, %f%% accuracy%n", tests, (hits * 100.0) / tests);
    System.out.printf("Training took %d ms%n", (endTime - startTime));
}

From source file:org.remus.cmdlinehero.base.Executor.java

public Executor() {
    pool = Executors.newFixedThreadPool(10);
}

From source file:thingynet.Application.java

@Bean
public ExecutorService workerExecutorService() {
    ExecutorService executorService = Executors.newFixedThreadPool(applicationPoolSize);
    System.out.println("POOL SIZE:" + applicationPoolSize);

    for (int i = 0; i < applicationPoolSize; i++) {
        //            executorService.submit(workflowService);
    }//www. j a  va2  s .c o m
    return executorService;
}

From source file:com.netflix.evcache.pool.EVCacheClientPoolTest.java

@Test
public void selectClient_hugeNumOfModOps_noException() throws Exception {
    // Arrange/*from www .j  av  a 2 s .  c  o m*/

    // set up the object under test
    EVCacheNodeList evCacheNodeList = mock(EVCacheNodeList.class);
    EVCacheClientPoolManager evCacheClientPoolManager = mock(EVCacheClientPoolManager.class);

    EVCacheClientPool evCacheClientPool = new EVCacheClientPool("in a unit test", evCacheNodeList,
            (ThreadPoolExecutor) Executors.newFixedThreadPool(1), evCacheClientPoolManager);
    FieldUtils.writeField(evCacheClientPool, "numberOfModOps", new AtomicLong(0xFFFF_FFFF_FFFF_FFFFL), true);

    // Set up the method arguments
    EVCacheClient client1 = mock(EVCacheClient.class);
    EVCacheClient client2 = mock(EVCacheClient.class);
    List<EVCacheClient> clientsList = new ArrayList<>();
    clientsList.add(client1);
    clientsList.add(client2);

    // Ensure it's accessible
    // Yes it's private but this is a real bug we fixed.
    Method method = evCacheClientPool.getClass().getDeclaredMethod("selectClient", List.class);
    method.setAccessible(true);

    // Act
    Object ret = method.invoke(evCacheClientPool, clientsList);

    // Assert
    // The number set in numOfModOps should roll over to 0x1_0000_0000_0000_0000
    // so we should get client1 back
    EVCacheClient selected = (EVCacheClient) ret;
    assertSame(selected, client1);
}

From source file:com.splicemachine.test.MiniZooKeeperCluster.java

public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0)
        return -1;
    shutdown();/*www . j a  v  a2  s . c  om*/
    service = Executors.newFixedThreadPool(numZooKeeperServers);
    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
        int l = i + 1;
        File dir = new File(baseDir, "zookeeper_" + l).getAbsoluteFile();
        prepareDir(dir, i + 1);
        Properties startupProperties = new Properties();
        startupProperties.setProperty("tickTime", "2000");
        startupProperties.setProperty("dataDir", dir.getAbsolutePath());
        startupProperties.setProperty("initLimit", "10");
        startupProperties.setProperty("syncLimit", "5");
        startupProperties.setProperty("maxClientCnxns", "100");

        for (int j = 0; j < numZooKeeperServers; j++) {
            int m = j + 1;
            startupProperties.setProperty("server." + m, "localhost:" + (2888 + j) + ":" + (3888 + j));
        }
        startupProperties.setProperty("clientPort", String.valueOf(2181 + i));
        LOG.trace("startup Properties: " + startupProperties);
        QuorumPeerConfig config = new QuorumPeerConfig();
        try {
            config.parseProperties(startupProperties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        service.execute(new SpliceZoo(config, l));
    }
    started = true;
    return 0;
}

From source file:com.sitewhere.test.MultithreadedRestTest.java

@Test
public void doRestTest() throws Exception {
    java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
    java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "ERROR");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "ERROR");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "ERROR");

    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    CompletionService<SiteWhereClientTester.TestResults> completionService = new ExecutorCompletionService<SiteWhereClientTester.TestResults>(
            executor);/* ww  w  .  ja va2 s. c o  m*/

    for (int i = 0; i < numThreads; i++) {
        completionService
                .submit(new SiteWhereClientTester("90389b40-7c25-401b-bf72-98673913d59e", 100, updateState));
    }
    for (int i = 0; i < numThreads; ++i) {
        completionService.take().get();
    }
}

From source file:gov.nih.nci.indexgen.IndexGenerator.java

public IndexGenerator(String ormFileName, int threadCount, String[] classFilterArray, boolean include) {

    this.include = include;
    this.pool = Executors.newFixedThreadPool(threadCount);
    this.sessionFactory = new AnnotationConfiguration().configure(ormFileName).buildSessionFactory();

    if (classFilterArray != null) {
        this.classFilter.addAll(Arrays.asList(classFilterArray));
    }/*from w w  w.  j a  v a  2  s .  c om*/
}