Example usage for java.util.concurrent ConcurrentLinkedQueue size

List of usage examples for java.util.concurrent ConcurrentLinkedQueue size

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentLinkedQueue size.

Prototype

public int size() 

Source Link

Document

Returns the number of elements in this queue.

Usage

From source file:com.ibm.crail.storage.StorageServer.java

public static void main(String[] args) throws Exception {
    Logger LOG = CrailUtils.getLogger();
    CrailConfiguration conf = new CrailConfiguration();
    CrailConstants.updateConstants(conf);
    CrailConstants.printConf();// ww w .j av a 2s  . co m
    CrailConstants.verify();

    int splitIndex = 0;
    for (String param : args) {
        if (param.equalsIgnoreCase("--")) {
            break;
        }
        splitIndex++;
    }

    //default values
    StringTokenizer tokenizer = new StringTokenizer(CrailConstants.STORAGE_TYPES, ",");
    if (!tokenizer.hasMoreTokens()) {
        throw new Exception("No storage types defined!");
    }
    String storageName = tokenizer.nextToken();
    int storageType = 0;
    HashMap<String, Integer> storageTypes = new HashMap<String, Integer>();
    storageTypes.put(storageName, storageType);
    for (int type = 1; tokenizer.hasMoreElements(); type++) {
        String name = tokenizer.nextToken();
        storageTypes.put(name, type);
    }
    int storageClass = -1;

    //custom values
    if (args != null) {
        Option typeOption = Option.builder("t").desc("storage type to start").hasArg().build();
        Option classOption = Option.builder("c").desc("storage class the server will attach to").hasArg()
                .build();
        Options options = new Options();
        options.addOption(typeOption);
        options.addOption(classOption);
        CommandLineParser parser = new DefaultParser();

        try {
            CommandLine line = parser.parse(options, Arrays.copyOfRange(args, 0, splitIndex));
            if (line.hasOption(typeOption.getOpt())) {
                storageName = line.getOptionValue(typeOption.getOpt());
                storageType = storageTypes.get(storageName).intValue();
            }
            if (line.hasOption(classOption.getOpt())) {
                storageClass = Integer.parseInt(line.getOptionValue(classOption.getOpt()));
            }
        } catch (ParseException e) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("Storage tier", options);
            System.exit(-1);
        }
    }
    if (storageClass < 0) {
        storageClass = storageType;
    }

    StorageTier storageTier = StorageTier.createInstance(storageName);
    if (storageTier == null) {
        throw new Exception("Cannot instantiate datanode of type " + storageName);
    }

    String extraParams[] = null;
    splitIndex++;
    if (args.length > splitIndex) {
        extraParams = new String[args.length - splitIndex];
        for (int i = splitIndex; i < args.length; i++) {
            extraParams[i - splitIndex] = args[i];
        }
    }
    storageTier.init(conf, extraParams);
    storageTier.printConf(LOG);

    RpcClient rpcClient = RpcClient.createInstance(CrailConstants.NAMENODE_RPC_TYPE);
    rpcClient.init(conf, args);
    rpcClient.printConf(LOG);

    ConcurrentLinkedQueue<InetSocketAddress> namenodeList = CrailUtils.getNameNodeList();
    ConcurrentLinkedQueue<RpcConnection> connectionList = new ConcurrentLinkedQueue<RpcConnection>();
    while (!namenodeList.isEmpty()) {
        InetSocketAddress address = namenodeList.poll();
        RpcConnection connection = rpcClient.connect(address);
        connectionList.add(connection);
    }
    RpcConnection rpcConnection = connectionList.peek();
    if (connectionList.size() > 1) {
        rpcConnection = new RpcDispatcher(connectionList);
    }
    LOG.info("connected to namenode(s) " + rpcConnection.toString());

    StorageServer server = storageTier.launchServer();
    StorageRpcClient storageRpc = new StorageRpcClient(storageType, CrailStorageClass.get(storageClass),
            server.getAddress(), rpcConnection);

    HashMap<Long, Long> blockCount = new HashMap<Long, Long>();
    long sumCount = 0;
    while (server.isAlive()) {
        StorageResource resource = server.allocateResource();
        if (resource == null) {
            break;
        } else {
            storageRpc.setBlock(resource.getAddress(), resource.getLength(), resource.getKey());
            DataNodeStatistics stats = storageRpc.getDataNode();
            long newCount = stats.getFreeBlockCount();
            long serviceId = stats.getServiceId();

            long oldCount = 0;
            if (blockCount.containsKey(serviceId)) {
                oldCount = blockCount.get(serviceId);
            }
            long diffCount = newCount - oldCount;
            blockCount.put(serviceId, newCount);
            sumCount += diffCount;
            LOG.info("datanode statistics, freeBlocks " + sumCount);
        }
    }

    while (server.isAlive()) {
        DataNodeStatistics stats = storageRpc.getDataNode();
        long newCount = stats.getFreeBlockCount();
        long serviceId = stats.getServiceId();

        long oldCount = 0;
        if (blockCount.containsKey(serviceId)) {
            oldCount = blockCount.get(serviceId);
        }
        long diffCount = newCount - oldCount;
        blockCount.put(serviceId, newCount);
        sumCount += diffCount;

        LOG.info("datanode statistics, freeBlocks " + sumCount);
        Thread.sleep(2000);
    }
}

