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:io.pravega.test.integration.MultiReadersEndToEndTest.java

private Collection<Integer> readAllEvents(final int numParallelReaders, ClientFactory clientFactory,
        final String readerGroupName, final int numSegments) {
    ConcurrentLinkedQueue<Integer> read = new ConcurrentLinkedQueue<>();
    final ExecutorService executorService = Executors.newFixedThreadPool(numParallelReaders,
            new ThreadFactoryBuilder().setNameFormat("testreader-pool-%d").build());
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < numParallelReaders; i++) {
        futures.add(executorService.submit(() -> {
            final String readerId = UUID.randomUUID().toString();
            @Cleanup//from   w  ww  .j  av a2  s .c  om
            final EventStreamReader<Integer> reader = clientFactory.createReader(readerId, readerGroupName,
                    new IntegerSerializer(), ReaderConfig.builder().build());
            int emptyCount = 0;
            while (emptyCount <= numSegments) {
                try {
                    final Integer integerEventRead = reader.readNextEvent(100).getEvent();
                    if (integerEventRead != null) {
                        read.add(integerEventRead);
                        emptyCount = 0;
                    } else {
                        emptyCount++;
                    }
                } catch (ReinitializationRequiredException e) {
                    throw new RuntimeException(e);
                }
            }
        }));
    }

    // Wait until all readers are done.
    futures.forEach(f -> FutureHelpers.getAndHandleExceptions(f, RuntimeException::new));
    executorService.shutdownNow();
    return read;
}

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

/** combine the message queues.
 * @param outgoingQueue//ww  w .  ja va2 s.com
 */
private ConcurrentLinkedQueue<IMessage> combine(ConcurrentLinkedQueue<IMessage> outgoingQueue) {
    // Map of outgoing queues indexed by destination vertex ID.
    TreeMap<String, ConcurrentLinkedQueue<IMessage>> outgoingQueues = new TreeMap<String, ConcurrentLinkedQueue<IMessage>>();
    ConcurrentLinkedQueue<IMessage> tempQueue = null;
    IMessage tempMessage = null;
    // Traverse the outgoing queue and put the messages with the same
    // dstVertexID into the same queue in the tree map.
    Iterator<IMessage> iter = outgoingQueue.iterator();
    String dstVertexID = null;
    /**The result queue for return.*/
    ConcurrentLinkedQueue<IMessage> resultQueue = new ConcurrentLinkedQueue<IMessage>();
    while (iter.hasNext()) {
        tempMessage = iter.next();
        dstVertexID = tempMessage.getDstVertexID();
        tempQueue = outgoingQueues.get(dstVertexID);
        if (tempQueue == null) {
            tempQueue = new ConcurrentLinkedQueue<IMessage>();
        }
        tempQueue.add(tempMessage);
        outgoingQueues.put(dstVertexID, tempQueue);
    }
    // Do combine operation for each of the outgoing queues.
    for (Entry<String, ConcurrentLinkedQueue<IMessage>> entry : outgoingQueues.entrySet()) {
        tempQueue = entry.getValue();
        tempMessage = (IMessage) this.combiner.combine(tempQueue.iterator());
        resultQueue.add(tempMessage);
    }
    outgoingQueue.clear();
    outgoingQueues.clear();
    return resultQueue;
}

From source file:com.mellanox.r4h.ServerPortalWorker.java

ServerPortalWorker(URI uri, int numOfMsgsToBind, int msgInSize, int msgOutSize, int numSessionsLimit,
        int spwNumber, int dynamicMsgAllocationAmount, boolean allocateResources) {
    this.uri = uri;
    this.msgInSize = msgInSize;
    this.msgOutSize = msgOutSize;
    this.numSessionsLimit = numSessionsLimit;
    this.spwNumber = spwNumber;
    this.dynamicMsgAllocationAmount = dynamicMsgAllocationAmount;

    if (allocateResources) { // dxcs in accept model or worker in forward model
        this.eqh = new R4HEventHandler(onDynamicMsgPoolAllocation, onEqhBreak);
        MsgPool msgPool = new MsgPool(numOfMsgsToBind, msgInSize, msgOutSize);
        this.ioBufferSupplier = new IOBufferSupplier(numOfMsgsToBind, msgInSize);
        this.eqh.bindMsgPool(msgPool);
        this.asyncOprQueue = new ConcurrentLinkedQueue<>();
    } else { // dxcs in forward model
        this.eqh = new R4HEventHandler(null, null);
        this.ioBufferSupplier = null;
        this.asyncOprQueue = null;
    }// www.  ja  v  a  2 s .  c  o m
}

