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.chinamobile.bcbsp.graph.GraphDataMananger.java

@Override
public void getAllVertex(GraphStaffHandler graphStaffHandler, CommunicatorInterface communicator,
        RecordWriter output) throws IOException, InterruptedException {
    // TODO Auto-generated method stub
    try {/*www  .  j av  a 2  s. c om*/
        Vertex v = vertexClass.newInstance();
        for (int i = (MetaDataOfGraph.BCBSP_DISKGRAPH_HASHNUMBER - 1); i >= 0; i--) {
            int counter = MetaDataOfGraph.VERTEX_NUM_PERBUCKET[i];
            if (counter == 0) {
                continue;
            }
            this.prepareBucket(i, this.lastSuperstepCounter + 1);
            graphStaffHandler.preBucket(i, this.lastSuperstepCounter + 1);
            for (int j = 0; j < counter; j++) {
                fillVertex(v);
                LOG.info("Feng test! vertex is null " + v.getVertexID().toString());
                //this.vertexlist.add(v);
                //graphStaffHandler.vertexProcessing(v, bsp, job, superStepCounter,
                //  context, true);
                //      //Vertex<?, ?, Edge> vertex = graphData.getForAll(i);
                String vertexID = v.getVertexID().toString();
                //Iterator<IMessage> it = communicator.getMessageIterator(vertexID);
                ConcurrentLinkedQueue<IMessage> messages = communicator
                        .getMessageQueue(String.valueOf(v.getVertexID()));
                if (messages.size() == 0) {
                    LOG.info("Feng test vertex " + v.getVertexID().toString() + " message is empty!");
                    continue;
                }
                Iterator<IMessage> messagesIter = messages.iterator();
                StringBuffer sb = new StringBuffer();
                //sb.append(vertexID + Constants.MESSAGE_SPLIT);
                while (messagesIter.hasNext()) {
                    IMessage msg = messagesIter.next();
                    String info = msg.intoString();
                    if (info != null) {
                        sb.append(info + Constants.SPACE_SPLIT_FLAG);
                    }
                }
                if (sb.length() > 0) {
                    int k = sb.length();
                    sb.delete(k - 1, k - 1);
                }
                //sb.append("\n");
                output.write(new Text(v.getVertexID() + Constants.MESSAGE_SPLIT), new Text(sb.toString()));
            }

            //  OUT.close()
            this.vManager.processVertexSave(v);

            this.finishPreparedBucket();
        }
    } catch (InstantiationException e) {
        throw new RuntimeException("[Graph Data Manageer] saveAllVertices", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("[Graph Data Manageer] saveAllVertices", e);
    }
}

From source file:org.atmosphere.cpr.PoolableBroadcasterFactoryTest.java

@Test
public void concurrentLookupTest() throws InterruptedException {
    String id = "id";
    final CountDownLatch latch = new CountDownLatch(100);
    final AtomicInteger created = new AtomicInteger();

    factory.addBroadcasterListener(new BroadcasterListenerAdapter() {
        @Override/*from w  ww .j  av a 2 s.co m*/
        public void onPostCreate(Broadcaster b) {
            created.incrementAndGet();
        }

        @Override
        public void onComplete(Broadcaster b) {

        }

        @Override
        public void onPreDestroy(Broadcaster b) {

        }
    });

    final ConcurrentLinkedQueue<Broadcaster> c = new ConcurrentLinkedQueue<Broadcaster>();
    ExecutorService r = Executors.newCachedThreadPool();
    for (int i = 0; i < 100; i++) {
        r.submit(new Runnable() {
            @Override
            public void run() {
                c.add(factory.lookup("name" + UUID.randomUUID().toString(), true));
                latch.countDown();
            }
        });
    }

    try {
        assertTrue(latch.await(20, TimeUnit.SECONDS));
        assertEquals(created.get(), 100);
        assertEquals(c.size(), 100);

        for (Broadcaster b : c) {
            b.destroy();
        }

        assertNotNull(factory.lookup("name" + UUID.randomUUID().toString(), true).broadcast("test"));

        assertEquals(factory.poolableProvider().poolSize(), 100);

    } finally {
        factory.destroy();
        r.shutdownNow();
    }

}

From source file:com.chinamobile.bcbsp.comm.BDBMap.java

/**
 * When duplicates are allowed, this method return all values associated with
 * the key//from  w w w  .ja  va2  s. c  o m
 */
public ConcurrentLinkedQueue<IMessage> getDupilcates(K key) {

    String tmp;
    int indexOf$;
    Collection<V> valueSet = storedMap.duplicates(key);
    ConcurrentLinkedQueue<IMessage> list = new ConcurrentLinkedQueue<IMessage>();

    Iterator<V> it = valueSet.iterator();
    while (it.hasNext()) {
        // Note Need To Be Upgraded.
        IMessage tmpmsg = new BSPMessage();
        tmp = it.next().toString();
        indexOf$ = tmp.indexOf('$');
        tmpmsg.fromString(tmp.substring(indexOf$ + 1));

        list.add(tmpmsg);
    }
    storedMap.remove(key);
    bdbMapSize.addAndGet(-list.size());
    return list;

}

From source file:org.atmosphere.cpr.PoolableBroadcasterFactoryTest.java

@Test
public void concurrentAccessLookupTest() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1000);
    final AtomicInteger created = new AtomicInteger();
    factory.poolableProvider(new UnboundedApachePoolableProvider());
    factory.addBroadcasterListener(new BroadcasterListenerAdapter() {
        @Override/*from  w  ww  . ja  va 2s.  co  m*/
        public void onPostCreate(Broadcaster b) {
            created.incrementAndGet();
        }

        @Override
        public void onComplete(Broadcaster b) {

        }

        @Override
        public void onPreDestroy(Broadcaster b) {

        }
    });

    final ConcurrentLinkedQueue<Broadcaster> c = new ConcurrentLinkedQueue<Broadcaster>();
    ExecutorService r = Executors.newCachedThreadPool();
    final String me = new String("me");
    for (int i = 0; i < 1000; i++) {
        r.submit(new Runnable() {
            @Override
            public void run() {
                c.add(factory.get(me));
                latch.countDown();
            }
        });

    }
    try {
        assertTrue(latch.await(20, TimeUnit.SECONDS));
        assertEquals(latch.getCount(), 0);
        assertEquals(c.size(), 1000);
        assertEquals(created.get(), 1000);

        for (Broadcaster b : c) {
            b.destroy();
        }

        assertNotNull(factory.lookup("name" + UUID.randomUUID().toString(), true).broadcast("test"));

        assertEquals(factory.poolableProvider().poolSize(), 1000);

    } finally {
        factory.destroy();
        r.shutdownNow();
    }

}