From source file:playground.johannes.socialnets.SpatialNetworkGenerator.java

/**
 * @param args//w w  w  .  j  a v a2  s .  co m
 * @throws InterruptedException 
 * @throws IOException 
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
    Config config = Gbl.createConfig(args);

    alpha1 = Double.parseDouble(config.getParam("socialnets", "alpha1"));
    alpha2 = Double.parseDouble(config.getParam("socialnets", "alpha2"));
    gamma = Double.parseDouble(config.getParam("socialnets", "gamma"));
    logger.info(String.format("Parameters are: alpha1=%1$s, alpha2=%2$s, gamma=%3$s", alpha1, alpha2, gamma));

    logger.info("Loading network and plans...");
    ScenarioData data = new ScenarioData(config);
    final NetworkLayer network = data.getNetwork();
    final Population population = data.getPopulation();

    logger.info("Creating egos...");
    SocialNetwork socialNet = new SocialNetwork();
    ConcurrentLinkedQueue<Ego> egos = new ConcurrentLinkedQueue<Ego>();
    for (Person person : population) {
        egos.add(socialNet.addEgo(person));
    }

    logger.info("Initializing threads step 1...");
    double scale = 1;//Double.parseDouble(args[1]);
    InternalThread.totalEgos = egos.size();
    int count = 1;//Runtime.getRuntime().availableProcessors();
    InternalThread[] threads = new InternalThread[count];
    for (int i = 0; i < count; i++) {
        threads[i] = new InternalThread(network, socialNet, egos, scale, i);
    }
    for (Thread thread : threads) {
        thread.start();
    }
    for (Thread thread : threads) {
        thread.join();
    }

    dumpStats(socialNet, args[1], 0);

    for (int k = 0; k < 0; k++) {
        logger.info("Initializing threads setp " + (k + 2) + "...");

        Step2Thread.totalEgos = socialNet.getVertices().size();
        Step2Thread[] threads2 = new Step2Thread[count];
        for (int i = 0; i < count; i++) {
            threads2[i] = new Step2Thread(socialNet, new Random(i * (k + 1)));
        }
        for (Thread thread : threads2) {
            thread.start();
        }
        for (Thread thread : threads2) {
            thread.join();
        }

        dumpStats(socialNet, args[1], k + 1);
    }
}

From source file:com.interacciones.mxcashmarketdata.driver.queue.QueueWriteFile.java

public void ReadQueue(ConcurrentLinkedQueue<String> msgQueue) {
    LOGGER.debug("Reading from queue... Sizing " + msgQueue.size());

    while (true) {
        Iterator<String> it = msgQueue.iterator();

        while (it.hasNext()) {
            String msg = (String) it.next();
            //messageProcessing.receive(msg.toString());
            msgQueue.poll();/*ww  w .  java  2 s . c o  m*/
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
            LOGGER.error("Error: " + e.getMessage());
        }
    }
}

From source file:com.interacciones.mxcashmarketdata.mama.queue.QueueReader.java

public void ReadQueue(ConcurrentLinkedQueue<Parser> msgQueue) {

    LOGGER.debug("Reading from queue... Sizing " + msgQueue.size());

    while (true) {
        Iterator<Parser> it = msgQueue.iterator();

        while (it.hasNext()) {
            Parser msg = (Parser) it.next();
            LOGGER.debug("Message Type: " + msg.TypeMessage());
            LOGGER.debug("Symbol (Emisora):" + msg.Emisora());
            sendMessage.sendMessage(msg);
            msgQueue.poll();/*from   www .  j  a  v a 2 s. c o m*/
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
            LOGGER.error("Error: " + e.getMessage());
        }
    }
}

From source file:com.gistlabs.mechanize.cache.HttpCacheFilterTest.java

@Test
public void confirmConcurrentLinkedQueueOfferSemantics() {
    ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();

    assertTrue(queue.offer("a"));
    assertEquals(1, queue.size());

    assertTrue(queue.offer("b"));
    assertEquals(2, queue.size());// w  w w. j  a  va2  s .  c  o  m
    assertEquals("a", queue.peek());

    assertTrue(queue.offer("a"));
    //assertEquals(2, queue.size());
    //assertEquals("b", queue.peek());
    assertEquals(3, queue.size());
    assertEquals("a", queue.peek());
}

From source file:com.clican.pluto.cluster.base.BaseMessageDispatcher.java

