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.wenyu.clustertools.SetStreamThroughput.java

@Override
public void execute() {
    ExecutorService executor = Executors.newFixedThreadPool(parallel);

    Map<Node, Future<String>> futures = new HashMap<>();
    for (ClusterToolCmd.Node node : nodes) {
        futures.put(node, executor.submit(new Executor(node)));
    }/*from ww w . ja  v a  2  s. c  om*/

    for (Map.Entry<ClusterToolCmd.Node, Future<String>> future : futures.entrySet()) {
        try {
            String result = future.getValue().get(Constants.MAX_PARALLEL_WAIT_IN_SEC, TimeUnit.SECONDS);
            System.out.println(result);
        } catch (Exception ex) {
            System.out
                    .println(String.format("%s failed with error: %s", future.getKey().server, ex.toString()));
            ex.printStackTrace();
        }
    }
}

From source file:org.apache.streams.twitter.provider.TwitterStreamProcessor.java

public TwitterStreamProcessor(TwitterStreamProvider provider, int poolSize) {
    //We are only going to use the Hosebird processor to manage the extraction of the tweets from the Stream
    super(null);//from   w  w  w  .j  a  va2  s.  co  m
    service = Executors.newFixedThreadPool(poolSize);
    this.provider = provider;
}

From source file:com.amazonaws.services.kinesis.scaling.auto.AutoscalingController.java

private AutoscalingController(AutoscalingConfiguration[] config) {
    this.config = config;
    this.executor = Executors.newFixedThreadPool(this.config.length);
}

From source file:edu.msu.cme.rdp.kmer.cli.FastKmerFilter.java

