Example usage for java.util.concurrent ConcurrentLinkedQueue ConcurrentLinkedQueue

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

Introduction

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

Prototype

public ConcurrentLinkedQueue() 

Source Link

Document

Creates a ConcurrentLinkedQueue that is initially empty.

Usage

From source file:org.glassfish.jersey.examples.sseitemstore.jersey.JerseyItemStoreResourceTest.java

/**
 * Test the {@link EventSource} reconnect feature.
 *
 * @throws Exception in case of a test failure.
 *///from   w  w  w. java  2 s  . c om
@Test
public void testEventSourceReconnect() throws Exception {
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(MAX_ITEMS * MAX_LISTENERS * 2); // countdown only on new item events
    final List<Queue<String>> receivedQueues = new ArrayList<>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];

    for (int i = 0; i < MAX_LISTENERS; i++) {
        final int id = i;
        final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
        sources[id] = es;

        final Queue<String> received = new ConcurrentLinkedQueue<>();
        receivedQueues.add(received);

        es.register(inboundEvent -> {
            try {
                if (inboundEvent.getName() == null) {
                    latch.countDown();
                    final String data = inboundEvent.readData();
                    LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId() + " data="
                            + data);
                    received.add(data);
                }
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                received.add("[data processing error]");
            }
        });
    }

    final String[] postedItems = new String[MAX_ITEMS * 2];
    try {
        open(sources);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-1-%02d", i);
            postItem(itemsTarget, item);
            postedItems[i] = item;
            sendCommand(itemsTarget, "disconnect");
            Thread.sleep(100);
        }

        final int reconnectDelay = 1;
        sendCommand(itemsTarget, "reconnect " + reconnectDelay);
        sendCommand(itemsTarget, "disconnect");

        Thread.sleep(reconnectDelay * 1000);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-2-%02d", i);
            postedItems[i + MAX_ITEMS] = item;
            postItem(itemsTarget, item);
        }

        sendCommand(itemsTarget, "reconnect now");

        assertTrue("Waiting to receive all events has timed out.",
                latch.await(
                        (1 + MAX_LISTENERS * (MAX_ITEMS + 1) * reconnectDelay) * getAsyncTimeoutMultiplier(),
                        TimeUnit.SECONDS));

        // need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
        sendCommand(itemsTarget, "disconnect");
    } finally {
        close(sources);
    }

    final String storedItems = itemsTarget.request().get(String.class);
    for (String item : postedItems) {
        assertThat("Posted item '" + item + "' stored on server", storedItems, containsString(item));
    }

    int sourceId = 0;
    for (Queue<String> queue : receivedQueues) {
        assertThat("Received events in source " + sourceId, queue, describedAs("Collection containing %0",
                hasItems(postedItems), Arrays.asList(postedItems).toString()));
        assertThat("Size of received queue for source " + sourceId, queue.size(), equalTo(postedItems.length));
        sourceId++;
    }
}

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

/**
 * Constructor.//w  w w.  ja  va  2  s . com
 * @param job
 * @param partitionID
 */