From source file:com.thoughtworks.go.server.service.dd.reporting.ReportingDependencyFanInNode.java

private Pair<StageIdentifier, List<ReportingFaninScmMaterial>> getRevisionNthFor(int n,
        ReportingFanInGraphContext context) {
    List<ReportingFaninScmMaterial> scmMaterials = new ArrayList<>();
    PipelineTimeline pipelineTimeline = context.pipelineTimeline;
    Queue<PipelineTimelineEntry.Revision> revisionQueue = new ConcurrentLinkedQueue<>();
    DependencyMaterialConfig dependencyMaterial = (DependencyMaterialConfig) materialConfig;
    PipelineTimelineEntry entry = pipelineTimeline.instanceFor(dependencyMaterial.getPipelineName(),
            totalInstanceCount - n);/* www. j av  a  2 s. c om*/

    StageIdentifier dependentStageIdentifier = dependentStageIdentifier(context, entry,
            CaseInsensitiveString.str(dependencyMaterial.getStageName()));
    if (!StageIdentifier.NULL.equals(dependentStageIdentifier)) {
        addToRevisionQueue(entry, revisionQueue, scmMaterials, context);
    } else {
        return null;
    }
    while (!revisionQueue.isEmpty()) {
        PipelineTimelineEntry.Revision revision = revisionQueue.poll();
        DependencyMaterialRevision dmr = DependencyMaterialRevision.create(revision.revision, null);
        PipelineTimelineEntry pte = pipelineTimeline
                .getEntryFor(new CaseInsensitiveString(dmr.getPipelineName()), dmr.getPipelineCounter());
        addToRevisionQueue(pte, revisionQueue, scmMaterials, context);
    }

    return new Pair<>(dependentStageIdentifier, scmMaterials);
}

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 .j  a 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:org.glassfish.jersey.examples.sseitemstore.jaxrs.JaxrsItemStoreResourceTest.java

/**
 * Test the item addition, addition event broadcasting and item retrieval from {@link JaxrsItemStoreResource}.
 *
 * @throws Exception in case of a test failure.
 *//*w  w w  .ja v  a 2s.  c  o m*/
@Test
public void testItemsStore() throws Exception {
    final List<String> items = Collections.unmodifiableList(Arrays.asList("foo", "bar", "baz"));
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(items.size() * MAX_LISTENERS * 2); // countdown on all events
    final List<Queue<Integer>> indexQueues = new ArrayList<>(MAX_LISTENERS);
    final SseEventSource[] sources = new SseEventSource[MAX_LISTENERS];
    final AtomicInteger sizeEventsCount = new AtomicInteger(0);

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

        final Queue<Integer> indexes = new ConcurrentLinkedQueue<>();
        indexQueues.add(indexes);

        es.register(inboundEvent -> {
            try {
                if (null == inboundEvent.getName()) {
                    final String data = inboundEvent.readData();
                    LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId() + " data="
                            + data);
                    indexes.add(items.indexOf(data));
                } else if ("size".equals(inboundEvent.getName())) {
                    sizeEventsCount.incrementAndGet();
                }
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                indexes.add(-999);
            } finally {
                latch.countDown();
            }
        });
    }

    try {
        open(sources);
        items.forEach((item) -> postItem(itemsTarget, item));

        assertTrue("Waiting to receive all events has timed out.",
                latch.await((1000 + MAX_LISTENERS * RECONNECT_DEFAULT) * getAsyncTimeoutMultiplier(),
                        TimeUnit.MILLISECONDS));

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

    String postedItems = itemsTarget.request().get(String.class);
    items.forEach(
            (item) -> assertTrue("Item '" + item + "' not stored on server.", postedItems.contains(item)));

    final AtomicInteger queueId = new AtomicInteger(0);
    indexQueues.forEach((indexes) -> {
        for (int i = 0; i < items.size(); i++) {
            assertTrue("Event for '" + items.get(i) + "' not received in queue " + queueId.get(),
                    indexes.contains(i));
        }
        assertEquals("Not received the expected number of events in queue " + queueId.get(), items.size(),
                indexes.size());
        queueId.incrementAndGet();
    });

    assertEquals("Number of received 'size' events does not match.", items.size() * MAX_LISTENERS,
            sizeEventsCount.get());
}

From source file:org.apereo.portal.io.xml.PortalDataKeyFileProcessor.java