From source file:metlos.executors.batch.BatchExecutorTest.java

public void testChangesInTaskCollectionPickedUpInRepetitions() throws Exception {
    final ConcurrentLinkedQueue<Runnable> tasks = new ConcurrentLinkedQueue<Runnable>();
    final AtomicInteger reportedNofTasks = new AtomicInteger();
    final CountDownLatch waitForTask2 = new CountDownLatch(2);
    final CountDownLatch waitForTask3 = new CountDownLatch(2);

    Runnable task1 = new Runnable() {
        @Override/*from ww  w.  jav a2  s .c  om*/
        public void run() {
        }
    };

    Runnable task2 = new Runnable() {
        @Override
        public void run() {
            if (tasks.size() == 2) {
                reportedNofTasks.set(2);
                waitForTask2.countDown();
            }
        }
    };

    Runnable task3 = new Runnable() {
        @Override
        public void run() {
            if (tasks.size() == 3) {
                reportedNofTasks.set(3);
                waitForTask3.countDown();
            }
        }
    };

    BatchExecutor ex = getExecutor(10);

    tasks.add(task1);
    tasks.add(task2);

    ex.submitWithPreferedDurationAndFixedDelay(tasks, 0, 0, 0, TimeUnit.MILLISECONDS);

    //k, now the tasks should be running and there should be just 2 of them...
    //so we should be getting the value of "2" reported by the reportedNofTasks

    waitForTask2.countDown();
    waitForTask2.await();

    int currentReportedTasks = reportedNofTasks.get();

    assert currentReportedTasks == 2 : "We should be getting 2 tasks reported but are getting "
            + currentReportedTasks + " instead.";

    //k, now let's try updating the tasks collection... this should get picked up by the
    //repeated executions 
    tasks.add(task3);

    //now the reported nof tasks should change to 3. let's wait on it first to make sure the executor has had time to 
    //register the change.        
    waitForTask3.countDown();
    waitForTask3.await();

    currentReportedTasks = reportedNofTasks.get();

    assert currentReportedTasks == 3 : "We should be getting 3 tasks reported but are getting "
            + currentReportedTasks + " instead.";

    ex.shutdown();
}