public MessageQueuesForDisk(BSPJob job, int partitionID) {
    LOG.info("========== Initializing Message Queues Data For Disk ==========");
    this.dataPercent = job.getMemoryDataPercent(); // Default 0.8
    this.jobID = job.getJobID();
    this.partitionID = partitionID;
    // ratio of graph data
    this.beta = job.getBeta();
    this.hashBucketNumber = job.getHashBucketNumber();
    LOG.info("[beta] = " + this.beta);
    LOG.info("[hashBucketNumber] = " + this.hashBucketNumber);
    // the structure of three kinds message queues
    this.outgoingQueues = new ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>>();
    this.incomingQueues = new ArrayList<BucketMeta>(this.hashBucketNumber);
    this.incomedQueues = new ArrayList<BucketMeta>(this.hashBucketNumber);
    for (int i = 0; i < this.hashBucketNumber; i++) {
        BucketMeta meta = new BucketMeta();
        meta.onDiskFlag = false;
        meta.length = 0;
        meta.lengthInMemory = 0;
        meta.count = 0;
        meta.countInMemory = 0;
        meta.queueMap = new ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>>();
        this.incomingQueues.add(meta);
        meta = new BucketMeta();
        meta.onDiskFlag = false;
        meta.length = 0;
        meta.lengthInMemory = 0;
        meta.count = 0;
        meta.countInMemory = 0;
        meta.queueMap = new ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>>();
        this.incomedQueues.add(meta);
    }
    // Initialize the bucket file locks
    this.incomedFileLocks = new ReentrantLock[this.hashBucketNumber];
    this.incomingFileLocks = new ReentrantLock[this.hashBucketNumber];
    for (int i = 0; i < this.hashBucketNumber; i++) {
        this.incomedFileLocks[i] = new ReentrantLock();
        this.incomingFileLocks[i] = new ReentrantLock();
    }
    // Initialize the size of objects.
    BSPConfiguration conf = new BSPConfiguration();
    if (conf.getInt(Constants.BC_BSP_JVM_VERSION, 32) == 64) {
        sizer = ObjectSizer.forSun64BitsVM();
    } else {
        sizer = ObjectSizer.forSun32BitsVM();
    }
    this.sizeOfRef = sizer.sizeOfRef();
    this.sizeOfInteger = sizer.sizeOf(new Integer(0));
    this.sizeOfChar = sizer.sizeOfChar();
    this.sizeOfEmptyMessageQueue = sizer.sizeOf(new ConcurrentLinkedQueue<IMessage>());
    // Size will be evaluted later based on first m received messages.
    this.sizeOfMessage = MESSAGE_SIZE; // Default
    // Get the memory mxBean.
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    // Get the heap memory usage.
    MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
    long maxHeapSize = memoryUsage.getMax();
    LOG.info("[JVM max Heap size] = " + maxHeapSize / MB_SIZE + "MB");
    this.sizeOfMessagesSpace = (long) (maxHeapSize * dataPercent * (1.0f - beta));
    this.sizeThreshold = (long) (sizeOfMessagesSpace);
    this.countThreshold = (long) (sizeThreshold / sizeOfMessage);
    this.countThresholdForBucket = this.countThreshold / (3 * this.hashBucketNumber);
    LOG.info("[size of Messages Space Threshold] = " + this.sizeThreshold / MB_SIZE + "MB");
    LOG.info("[count of Messages In Memory Threshold] = " + this.countThreshold / KB_SIZE + "K");
    this.sizeOfMessagesDataInMem = 0;
    this.countOfMessagesDataInMem = 0;
    this.totalSizeOfMessages = 0;
    this.totalCount = 0;
    this.sizeOfHashMapsInMem = 0;
    this.fileRoot = new File("/tmp/bcbsp/" + this.jobID.toString() + "/" + "partition-" + this.partitionID);
    this.messagesDataFile = new File(this.fileRoot + "/" + "MessagesData");
    // If the root dir does not exit, create it.
    if (!this.fileRoot.exists()) {
        this.fileRoot.mkdirs();
    }
    // If the messages data dir does not exit, create it.
    if (!this.messagesDataFile.exists()) {
        this.messagesDataFile.mkdir();
    }
    // Initialize the current accessed bucket index.
    this.currentBucket = -1;
    LOG.info("===============================================================");
}

From source file:com.jtxdriggers.android.ventriloid.VentriloidService.java

