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:edu.iu.daal_kmeans.regroupallgather.KMUtil.java

/**
 * Generate data and upload to the data dir.
 * /*from  w ww  . j  a  va2s .c o m*/
 * @param numOfDataPoints
 * @param vectorSize
 * @param numPointFiles
 * @param localInputDir
 * @param fs
 * @param dataDir
 * @throws IOException
 * @throws InterruptedException
 * @throws ExecutionException
 */
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir,
        FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException {
    int pointsPerFile = numOfDataPoints / numPointFiles;
    System.out.println("Writing " + pointsPerFile + " vectors to a file");
    // Check data directory
    if (fs.exists(dataDir)) {
        fs.delete(dataDir, true);
    }
    // Check local directory
    File localDir = new File(localInputDir);
    // If existed, regenerate data
    if (localDir.exists() && localDir.isDirectory()) {
        for (File file : localDir.listFiles()) {
            file.delete();
        }
        localDir.delete();
    }
    boolean success = localDir.mkdir();
    if (success) {
        System.out.println("Directory: " + localInputDir + " created");
    }
    if (pointsPerFile == 0) {
        throw new IOException("No point to write.");
    }
    // Create random data points
    int poolSize = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(poolSize);
    List<Future<?>> futures = new LinkedList<Future<?>>();
    for (int k = 0; k < numPointFiles; k++) {

        Future<?> f = service
                .submit(new DataGenMMDense(pointsPerFile, localInputDir, Integer.toString(k), vectorSize));

        futures.add(f); // add a new thread
    }
    for (Future<?> f : futures) {
        f.get();
    }
    // Shut down the executor service so that this
    // thread can exit
    service.shutdownNow();
    // Wrap to path object
    Path localInput = new Path(localInputDir);
    fs.copyFromLocalFile(localInput, dataDir);
    DeleteFileFolder(localInputDir);
}

From source file:com.netflix.config.ConcurrentMapConfigurationTest.java

@Test
public void testConcurrency() {
    final ConcurrentMapConfiguration conf = new ConcurrentMapConfiguration();
    ExecutorService exectuor = Executors.newFixedThreadPool(20);
    final CountDownLatch doneSignal = new CountDownLatch(1000);
    for (int i = 0; i < 1000; i++) {
        final Integer index = i;
        exectuor.submit(new Runnable() {
            public void run() {
                conf.addProperty("key", index);
                conf.addProperty("key", "stringValue");
                doneSignal.countDown();/*from   w  w  w  .  ja  v a2s.c om*/
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }
            }
        });
    }
    try {
        doneSignal.await();
    } catch (InterruptedException e) {

    }
    List prop = (List) conf.getProperty("key");
    assertEquals(2000, prop.size());
}

From source file:com.netflix.spinnaker.front50.config.AzureStorageConfig.java

@Bean
public ApplicationPermissionDAO applicationPermissionDAO(AzureStorageService storageService,
        Registry registry) {//from w w  w  .ja  v  a 2 s  . co m
    return new DefaultApplicationPermissionDAO(storageService,
            Schedulers.from(Executors.newFixedThreadPool(20)), 45000, registry);
}

From source file:com.mobius.software.mqtt.performance.controller.Controller.java

private void initTaskExecutor() {
    scheduler = new PeriodicQueuedTasks<TimedTask>(Config.getInstance().getTimersInterval(), mainQueue);
    workersExecutor = Executors.newFixedThreadPool(Config.getInstance().getWorkers());
    for (int i = 0; i < Config.getInstance().getWorkers(); i++) {
        Worker worker = new Worker(scheduler, mainQueue);
        workers.add(worker);//w  w  w  . jav a  2  s.  co m
        workersExecutor.submit(worker);
    }
    timersExecutor = Executors.newScheduledThreadPool(2);
    timersExecutor.scheduleAtFixedRate(new PeriodicTasksRunner(scheduler), 0,
            Config.getInstance().getTimersInterval(), TimeUnit.MILLISECONDS);

}

From source file:com.flipkart.foxtrot.core.querystore.QueryExecutorTest.java