From source file:com.linkedin.pinot.perf.QueryRunner.java

/**
 * Use multiple threads to run query at an increasing target QPS.
 *
 * Use a concurrent linked queue to buffer the queries to be sent. Use the main thread to insert queries into the
 * queue at the target QPS, and start {numThreads} worker threads to fetch queries from the queue and send them.
 * We start with the start QPS, and keep adding delta QPS to the start QPS during the test. The main thread is
 * responsible for collecting the statistic information and log them periodically.
 *
 * @param conf perf benchmark driver config.
 * @param queryFile query file./*from ww  w  .ja va2 s  .c  om*/
 * @param numThreads number of threads sending queries.
 * @param startQPS start QPS
 * @param deltaQPS delta QPS
 * @throws Exception
 */
@SuppressWarnings("InfiniteLoopStatement")
public static void targetQPSQueryRunner(PerfBenchmarkDriverConf conf, String queryFile, int numThreads,
        double startQPS, double deltaQPS) throws Exception {
    final long randomSeed = 123456789L;
    final Random random = new Random(randomSeed);
    final int timePerTargetQPSMillis = 60000;
    final int queueLengthThreshold = Math.max(20, (int) startQPS);

    final List<String> queries;
    try (FileInputStream input = new FileInputStream(new File(queryFile))) {
        queries = IOUtils.readLines(input);
    }
    final int numQueries = queries.size();

    final PerfBenchmarkDriver driver = new PerfBenchmarkDriver(conf);
    final AtomicInteger counter = new AtomicInteger(0);
    final AtomicLong totalResponseTime = new AtomicLong(0L);
    final ExecutorService executorService = Executors.newFixedThreadPool(numThreads);

    final ConcurrentLinkedQueue<String> queryQueue = new ConcurrentLinkedQueue<>();
    double currentQPS = startQPS;
    int intervalMillis = (int) (MILLIS_PER_SECOND / currentQPS);

    for (int i = 0; i < numThreads; i++) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String query = queryQueue.poll();
                    if (query == null) {
                        try {
                            Thread.sleep(1);
                            continue;
                        } catch (InterruptedException e) {
                            LOGGER.error("Interrupted.", e);
                            return;
                        }
                    }
                    long startTime = System.currentTimeMillis();
                    try {
                        driver.postQuery(query);
                        counter.getAndIncrement();
                        totalResponseTime.getAndAdd(System.currentTimeMillis() - startTime);
                    } catch (Exception e) {
                        LOGGER.error("Caught exception while running query: {}", query, e);
                        return;
                    }
                }
            }
        });
    }

    LOGGER.info("Start with QPS: {}, delta QPS: {}", startQPS, deltaQPS);
    while (true) {
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime <= timePerTargetQPSMillis) {
            if (queryQueue.size() > queueLengthThreshold) {
                executorService.shutdownNow();
                throw new RuntimeException("Cannot achieve target QPS of: " + currentQPS);
            }
            queryQueue.add(queries.get(random.nextInt(numQueries)));
            Thread.sleep(intervalMillis);
        }
        double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND;
        int count = counter.getAndSet(0);
        double avgResponseTime = ((double) totalResponseTime.getAndSet(0)) / count;
        LOGGER.info("Target QPS: {}, Interval: {}ms, Actual QPS: {}, Avg Response Time: {}ms", currentQPS,
                intervalMillis, count / timePassedSeconds, avgResponseTime);

        // Find a new interval
        int newIntervalMillis;
        do {
            currentQPS += deltaQPS;
            newIntervalMillis = (int) (MILLIS_PER_SECOND / currentQPS);
        } while (newIntervalMillis == intervalMillis);
        intervalMillis = newIntervalMillis;
    }
}

From source file:com.linkedin.pinot.tools.perf.QueryRunner.java