@Override
public void onCreate() {
    stopForeground(true);/*from  www.  j  a  v  a 2s  .c  o m*/

    new Thread(new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            handler = new Handler();
            Looper.loop();
        }
    }).start();

    items = new ItemData(this);
    player = new Player(this);
    recorder = new Recorder(this);

    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (prefs.getString("notification_type", "Text to Speech").equals("Text to Speech")) {
        ttsActive = true;
        tts = new TextToSpeech(VentriloidService.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS)
                    ttsActive = true;
                else {
                    ttsActive = false;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "TTS Initialization faled.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    } else if (prefs.getString("notification_type", "Text to Speech").equals("Ringtone"))
        ringtoneActive = true;

    registerReceiver(activityReceiver, new IntentFilter(ACTIVITY_RECEIVER));

    nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    voiceActivation = prefs.getBoolean("voice_activation", false);
    threshold = voiceActivation ? 55.03125 : -1;
    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibrate = prefs.getBoolean("vibrate", true);

    queue = new ConcurrentLinkedQueue<VentriloEventData>();

    //VentriloInterface.debuglevel(65535);
    new Thread(eventHandler).start();
}

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 .j a v a  2  s  .c  o  m
 * @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:org.glassfish.jersey.examples.sseitemstore.ItemStoreResourceTest.java

/**
 * Test the {@link EventSource} reconnect feature.
 *
 * @throws Exception in case of a test failure.
 *///from   ww w .  j  a v  a2 s .  co  m
@Test
public void testEventSourceReconnect() throws Exception {
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(MAX_ITEMS * MAX_LISTENERS * 2); // countdown only on new item events
    final List<Queue<String>> receivedQueues = new ArrayList<Queue<String>>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];

    for (int i = 0; i < MAX_LISTENERS; i++) {
        final int id = i;
        final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
        sources[id] = es;

        final Queue<String> received = new ConcurrentLinkedQueue<String>();
        receivedQueues.add(received);

        es.register(new EventListener() {
            @Override
            public void onEvent(InboundEvent inboundEvent) {
                try {
                    if (inboundEvent.getName() == null) {
                        latch.countDown();
                        final String data = inboundEvent.readData();
                        LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId()
                                + " data=" + data);
                        received.add(data);
                    }
                } catch (Exception ex) {
                    LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                    received.add("[data processing error]");
                }
            }
        });
    }

    final String[] postedItems = new String[MAX_ITEMS * 2];
    try {
        open(sources);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-1-%02d", i);
            postItem(itemsTarget, item);
            postedItems[i] = item;
            sendCommand(itemsTarget, "disconnect");
            Thread.sleep(100);
        }

        final int reconnectDelay = 1;
        sendCommand(itemsTarget, "reconnect " + reconnectDelay);
        sendCommand(itemsTarget, "disconnect");

        Thread.sleep(reconnectDelay * 1000);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-2-%02d", i);
            postedItems[i + MAX_ITEMS] = item;
            postItem(itemsTarget, item);
        }

        sendCommand(itemsTarget, "reconnect now");

        assertTrue("Waiting to receive all events has timed out.",
                latch.await(
                        (1 + MAX_LISTENERS * (MAX_ITEMS + 1) * reconnectDelay) * getAsyncTimeoutMultiplier(),
                        TimeUnit.SECONDS));

        // need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
        sendCommand(itemsTarget, "disconnect");
    } finally {
        close(sources);
    }

    final String storedItems = itemsTarget.request().get(String.class);
    for (String item : postedItems) {
        assertThat("Posted item '" + item + "' stored on server", storedItems, containsString(item));
    }

    int sourceId = 0;
    for (Queue<String> queue : receivedQueues) {
        assertThat("Received events in source " + sourceId, queue, describedAs("Collection containing %0",
                hasItems(postedItems), Arrays.asList(postedItems).toString()));
        assertThat("Size of received queue for source " + sourceId, queue.size(), equalTo(postedItems.length));
        sourceId++;
    }
}

From source file:com.ibm.crail.tools.CrailBenchmark.java