public static void main(String[] args) throws Exception {
    final KmerSet<Set<RefKmer>> kmerSet;
    final SeqReader queryReader;
    final SequenceType querySeqType;
    final File queryFile;
    final KmerStartsWriter out;
    final boolean translQuery;
    final int wordSize;
    final int translTable;
    final boolean alignedSeqs;
    final List<String> refLabels = new ArrayList();
    final int maxThreads;
    final int trieWordSize;

    try {/*from ww w.  ja v  a 2  s  . c  om*/
        CommandLine cmdLine = new PosixParser().parse(options, args);
        args = cmdLine.getArgs();

        if (args.length < 3) {
            throw new Exception("Unexpected number of arguments");
        }

        if (cmdLine.hasOption("out")) {
            out = new KmerStartsWriter(cmdLine.getOptionValue("out"));
        } else {
            out = new KmerStartsWriter(System.out);
        }

        if (cmdLine.hasOption("aligned")) {
            alignedSeqs = true;
        } else {
            alignedSeqs = false;
        }

        if (cmdLine.hasOption("transl-table")) {
            translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table"));
        } else {
            translTable = 11;
        }

        if (cmdLine.hasOption("threads")) {
            maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads"));
        } else {
            maxThreads = Runtime.getRuntime().availableProcessors();
        }

        queryFile = new File(args[1]);
        wordSize = Integer.valueOf(args[0]);
        SequenceType refSeqType = null;

        querySeqType = SeqUtils.guessSequenceType(queryFile);
        queryReader = new SequenceReader(queryFile);

        if (querySeqType == SequenceType.Protein) {
            throw new Exception("Expected nucl query sequences");
        }

        refSeqType = SeqUtils
                .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2]));

        translQuery = refSeqType == SequenceType.Protein;

        if (translQuery && wordSize % 3 != 0) {
            throw new Exception("Word size must be a multiple of 3 for nucl ref seqs");
        }

        if (translQuery) {
            trieWordSize = wordSize / 3;
        } else {
            trieWordSize = wordSize;
        }
        kmerSet = new KmerSet<Set<RefKmer>>();//new KmerTrie(trieWordSize, translQuery);

        for (int index = 2; index < args.length; index++) {
            String refName;
            String refFileName = args[index];
            if (refFileName.contains("=")) {
                String[] lexemes = refFileName.split("=");
                refName = lexemes[0];
                refFileName = lexemes[1];
            } else {
                String tmpName = new File(refFileName).getName();
                if (tmpName.contains(".")) {
                    refName = tmpName.substring(0, tmpName.lastIndexOf("."));
                } else {
                    refName = tmpName;
                }
            }

            File refFile = new File(refFileName);

            if (refSeqType != SeqUtils.guessSequenceType(refFile)) {
                throw new Exception(
                        "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile)
                                + " sequences but expected " + refSeqType + " sequences");
            }

            SequenceReader seqReader = new SequenceReader(refFile);
            Sequence seq;

            while ((seq = seqReader.readNextSequence()) != null) {
                if (seq.getSeqName().startsWith("#")) {
                    continue;
                }

                KmerGenerator kmers;
                try {
                    if (translQuery) { //protein ref
                        kmers = new ProtKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs);
                    } else {
                        kmers = new NuclKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs);
                    }
                    while (kmers.hasNext()) {
                        Kmer temp = kmers.next();
                        long[] next = temp.getLongKmers();
                        Set<RefKmer> refKmers = kmerSet.get(next);
                        if (refKmers == null) {
                            refKmers = new HashSet();
                            kmerSet.add(next, refKmers);
                        }

                        RefKmer kmerRef = new RefKmer();
                        kmerRef.modelPos = kmers.getPosition();
                        kmerRef.refFileIndex = refLabels.size();
                        kmerRef.refSeqid = seq.getSeqName();
                        refKmers.add(kmerRef);
                    }
                } catch (IllegalArgumentException ex) {
                    //System.err.println(seq.getSeqName()+ " " + ex.getMessage());
                }
            }
            seqReader.close();

            refLabels.add(refName);
        }

    } catch (Exception e) {
        new HelpFormatter().printHelp(
                "KmerSearch <kmerSize> <query_file> [name=]<ref_file> ...\nkmerSize should be multiple of 3, (recommend 45, minimum 30, maximum 63) ",
                options);
        e.printStackTrace();
        System.exit(1);
        throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables
    }

    long startTime = System.currentTimeMillis();
    long seqCount = 0;
    final int maxTasks = 25000;

    System.err.println("Starting kmer mapping at " + new Date());
    System.err.println("*  Number of threads:       " + maxThreads);
    System.err.println("*  References:              " + refLabels);
    System.err.println("*  Reads file:              " + queryFile);
    System.err.println("*  Kmer length:             " + trieWordSize);
    System.err.println("*  Kmer Refset Size:        " + kmerSet.size());

    final AtomicInteger processed = new AtomicInteger();
    final AtomicInteger outstandingTasks = new AtomicInteger();

    ExecutorService service = Executors.newFixedThreadPool(maxThreads);

    Sequence querySeq;

    while ((querySeq = queryReader.readNextSequence()) != null) {
        seqCount++;

        String seqString = querySeq.getSeqString();

        if ((!translQuery && seqString.length() < wordSize)
                || (translQuery && seqString.length() < wordSize + 2)) {
            //System.err.println(querySeq.getSeqName() + "\t" + seqString.length());
            continue;
        }

        final Sequence threadSeq = querySeq;

        Runnable r = new Runnable() {

            public void run() {
                try {
                    processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, false);
                    processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, true);

                    processed.incrementAndGet();
                    outstandingTasks.decrementAndGet();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        outstandingTasks.incrementAndGet();
        service.submit(r);

        while (outstandingTasks.get() >= maxTasks)
            ;

        if ((processed.get() + 1) % 1000000 == 0) {
            System.err.println("Processed " + processed + " sequences in "
                    + (System.currentTimeMillis() - startTime) + " ms");
        }
    }

    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);

    System.err.println("Finished Processed " + processed + " sequences in "
            + (System.currentTimeMillis() - startTime) + " ms");

    out.close();
}

From source file:interactivespaces.activity.component.ActivityComponentContextTest.java