/**
 * Use multiple threads to run queries as fast as possible.
 * <p>Use a concurrent linked queue to buffer the queries to be sent. Use the main thread to insert queries into the
 * queue whenever the queue length is low, and start <code>numThreads</code> worker threads to fetch queries from the
 * queue and send them./*  www .ja v a 2s  . c om*/
 * <p>The main thread is responsible for collecting and logging the statistic information periodically.
 * <p>Queries are picked sequentially from the query file.
 * <p>Query runner will stop when all queries in the query file has been executed number of times configured.
 *
 * @param conf perf benchmark driver config.
 * @param queryFile query file.
 * @param numTimesToRunQueries number of times to run all queries in the query file, 0 means infinite times.
 * @param numThreads number of threads sending queries.
 * @param reportIntervalMs report interval in milliseconds.
 * @param numIntervalsToReportAndClearStatistics number of report intervals to report detailed statistics and clear
 *                                               them, 0 means never.
 * @throws Exception
 */
public static void multiThreadedQueryRunner(PerfBenchmarkDriverConf conf, String queryFile,
        int numTimesToRunQueries, int numThreads, int reportIntervalMs,
        int numIntervalsToReportAndClearStatistics) throws Exception {
    List<String> queries;
    try (FileInputStream input = new FileInputStream(new File(queryFile))) {
        queries = IOUtils.readLines(input);
    }

    PerfBenchmarkDriver driver = new PerfBenchmarkDriver(conf);
    ConcurrentLinkedQueue<String> queryQueue = new ConcurrentLinkedQueue<>();
    AtomicInteger numQueriesExecuted = new AtomicInteger(0);
    AtomicLong totalBrokerTime = new AtomicLong(0L);
    AtomicLong totalClientTime = new AtomicLong(0L);
    List<Statistics> statisticsList = Collections.singletonList(new Statistics(CLIENT_TIME_STATISTICS));

    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        executorService.submit(new Worker(driver, queryQueue, numQueriesExecuted, totalBrokerTime,
                totalClientTime, statisticsList));
    }
    executorService.shutdown();

    long startTime = System.currentTimeMillis();
    long reportStartTime = startTime;
    int numReportIntervals = 0;
    int numTimesExecuted = 0;
    while (numTimesToRunQueries == 0 || numTimesExecuted < numTimesToRunQueries) {
        if (executorService.isTerminated()) {
            LOGGER.error("All threads got exception and already dead.");
            return;
        }

        for (String query : queries) {
            queryQueue.add(query);

            // Keep 20 queries inside the query queue.
            while (queryQueue.size() == 20) {
                Thread.sleep(1);

                long currentTime = System.currentTimeMillis();
                if (currentTime - reportStartTime >= reportIntervalMs) {
                    long timePassed = currentTime - startTime;
                    int numQueriesExecutedInt = numQueriesExecuted.get();
                    LOGGER.info(
                            "Time Passed: {}ms, Queries Executed: {}, Average QPS: {}, Average Broker Time: {}ms, "
                                    + "Average Client Time: {}ms.",
                            timePassed, numQueriesExecutedInt,
                            numQueriesExecutedInt / ((double) timePassed / MILLIS_PER_SECOND),
                            totalBrokerTime.get() / (double) numQueriesExecutedInt,
                            totalClientTime.get() / (double) numQueriesExecutedInt);
                    reportStartTime = currentTime;
                    numReportIntervals++;

                    if ((numIntervalsToReportAndClearStatistics != 0)
                            && (numReportIntervals == numIntervalsToReportAndClearStatistics)) {
                        numReportIntervals = 0;
                        startTime = currentTime;
                        reportAndClearStatistics(numQueriesExecuted, totalBrokerTime, totalClientTime,
                                statisticsList);
                    }
                }
            }
        }
        numTimesExecuted++;
    }

    // Wait for all queries getting executed.
    while (queryQueue.size() != 0) {
        Thread.sleep(1);
    }
    executorService.shutdownNow();
    while (!executorService.isTerminated()) {
        Thread.sleep(1);
    }

    long timePassed = System.currentTimeMillis() - startTime;
    int numQueriesExecutedInt = numQueriesExecuted.get();
    LOGGER.info("--------------------------------------------------------------------------------");
    LOGGER.info("FINAL REPORT:");
    LOGGER.info(
            "Time Passed: {}ms, Queries Executed: {}, Average QPS: {}, Average Broker Time: {}ms, "
                    + "Average Client Time: {}ms.",
            timePassed, numQueriesExecutedInt,
            numQueriesExecutedInt / ((double) timePassed / MILLIS_PER_SECOND),
            totalBrokerTime.get() / (double) numQueriesExecutedInt,
            totalClientTime.get() / (double) numQueriesExecutedInt);
    for (Statistics statistics : statisticsList) {
        statistics.report();
    }
}