void readSequential(String filename, int size, int loop, boolean buffered) throws Exception {
    System.out.println("readSequential, filename " + filename + ", size " + size + ", loop " + loop
            + ", buffered " + buffered);

    CrailBuffer buf = null;/*  w  ww.ja  v a  2  s  . c om*/
    if (size == CrailConstants.BUFFER_SIZE) {
        buf = fs.allocateBuffer();
    } else if (size < CrailConstants.BUFFER_SIZE) {
        CrailBuffer _buf = fs.allocateBuffer();
        _buf.clear().limit(size);
        buf = _buf.slice();
    } else {
        buf = OffHeapBuffer.wrap(ByteBuffer.allocateDirect(size));
    }

    //warmup
    ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>();
    bufferQueue.add(buf);
    warmUp(filename, warmup, bufferQueue);

    CrailFile file = fs.lookup(filename).get().asFile();
    CrailBufferedInputStream bufferedStream = file.getBufferedInputStream(file.getCapacity());
    CrailInputStream directStream = file.getDirectInputStream(file.getCapacity());

    //benchmark
    System.out.println("starting benchmark...");
    fs.getStatistics().reset();
    double sumbytes = 0;
    double ops = 0;
    long start = System.currentTimeMillis();
    while (ops < loop) {
        if (buffered) {
            buf.clear();
            double ret = (double) bufferedStream.read(buf.getByteBuffer());
            if (ret > 0) {
                sumbytes = sumbytes + ret;
                ops = ops + 1.0;
            } else {
                ops = ops + 1.0;
                if (bufferedStream.position() == 0) {
                    break;
                } else {
                    bufferedStream.seek(0);
                }
            }
        } else {
            buf.clear();
            double ret = (double) directStream.read(buf).get().getLen();
            if (ret > 0) {
                sumbytes = sumbytes + ret;
                ops = ops + 1.0;
            } else {
                ops = ops + 1.0;
                if (directStream.position() == 0) {
                    break;
                } else {
                    directStream.seek(0);
                }
            }
        }
    }
    long end = System.currentTimeMillis();
    double executionTime = ((double) (end - start)) / 1000.0;
    double throughput = 0.0;
    double latency = 0.0;
    double sumbits = sumbytes * 8.0;
    if (executionTime > 0) {
        throughput = sumbits / executionTime / 1000.0 / 1000.0;
        latency = 1000000.0 * executionTime / ops;
    }
    bufferedStream.close();
    directStream.close();

    System.out.println("execution time " + executionTime);
    System.out.println("ops " + ops);
    System.out.println("sumbytes " + sumbytes);
    System.out.println("throughput " + throughput);
    System.out.println("latency " + latency);

    fs.getStatistics().print("close");
}

From source file:com.gcm.client.GcmHelper.java

/**
 * Add a GCM Registration listener. This does not replace the existing listeners but adds to the list of listeners
 *
 * @param context              An instance of the application context
 * @param registrationListener The instance of {@link GcmRegistrationListener} which needs to be added
 * @param sticky               if true then token is passed even if it was generated earlier
 *//*from  w w  w  .j a  v  a 2 s .  co m*/
public synchronized GcmHelper addRegistrationCallback(@NonNull Context context,
        @NonNull GcmRegistrationListener registrationListener, boolean sticky) {
    if (null == registrationCallbacks) {
        registrationCallbacks = new ConcurrentLinkedQueue<>();
    }
    registrationCallbacks.add(registrationListener);
    if (sticky && !TextUtils.isEmpty(this.pushToken)) {
        registrationListener.onTokenAvailable(context, pushToken, false);
    }
    return this;
}

From source file:org.jiemamy.utils.collection.CollectionsUtil.java

/**
 * {@link ConcurrentLinkedQueue}?????//from  w w w  . ja v a2s .  c o  m
 * 
 * @param <E> {@link ConcurrentLinkedQueue}??
 * @return {@link ConcurrentLinkedQueue}???
 * @see ConcurrentLinkedQueue#ConcurrentLinkedQueue()
 */
public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue() {
    return new ConcurrentLinkedQueue<E>();
}

From source file:com.gcm.client.GcmHelper.java

/**
 * Add a GCM Messaging listener. This listener will be triggered when a GCM payload is received
 *
 * @param messageReceivedListener The instance of {@link GcmMessageListener} which needs to be added
 *//* ww  w. j a  v a  2 s .c om*/
public synchronized GcmHelper addOnMessageReceivedCallback(
        @NonNull GcmMessageListener messageReceivedListener) {
    if (null == messageReceivedCallbacks) {
        messageReceivedCallbacks = new ConcurrentLinkedQueue<>();
    }
    messageReceivedCallbacks.add(messageReceivedListener);
    return this;
}