@Before
public void setup() {
    executor = Executors.newFixedThreadPool(10);

    activity = Mockito.mock(SupportedActivity.class);

    activityConfiguration = Mockito.mock(Configuration.class);
    Mockito.when(activity.getConfiguration()).thenReturn(activityConfiguration);

    log = Mockito.mock(Log.class);
    Mockito.when(activity.getLog()).thenReturn(log);

    componentFactory = Mockito.mock(ActivityComponentFactory.class);

    context = new ActivityComponentContext(activity, componentFactory);
}

From source file:net.openhft.chronicle.logger.VanillChronicleQueuePerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {

    final int RUNS = 15000000;
    final int THREADS = Runtime.getRuntime().availableProcessors();

    for (int size : new int[] { 64, 128, 256 }) {
        {/* www  .j a  v a  2s. co  m*/
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size));
            }

            es.shutdown();
            es.awaitTermination(2, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "ChronicleLog.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }
    }
}

From source file:ufo.remote.calls.benchmark.client.caller.resttemplate.AsyncRestTemplateTester.java

@Override
protected void startTest(final TesterResult result) {

    String url = "http://" + hostname + ":" + port + path;

    CountDownLatch latch = new CountDownLatch(result.totalCalls);
    AtomicInteger failures = new AtomicInteger(0);
    Executor executor = Executors.newFixedThreadPool(10);
    for (int i = 0; i < result.totalCalls; i++) {
        executor.execute(new Caller(url, result.message, latch, failures));
    }/*from   www  .  j a  va 2  s .  co  m*/

    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    result.failures = failures.get();
}

From source file:com.wenyu.clustertools.SetLoggingLevel.java

@Override
public void execute() {
    ExecutorService executor = Executors.newFixedThreadPool(parallel);

    Map<ClusterToolCmd.Node, Future<String>> futures = new HashMap<>();
    for (ClusterToolCmd.Node node : nodes) {
        futures.put(node, executor.submit(new Executor(node)));
    }//from w  w  w.j  av a2 s. c o  m

    for (Map.Entry<ClusterToolCmd.Node, Future<String>> future : futures.entrySet()) {
        try {
            String result = future.getValue().get(Constants.MAX_PARALLEL_WAIT_IN_SEC, TimeUnit.SECONDS);
            System.out.println(result);
        } catch (Exception ex) {
            System.out
                    .println(String.format("%s failed with error: %s", future.getKey().server, ex.toString()));
            ex.printStackTrace();
        }
    }
}

From source file:com.jivesoftware.os.rcvs.api.keys.SymetricalHashableKeyTest.java

License:asdf

@Test
public void testThreadSafe() throws Exception {
    System.out.println("testThreadSafe");
    final SymetricalHashableKey instance = new SymetricalHashableKey("asdfghjk");
    int poolSize = 4;
    ExecutorService threadPool = Executors.newFixedThreadPool(poolSize);
    final CountDownLatch latch = new CountDownLatch(poolSize);
    final MutableBoolean failed = new MutableBoolean(false);
    for (int i = 0; i < poolSize; i++) {
        threadPool.submit(new Runnable() {
            @Override/*w  w  w .j  a va 2s  . c o  m*/
            public void run() {
                try {
                    int count = 1000000;
                    for (long i = 0; i < count; i++) {
                        long a = Math.abs(rand.nextLong());
                        byte[] hash = instance.toHash(longBytes(a));
                        byte[] output = instance.toBytes(hash);
                        long b = bytesLong(output);
                        if (a != b) {
                            System.out.println("Failed a:" + a + " ne b:" + b);
                            failed.setValue(true);
                            return;
                        }
                    }
                } catch (InvalidKeyException | ShortBufferException | IllegalBlockSizeException
                        | BadPaddingException | ExecutionException x) {
                    failed.setValue(true);
                } finally {
                    latch.countDown();
                }
            }
        });
    }

    latch.await();
    Assert.assertFalse(failed.booleanValue());
}

From source file:org.openbaton.common.vnfm_sdk.amqp.VnfmSpringHelper.java

@PostConstruct
private void init() throws IOException {
    properties = new Properties();
    properties.load(this.getClass().getResourceAsStream("/conf.properties"));
    executorService = Executors.newFixedThreadPool(20);
}