Example usage for java.util.concurrent.atomic AtomicInteger AtomicInteger

List of usage examples for java.util.concurrent.atomic AtomicInteger AtomicInteger

Introduction

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

Prototype

public AtomicInteger() 

Source Link

Document

Creates a new AtomicInteger with initial value 0 .

Usage

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 {/*  w  w w . j  a v a  2 s . c  o  m*/
        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:io.watchcat.node.metrics.MemoryUsage.java

public MemoryUsage() {
    command = new ShellCommand(
            "/usr/bin/free -tmo | /usr/bin/awk '{print $1\",\"$2\",\"$3-$6-$7\",\"$4+$6+$7}'");

    totalMemory = new AtomicInteger();
    totalSwap = new AtomicInteger();
    usedMemory = new AtomicInteger();
    usedSwap = new AtomicInteger();
}

From source file:org.zenoss.zep.dao.impl.DaoUtilsTest.java

@Test
public void testDeadlockRetryNestedException() throws Exception {
    final AtomicInteger i = new AtomicInteger();
    final int returnVal = new Random().nextInt();
    int result = DaoUtils.deadlockRetry(new Callable<Integer>() {
        @Override//from w ww  .j a v a 2s.  co m
        public Integer call() throws Exception {
            if (i.incrementAndGet() < 5) {
                throw new RuntimeException(new DeadlockLoserDataAccessException("My fake exception", null));
            }
            return returnVal;
        }
    });
    assertEquals(i.get(), 5);
    assertEquals(result, returnVal);
}

From source file:com.weibo.datasys.crawler.impl.strategy.rule.save.StatisticSaveRule.java

@Override
public Null apply(ParseInfo in) {
    String url = in.getThisCrawlInfo().getSeedData().getUrl();
    String host = URLUtil.getHost(url);
    AtomicInteger count = hostCountMap.get(host);
    if (count == null) {
        synchronized (StatisticSaveRule.class) {
            if (count == null) {
                count = new AtomicInteger();
                hostCountMap.put(host, count);
            } else {
                count = hostCountMap.get(host);
            }//from w  ww. j av  a2  s .co m
        }
    }
    count.incrementAndGet();
    saveResult();
    return null;
}

From source file:com.predic8.membrane.core.interceptor.apimanagement.AMRateLimitInterceptorTest.java

@Test
public void testHandleRequestRateLimit5SecondConcurrency() throws Exception {

    final Exchange exc = new Exchange(null);
    exc.setResponse(Response.ResponseBuilder.newInstance().build());
    exc.setProperty(Exchange.API_KEY, "junit");
    exc.setRule(new ServiceProxy());
    exc.getRule().setName("junit API");

    final AMRateLimiter rli = new AMRateLimiter();
    ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"),
            "src\\test\\resources\\apimanagement\\api.yaml");
    rli.setAmc(amc);//from w ww .  jav a2s.c o m

    ArrayList<Thread> threads = new ArrayList<Thread>();
    final AtomicInteger continues = new AtomicInteger();
    final AtomicInteger returns = new AtomicInteger();
    for (int i = 0; i < 1000; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Outcome out = rli.handleRequest(exc);
                    if (out == Outcome.CONTINUE) {
                        continues.incrementAndGet();
                    } else if (out == Outcome.RETURN) {
                        returns.incrementAndGet();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    assertEquals(5, continues.get());
    assertEquals(995, returns.get());
    Thread.sleep(2000);
    assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}

From source file:io.pravega.test.integration.selftest.Producer.java

/**
 * Creates a new instance of the Producer class.
 *
 * @param id         The Id of the producer.
 * @param config     Test Configuration.
 * @param dataSource DataSource for the Producer.
 * @param store      A StoreAdapter to execute operations on.
 * @param executor   An Executor to use for async operations.
 *//*w ww .  jav a2  s  .c o  m*/
Producer(int id, TestConfig config, ProducerDataSource dataSource, StoreAdapter store,
        ScheduledExecutorService executor) {
    super(config, dataSource, store, executor);

    this.id = id;
    this.logId = String.format("Producer[%s]", id);
    this.iterationCount = new AtomicInteger();
    this.canContinue = new AtomicBoolean(true);
}

From source file:dk.netarkivet.common.utils.warc.WARCUtils.java

/**
 * Create new WARCWriter, writing to warcfile newFile.
 * @param newFile the WARCfile, that the WARCWriter writes to.
 * @return new WARCWriter, writing to warcfile newFile.
 *///from   w  w w .  j  a va2  s. c o m
public static WARCWriter createWARCWriter(File newFile) {
    WARCWriter writer;
    PrintStream ps = null;
    try {
        ps = new PrintStream(new FileOutputStream(newFile));
        writer = new WARCWriterNAS(new AtomicInteger(), ps,
                //This name is used for the first (file metadata) record
                newFile, false, //Don't compress
                //Use current time
                ArchiveDateConverter.getWarcDateFormat().format(new Date()), null //No particular file metadata to add
        );
    } catch (IOException e) {
        if (ps != null) {
            ps.close();
        }
        String message = "Could not create WARCWriter to file '" + newFile + "'.\n";
        log.warn(message);
        throw new IOFailure(message, e);
    }
    return writer;
}

From source file:com.astamuse.asta4d.web.util.timeout.DefaultSessionAwareExpirableDataManager.java

public DefaultSessionAwareExpirableDataManager() {
    dataMap = createThreadSafeDataMap();
    dataCounter = new AtomicInteger();
}

From source file:org.apache.servicemix.jbi.cluster.engine.ClusterEndpointLoadTest.java

public void testLoadInOnly() throws Exception {
    createRoute(Transacted.Jms, true, false, true);

    final int nbThreads = 10;
    final int nbExchanges = 10;
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    final CountDownLatch latch = new CountDownLatch(nbThreads);
    final AtomicInteger id = new AtomicInteger();
    lock.writeLock().lock();/*from w w w.jav  a2  s.  com*/
    for (int i = 0; i < nbThreads; i++) {
        new Thread() {
            public void run() {
                Channel client = null;
                try {
                    client = nmr1.createChannel();
                    lock.readLock().lock();
                    for (int i = 0; i < nbExchanges; i++) {
                        Exchange exchange = client.createExchange(Pattern.InOnly);
                        exchange.getIn()
                                .setBody(new StringSource("<hello id='" + id.getAndIncrement() + "'/>"));
                        exchange.setTarget(nmr1.getEndpointRegistry()
                                .lookup(ServiceHelper.createMap(Endpoint.NAME, PROXY_ENDPOINT_NAME)));
                        client.sendSync(exchange);
                        assertEquals(Status.Done, exchange.getStatus());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.readLock().unlock();
                    latch.countDown();
                    if (client != null) {
                        client.close();
                    }
                }
            }
        }.start();
    }

    long t0, t1;

    t0 = System.currentTimeMillis();
    lock.writeLock().unlock();

    latch.await();

    receiver.assertExchangesReceived(nbThreads * nbExchanges, TIMEOUT);

    t1 = System.currentTimeMillis();

    System.err.println("Elapsed time: " + (t1 - t0) + " ms");
    System.err.println("Throuput: " + (nbThreads * nbExchanges * 1000 / (t1 - t0)) + " messages/sec");
}

From source file:org.apache.asterix.experiment.client.StatisticsQueryGenerator.java

public StatisticsQueryGenerator(StatisticsQueryGeneratorConfig config) {
    threadPool = Executors.newCachedThreadPool(new ThreadFactory() {

        private final AtomicInteger count = new AtomicInteger();

        @Override/*from www  .j a  v  a2 s .c o m*/
        public Thread newThread(Runnable r) {
            int tid = count.getAndIncrement();
            Thread t = new Thread(r, "DataGeneratorThread: " + tid);
            t.setDaemon(true);
            return t;
        }
    });
    this.config = config;
}