From source file:ua.pp.serga.socketio.IOConnection.java

/**
 * Transport connected.//from   www  .  j  a  v a2 s .  c om
 * 
 * {@link IOTransport} calls this when a connection is established.
 */
public void transportConnected() {
    setState(STATE_READY);
    if (reconnectTask != null) {
        reconnectTask.cancel();
        reconnectTask = null;
    }
    resetTimeout();
    synchronized (outputBuffer) {
        if (transport.canSendBulk()) {
            ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
            this.outputBuffer = new ConcurrentLinkedQueue<String>();
            try {
                // DEBUG
                String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]);
                logger.info("Bulk start:");
                for (String text : texts) {
                    logger.info("> " + text);
                }
                logger.info("Bulk end");
                // DEBUG END
                transport.sendBulk(texts);
            } catch (IOException e) {
                this.outputBuffer = outputBuffer;
            }
        } else {
            String text;
            while ((text = outputBuffer.poll()) != null)
                sendPlain(text);
        }
        this.keepAliveInQueue = false;
    }
}

From source file:metlos.executors.batch.BatchExecutorTest.java

private void runSimpleDelayTest(int nofThreads) throws Exception {
    final ConcurrentLinkedQueue<Long> executionTimes = new ConcurrentLinkedQueue<Long>();

    Runnable task = new Runnable() {
        @Override/*from  w ww .ja v a 2  s .com*/
        public void run() {
            executionTimes.add(System.currentTimeMillis());
        }
    };

    BatchExecutor ex = getExecutor(nofThreads);

    //start running my task... the task should "take" 0ms and there should be a delay
    //of 10ms between executions... the executionTimes collection should therefore
    //contain time stamps 10ms apart from each other.
    ex.submitWithPreferedDurationAndFixedDelay(Collections.singleton(task), 0, 0, 10, TimeUnit.MILLISECONDS);

    Thread.sleep(1000);

    ex.shutdown();

    assert executionTimes.size() > 1 : "There should have been more than 1 task executed.";

    long minDelay = 8; //10ms +- 20%
    long maxDelay = 12;
    int nofElements = executionTimes.size();

    long previousTime = executionTimes.poll();
    long cummulativeDiff = 0;
    while (!executionTimes.isEmpty()) {
        long thisTime = executionTimes.poll();

        long diff = thisTime - previousTime;
        cummulativeDiff += diff;

        previousTime = thisTime;
    }

    long averageDelay = cummulativeDiff / (nofElements - 1);

    assert minDelay < averageDelay && averageDelay < maxDelay : "The average delay should be in <" + minDelay
            + ", " + maxDelay + "> but was " + averageDelay + ".";
}

From source file:com.huobi.demo.socketio.core.IOConnection.java

/**
 * Transport connected./* ww  w  .j a va  2  s . c om*/
 * 
 * {@link IOTransport} calls this when a connection is established.
 */
public synchronized void transportConnected() {
    setState(STATE_READY);
    if (reconnectTask != null) {
        reconnectTask.cancel();
        reconnectTask = null;
    }
    resetTimeout();
    if (transport.canSendBulk()) {
        ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
        this.outputBuffer = new ConcurrentLinkedQueue<String>();
        try {
            // DEBUG
            String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]);
            logger.debug("Bulk start:");
            for (String text : texts) {
                logger.debug("> " + text);
            }
            logger.debug("Bulk end");
            // DEBUG END
            transport.sendBulk(texts);
        } catch (IOException e) {
            this.outputBuffer = outputBuffer;
        }
    } else {
        String text;
        while ((text = outputBuffer.poll()) != null)
            sendPlain(text);
    }
    this.keepAliveInQueue = false;
}