@Before
public void setUp() throws Exception {
    ElasticsearchUtils.setMapper(mapper);
    DataStore dataStore = TestUtils.getDataStore();

    //Initializing Cache Factory
    hazelcastInstance = new TestHazelcastInstanceFactory(1).newHazelcastInstance();
    HazelcastConnection hazelcastConnection = Mockito.mock(HazelcastConnection.class);
    when(hazelcastConnection.getHazelcast()).thenReturn(hazelcastInstance);
    CacheUtils.setCacheFactory(new DistributedCacheFactory(hazelcastConnection, mapper));

    elasticsearchServer = new MockElasticsearchServer(UUID.randomUUID().toString());
    ElasticsearchConnection elasticsearchConnection = Mockito.mock(ElasticsearchConnection.class);
    when(elasticsearchConnection.getClient()).thenReturn(elasticsearchServer.getClient());
    ElasticsearchUtils.initializeMappings(elasticsearchServer.getClient());
    TableMetadataManager tableMetadataManager = mock(TableMetadataManager.class);
    when(tableMetadataManager.exists(anyString())).thenReturn(true);
    when(tableMetadataManager.get(anyString())).thenReturn(TestUtils.TEST_TABLE);
    QueryStore queryStore = mock(QueryStore.class);
    analyticsLoader = spy(//w w  w  .  j a  va 2s .  c o  m
            new AnalyticsLoader(tableMetadataManager, dataStore, queryStore, elasticsearchConnection));
    TestUtils.registerActions(analyticsLoader, mapper);
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    queryExecutor = new QueryExecutor(analyticsLoader, executorService);
}

From source file:org.fast.maven.plugin.AssemblyMojo.java

/**
 * @throws MojoExecutionException/*from w  w w  . ja v a  2 s.c om*/
 *             thrown if modules are not found
 */