@Override
public Object apply(Resource input) {
    final InputStream fis;
    try {// ww  w  . j  a  va  2 s  .c o m
        fis = input.getInputStream();
    } catch (IOException e) {
        if (this.options == null || this.options.isFailOnError()) {
            throw new RuntimeException("Failed to create InputStream for: " + input, e);
        }

        logger.warn("Failed to create InputStream, resource will be ignored: {}", input);
        return null;
    }

    final PortalDataKey portalDataKey;
    final BufferedXMLEventReader xmlEventReader;
    try {
        xmlEventReader = new BufferedXMLEventReader(this.xmlInputFactory.createXMLEventReader(fis), -1);

        final StartElement rootElement = StaxUtils.getRootElement(xmlEventReader);
        portalDataKey = new PortalDataKey(rootElement);
    } catch (Exception e) {
        if (this.options != null && !this.options.isIngoreNonDataFiles()) {
            throw new RuntimeException("Failed to parse: " + input, e);
        }

        logger.warn("Failed to parse resource, it will be ignored: {}", input);
        return null;
    } finally {
        IOUtils.closeQuietly(fis);
    }
    xmlEventReader.reset();

    final IPortalDataType portalDataType = this.dataKeyTypes.get(portalDataKey);
    if (portalDataType == null) {
        Iterator<PortalDataKey> iter = dataKeyTypes.keySet().iterator();
        StringBuffer potentialKeys = new StringBuffer();
        potentialKeys.append("---------------- Potential Keys To Match -------------------");
        while (iter.hasNext()) {
            PortalDataKey key = iter.next();
            potentialKeys.append(key + "\n");
        }
        logger.warn("{}No IPortalDataType configured for {}, the resource will be ignored: {}", potentialKeys,
                portalDataKey, input);
        return null;
    }

    //Allow the PortalDataType to do any necessary post-processing of the input, needed as some types require extra work
    final String resourceUri = ResourceUtils.getResourceUri(input);
    final Set<PortalDataKey> processedPortalDataKeys = portalDataType.postProcessPortalDataKey(resourceUri,
            portalDataKey, xmlEventReader);
    xmlEventReader.reset();

    for (final PortalDataKey processedPortalDataKey : processedPortalDataKeys) {
        //Add the PortalDataKey and File into the map
        Queue<Resource> queue = this.dataToImport.get(processedPortalDataKey);
        if (queue == null) {
            queue = ConcurrentMapUtils.putIfAbsent(this.dataToImport, processedPortalDataKey,
                    new ConcurrentLinkedQueue<Resource>());
        }
        queue.offer(input);
        count.incrementAndGet();
    }

    return null;
}

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

/**
 * Test the item addition, addition event broadcasting and item retrieval from {@link ItemStoreResource}.
 *
 * @throws Exception in case of a test failure.
 *//*from   w  w w  . j a va2 s.co  m*/
@Test
public void testItemsStore() throws Exception {
    final List<String> items = Collections.unmodifiableList(Arrays.asList("foo", "bar", "baz"));
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(items.size() * MAX_LISTENERS * 2); // countdown on all events
    final List<Queue<Integer>> indexQueues = new ArrayList<>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];
    final AtomicInteger sizeEventsCount = new AtomicInteger(0);

    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<Integer> indexes = new ConcurrentLinkedQueue<>();
        indexQueues.add(indexes);

        es.register(inboundEvent -> {
            try {
                if (inboundEvent.getName() == null) {
                    final String data = inboundEvent.readData();
                    LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId() + " data="
                            + data);
                    indexes.add(items.indexOf(data));
                } else if ("size".equals(inboundEvent.getName())) {
                    sizeEventsCount.incrementAndGet();
                }
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                indexes.add(-999);
            } finally {
                latch.countDown();
            }
        });
    }

    try {
        open(sources);

        for (String item : items) {
            postItem(itemsTarget, item);
        }

        assertTrue("Waiting to receive all events has timed out.",
                latch.await(
                        (1000 + MAX_LISTENERS * EventSource.RECONNECT_DEFAULT) * getAsyncTimeoutMultiplier(),
                        TimeUnit.MILLISECONDS));

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

    String postedItems = itemsTarget.request().get(String.class);
    for (String item : items) {
        assertTrue("Item '" + item + "' not stored on server.", postedItems.contains(item));
    }

    int queueId = 0;
    for (Queue<Integer> indexes : indexQueues) {
        for (int i = 0; i < items.size(); i++) {
            assertTrue("Event for '" + items.get(i) + "' not received in queue " + queueId,
                    indexes.contains(i));
        }
        assertEquals("Not received the expected number of events in queue " + queueId, items.size(),
                indexes.size());
        queueId++;
    }

    assertEquals("Number of received 'size' events does not match.", items.size() * MAX_LISTENERS,
            sizeEventsCount.get());
}