public void dispatch(Message msg) throws SynchronizeException {
    String msgName = msg.getName();
    ConcurrentLinkedQueue<IMessageHandler> handlerQueue = messageHandlerMapping.get(msgName);
    if (handlerQueue != null && handlerQueue.size() != 0) {
        Iterator<IMessageHandler> it = handlerQueue.iterator();
        List<IMessageHandler> executedHandlerList = new ArrayList<IMessageHandler>();
        while (it.hasNext()) {
            try {
                IMessageHandler handler = it.next();
                handler.handle(msg);/*from   www . ja  va2  s.c om*/
                executedHandlerList.add(handler);
            } catch (SynchronizeException e) {
                for (IMessageHandler handler : executedHandlerList) {
                    try {
                        handler.rollback(msg);
                    } catch (Throwable t) {
                        log.error("", e);
                    }
                }
                throw e;
            }
        }
    }
}

From source file:org.dlw.ai.blackboard.Controller.java

/**
 * Public method to cycle each KnowledgeSource and evaluate the current
 * blackboard problem string (repeatable method)
 *//*ww  w  .  j a  va  2 s.  c  om*/
public final void processNextHint() {

    KnowledgeSourcesImpl knowledgeSources = (KnowledgeSourcesImpl) getKnowledgeSources();

    Collections.sort(knowledgeSources);

    /**
     * go thru ks experts and choose the best one to go to the blackboard
     */
    for (KnowledgeSource ks : knowledgeSources) {

        ks.evaluate();
        state = ControllerState.EVALUATING;

        ConcurrentLinkedQueue<Assumption> queue = ks.getPastAssumptions();

        if (queue.size() > 0) {
            activeKnowledgeSource = ks;
            break;
        }

    }

    log.info("processNextHint->The " + activeKnowledgeSource.toString() + " is now active.");

    if (activeKnowledgeSource != null) {

        /**
         * Take a turn at the board
         */
        visitBlackboard(activeKnowledgeSource);

        /**
         * Step down and give the next expert a chance
         */
        leaveBlackboard(activeKnowledgeSource);

        /**
         * Null activeKnowledgeSource
         */
        activeKnowledgeSource = null;
    }

}

From source file:org.dlw.ai.blackboard.Blackboard.java

/**
 * Public method allowing expert (knowledge source) a chance at the
 * blackboard model to evaluate the problem domain
 * //from  www  .ja v  a2  s  . c o m
 * @param ks
 *            the {@link org.dlw.ai.blackboard.knowledge.KnowledgeSource} at
 *            the board (presently)
 */
public final void connect(KnowledgeSource ks) {

    ConcurrentLinkedQueue<Assumption> queue = ks.getPastAssumptions();

    if (queue.size() > 0) {

        Assumption assumption = queue.peek();

        /**
         * Search the ArrayList for our one and only sentence
         */
        Sentence sentence = getSentence();

        /**
         * make affirmation statement on letter-stack (push assumptions)
         */
        updateAffirmations(sentence, assumption);
    }

    //BlackboardUtil.outputSnapshot(this);
    BlackboardUtil.outputProgress(this);

}

From source file:com.mobilehelix.appserver.push.PushManager.java

public void removeSession(String uniqueID, String combinedUser) {
    idMap.remove(uniqueID);/*from   w ww .  j  a va 2 s  .  c o m*/
    ConcurrentLinkedQueue<PushReceiver> receivers = this.userPushMap.get(combinedUser);
    if (receivers != null) {
        Integer idx = 0;
        Integer nsessions = receivers.size();
        Iterator<PushReceiver> it = receivers.iterator();
        while (it.hasNext()) {
            PushReceiver pr = it.next();
            if (pr.getUniqueID().equals(uniqueID)) {
                LOG.log(Level.WARNING, "Removing push session {0} or {1} for combined user {3}",
                        new Object[] { idx, nsessions, combinedUser });
                receivers.iterator().remove();
                break;
            }
            ++idx;
        }
    }
}

From source file:org.onehippo.cms7.brokenlinks.LinkChecker.java

private void runCheckerThreads(final Iterable<Link> links) {

    ConcurrentLinkedQueue<Link> queue = new ConcurrentLinkedQueue<Link>();

    for (Link link : links) {
        queue.add(link);/* ww  w.  ja  v a2  s.  c o m*/
    }

    final int threadCount = Math.min(queue.size(), nrOfThreads);
    final AtomicInteger internalLinksChecked = new AtomicInteger();

    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; i++) {
        threads[i] = new LinkCheckerRunner(queue, internalLinksChecked);
        threads[i].setUncaughtExceptionHandler(new LogUncaughtExceptionHandler(log));
    }

    for (int i = 0; i < threadCount; i++) {
        threads[i].start();
    }

    try {
        for (int i = 0; i < threadCount; i++) {
            threads[i].join();
        }
    } catch (InterruptedException ex) {
        // aborted
    }

    try {
        session.refresh(false);
    } catch (RepositoryException e) {
        log.warn("Failed to clear the session.", e);
    }
}