public void execute() throws MojoExecutionException {
    Properties prop = new Properties();
    prop.put("remoteUrlBase", outputDirectory.getAbsolutePath() + "/modules");
    prop.put("uriEncoding", charset);
    StaticDriver driver = new StaticDriver("modules", prop);
    try {
        checkStructure();
        init(driver);
        this.executor = Executors.newFixedThreadPool(this.threads);
        assemblePages(driver);
        copyStaticResources();

        this.executor.shutdown();
        this.executor.awaitTermination(10, TimeUnit.MINUTES);
        for (Future task : tasks) {
            task.get();
        }

    } catch (HttpErrorPage e) {
        throw new MojoExecutionException("Error", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Error", e);
    } catch (InterruptedException e) {
        throw new MojoExecutionException("Error", e);
    } catch (ExecutionException e) {
        throw new MojoExecutionException("Error", e);
    }
}

From source file:com.alibaba.doris.admin.service.failover.node.check.NodeCheckService.java

/**
 * ?ExecutorService???poolSizeExecutorService
 *//*from  ww w . j  a v  a 2s  . c  o m*/
private void createExecutorService() {
    destoryExecutorService(1000);
    executorService = Executors.newFixedThreadPool(poolSize);
}

From source file:com.legstar.host.server.EngineHolder.java

/**
 * This method initializes the work manager used by the engine. We will
 * first attempt to lookup the work manager from the JNDI location
 * specified in the engine config file. If not specified, or unable to load,
 * we will use the default work manager.
 *//* ww  w.  j  a  v a  2 s . c o m*/
private static void initializeWorkManager() {
    LOG.debug("Initializing Work Manager.");
    String workMgrLocation = sConfig.getWorkManagerJNDILocation();
    if (workMgrLocation != null && workMgrLocation.length() > 0) {
        try {
            InitialContext ic = new InitialContext();
            sWorkManager = (WorkManager) ic.lookup(workMgrLocation);
        } catch (Exception e) {
            sWorkManager = null;
        }
    } else {
        sWorkManager = null;
    }

    if (sWorkManager == null) {
        int threadPoolSize = sConfig.getThreadPoolSize();
        sExecutor = Executors.newFixedThreadPool(threadPoolSize);
        sWorkManager = new WorkManagerImpl(sExecutor);
    }
}

From source file:de.uzk.hki.da.pkg.MetsConsistencyChecker.java

/**
 * Checks the package consistency based on actual files
 * present in package the on the file system.
 * //w  w w . j a v a 2s.co m
 * For every file in the packagePath, a checksum will be computed.
 * Also a corresponding mets:FLocat element will be extracted and
 * the expected checksum will be compared to the computed one.
 *
 * @return true if a corresponding FLocat element could be found for every
 * file and the checksums matched, otherwise false
 * @throws Exception the exception
 */
public boolean checkPackageBasedOnFiles() throws Exception {

    boolean result = true;

    Namespace metsNS = Namespace.getNamespace("mets", "http://www.loc.gov/METS/");
    Namespace xlinkNS = Namespace.getNamespace("xlink", "http://www.w3.org/1999/xlink");
    String metsPath = packagePath + "/export_mets.xml";

    SAXBuilder builder = new SAXBuilder(false);
    Document doc = builder.build(new File(metsPath));

    ExecutorService executor = Executors.newFixedThreadPool(8);
    List<FileChecksumVerifierThread> threads = new ArrayList<FileChecksumVerifierThread>();

    String absPackagePath = new File(packagePath).getAbsolutePath();

    for (File f : (List<File>) FileUtils.listFiles(new File(packagePath), null, true)) {

        // skip the METS file itself
        if ("export_mets.xml".equals(f.getName())) {
            continue;
        }

        String relPath = f.getAbsolutePath().substring(absPackagePath.length() + 1);

        logger.debug("Verifying file: {}", relPath);

        String xpathExpr = "//mets:file/mets:FLocat[@xlink:href='" + relPath + "']";
        XPath xpath = XPath.newInstance(xpathExpr);
        xpath.addNamespace(metsNS);
        xpath.addNamespace(xlinkNS);
        Element elemFLocat = (Element) xpath.selectSingleNode(doc);

        // check if METS contains FLocat element for file
        if (elemFLocat == null) {
            result = false;
            String msg = "Could not find FLocat element in METS metadata: " + relPath;
            logger.error(msg);
            messages.add(msg);
            continue;
        }

        Element elemFile = elemFLocat.getParentElement();

        String checksum = elemFile.getAttributeValue("CHECKSUM");
        String checksumType = elemFile.getAttributeValue("CHECKSUMTYPE");

        // check if required attributes are set
        if (checksum == null) {
            logger.warn(
                    "METS File Element in {} does not contain attribute CHECKSUM. File consistency can not be verified.",
                    metsPath);
            continue;
        }
        if (checksumType == null) {
            logger.warn(
                    "METS File Element in {} does not contain attribute CHECKSUM TYPE. File consistency can not be verified.",
                    metsPath);
            continue;
        }

        logger.debug("Checking with algorithm: {}", checksumType);

        // calculate and verify checksum
        checksumType = checksumType.replaceAll("-", "");
        try {
            MessageDigest algorithm = MessageDigest.getInstance(checksumType);
            threads.add(new FileChecksumVerifierThread(checksum, f, algorithm));
        } catch (NoSuchAlgorithmException e) {
            logger.warn(
                    "METS File Element in {} contains unknown CHECKSUM TYPE: {}. File consistency can not be verified.",
                    metsPath, checksumType);
            continue;
        }

    }

    List<Future<ChecksumResult>> futures = executor.invokeAll(threads);

    for (Future<ChecksumResult> future : futures) {
        ChecksumResult cResult = future.get();
        if (!cResult.isSuccess()) {
            result = false;
            logger.error(cResult.getMessage());
            messages.add(cResult.getMessage());
        }
    }

    return result;

}

From source file:com.zhaimi.message.kafka.KafkaReceiver.java

private void processStreamsByTopic(String topicKeys, List<KafkaStream<byte[], byte[]>> streamList) {
    // init stream thread pool
    ExecutorService streamPool = Executors.newFixedThreadPool(partitions);
    String[] topics = StringUtils.split(topicKeys, ",");
    if (log.isDebugEnabled())
        log.debug("???? KafkaStreamList,topic count={},topics={}, partitions/topic={}",
                topics.length, topicKeys, partitions);

    //??stream//from  w  w  w  .j  ava 2s . c o m
    AtomicInteger index = new AtomicInteger(0);
    for (KafkaStream<byte[], byte[]> stream : streamList) {
        Thread streamThread = new Thread() {

            @Override
            public void run() {
                int i = index.getAndAdd(1);
                if (log.isDebugEnabled())
                    log.debug("???KafkaStream -- No.={}, partitions={}", i, partitions + ":" + i);

                ConsumerIterator<byte[], byte[]> consumerIterator = stream.iterator();

                processStreamByConsumer(topicKeys, consumerIterator);
            }
        };
        streamPool.execute(streamThread);
    }
}