org.agrona.concurrent.OneToOneConcurrentArrayQueue Java Examples

The following examples show how to use org.agrona.concurrent.OneToOneConcurrentArrayQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ManyToOneRingBufferBenchmark.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new int[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(BUFFER_LENGTH)));

    consumerThread = new Thread(new Subscriber(ringBuffer, running, responseQueues));

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #2
Source File: DirectBufferProxy.java    From lmdbjava with Apache License 2.0 5 votes vote down vote up
@Override
protected DirectBuffer allocate() {
  final OneToOneConcurrentArrayQueue<DirectBuffer> q = BUFFERS.get();
  final DirectBuffer buffer = q.poll();

  if (buffer != null && buffer.capacity() >= 0) {
    return buffer;
  } else {
    final ByteBuffer bb = allocateDirect(0);
    return new UnsafeBuffer(bb);
  }
}
 
Example #3
Source File: AeronExclusiveIpcBenchmark.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new int[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    ctx = new MediaDriver.Context()
        .termBufferSparseFile(false)
        .threadingMode(ThreadingMode.SHARED)
        .sharedIdleStrategy(new BusySpinIdleStrategy())
        .dirDeleteOnStart(true);

    mediaDriver = MediaDriver.launch(ctx);
    aeron = Aeron.connect(new Aeron.Context().preTouchMappedMemory(true));
    publication = aeron.addExclusivePublication(CommonContext.IPC_CHANNEL, STREAM_ID);
    subscription = aeron.addSubscription(CommonContext.IPC_CHANNEL, STREAM_ID);

    consumerThread = new Thread(new Subscriber(subscription, running, responseQueues));

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #4
Source File: DisruptorBenchmark.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
@Setup
public synchronized void setup() throws InterruptedException
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new int[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    handler = new Handler(responseQueues);

    disruptor = new Disruptor<>(
        Message::new,
        Configuration.SEND_QUEUE_CAPACITY,
        (ThreadFactory)Thread::new,
        ProducerType.MULTI,
        new BusySpinWaitStrategy());

    disruptor.handleEventsWith(handler);
    disruptor.start();

    handler.waitForStart();
}
 
Example #5
Source File: AeronIpcBenchmark.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new int[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    ctx = new MediaDriver.Context()
        .termBufferSparseFile(false)
        .threadingMode(ThreadingMode.SHARED)
        .sharedIdleStrategy(new BusySpinIdleStrategy())
        .dirDeleteOnStart(true);

    mediaDriver = MediaDriver.launch(ctx);
    aeron = Aeron.connect(new Aeron.Context().preTouchMappedMemory(true));
    publication = aeron.addPublication(CommonContext.IPC_CHANNEL, STREAM_ID);
    subscription = aeron.addSubscription(CommonContext.IPC_CHANNEL, STREAM_ID);

    consumerThread = new Thread(new Subscriber(subscription, running, responseQueues));

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #6
Source File: DirectBufferProxy.java    From lmdbjava with Apache License 2.0 4 votes vote down vote up
@Override
protected void deallocate(final DirectBuffer buff) {
  final OneToOneConcurrentArrayQueue<DirectBuffer> q = BUFFERS.get();
  q.offer(buff);
}
 
Example #7
Source File: LinkedBlockingQueueBenchmark.java    From benchmarks with Apache License 2.0 4 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new Integer[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    consumerThread = new Thread(
        () ->
        {
            while (true)
            {
                final Integer value = sendQueue.poll();
                if (null == value)
                {
                    if (!running.get())
                    {
                        break;
                    }

                    ThreadHints.onSpinWait();
                }
                else
                {
                    final int intValue = value;
                    if (intValue >= 0)
                    {
                        final Queue<Integer> responseQueue = responseQueues[value];
                        while (!responseQueue.offer(value))
                        {
                            ThreadHints.onSpinWait();
                        }
                    }
                }
            }
        }
    );

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #8
Source File: ManyToOneConcurrentArrayQueueBenchmark.java    From benchmarks with Apache License 2.0 4 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new Integer[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    consumerThread = new Thread(
        () ->
        {
            while (true)
            {
                final Integer value = sendQueue.poll();
                if (null == value)
                {
                    if (!running.get())
                    {
                        break;
                    }

                    ThreadHints.onSpinWait();
                }
                else
                {
                    final int intValue = value;
                    if (intValue >= 0)
                    {
                        final Queue<Integer> responseQueue = responseQueues[value];
                        while (!responseQueue.offer(value))
                        {
                            ThreadHints.onSpinWait();
                        }
                    }
                }
            }
        }
    );

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #9
Source File: ManyToOneConcurrentLinkedQueueBenchmark.java    From benchmarks with Apache License 2.0 4 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new Integer[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    consumerThread = new Thread(
        () ->
        {
            while (true)
            {
                final Integer value = sendQueue.poll();
                if (null == value)
                {
                    if (!running.get())
                    {
                        break;
                    }

                    ThreadHints.onSpinWait();
                }
                else
                {
                    final int intValue = value;
                    if (intValue >= 0)
                    {
                        final Queue<Integer> responseQueue = responseQueues[value];
                        while (!responseQueue.offer(value))
                        {
                            ThreadHints.onSpinWait();
                        }
                    }
                }
            }
        }
    );

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #10
Source File: ConcurrentLinkedQueueBenchmark.java    From benchmarks with Apache License 2.0 4 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new Integer[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    consumerThread = new Thread(
        () ->
        {
            while (true)
            {
                final Integer value = sendQueue.poll();
                if (null == value)
                {
                    if (!running.get())
                    {
                        break;
                    }

                    ThreadHints.onSpinWait();
                }
                else
                {
                    final int intValue = value;
                    if (intValue >= 0)
                    {
                        final Queue<Integer> responseQueue = responseQueues[value];
                        while (!responseQueue.offer(value))
                        {
                            ThreadHints.onSpinWait();
                        }
                    }
                }
            }
        }
    );

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #11
Source File: A1BaselineBenchmark.java    From benchmarks with Apache License 2.0 4 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new Integer[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    consumerThread = new Thread(
        () ->
        {
            while (true)
            {
                final Integer value = sendQueue.poll();
                if (null == value)
                {
                    if (!running.get())
                    {
                        break;
                    }

                    ThreadHints.onSpinWait();
                }
                else
                {
                    final int intValue = value;
                    if (intValue >= 0)
                    {
                        final Queue<Integer> responseQueue = responseQueues[value];
                        while (!responseQueue.offer(value))
                        {
                            ThreadHints.onSpinWait();
                        }
                    }
                }
            }
        }
    );

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #12
Source File: ArrayBlockingQueueBenchmark.java    From benchmarks with Apache License 2.0 4 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new Integer[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    consumerThread = new Thread(
        () ->
        {
            while (true)
            {
                final Integer value = sendQueue.poll();
                if (null == value)
                {
                    if (!running.get())
                    {
                        break;
                    }

                    ThreadHints.onSpinWait();
                }
                else
                {
                    final int intValue = value;
                    if (intValue >= 0)
                    {
                        final Queue<Integer> responseQueue = responseQueues[value];
                        while (!responseQueue.offer(value))
                        {
                            ThreadHints.onSpinWait();
                        }
                    }
                }
            }
        }
    );

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #13
Source File: QueueFactories.java    From cyclops with Apache License 2.0 3 votes vote down vote up
/**
 * Creates an async.Queue backed by an Agrona OneToOneConcurrentArrayQueue bounded by specified queueSize
 *  Wait strategy used is NoWaitRetry by default for both Consumers and Producers
 *  (both Consumers and Producers will repeatedly retry until successful). Use
 *  withConsumerWaitStrategy  &amp; withProducerWaitStrategy methods on the returned queue to change the
 *  wait strategy
 * <pre>
 * {@code
 *    queue.withConsumerWaitStrategy(new DirectWaitStrategy())
 *         .withProducerWaitStrategy(new YieldWait());
 * }</pre>
 *
 * @param queueSize
 * @return
 */
public static <T> QueueFactory<T> singleWriterboundedNonBlockingQueue(final int queueSize) {
    return () -> new Queue<T>(
                              new OneToOneConcurrentArrayQueue<>(
                                                                 queueSize),
                              new NoWaitRetry<>(), new NoWaitRetry<>());

}
 
Example #14
Source File: QueueFactories.java    From cyclops with Apache License 2.0 3 votes vote down vote up
/**
 * Generate QueueFactory for bounded non blocking queues. Max queue size is determined by the input parameter.
 * The provided WaitStrategy is used to determine behaviour of both producers and consumers when the Queue is full (producer)
 * or zero (consumer). {@see WaitStrategy#spinWait() , @see WaitStrategy#exponentialBackOff() , @see WaitStrategy#noWaitRetry() }
 *
 * @param queueSize Max Queue size
 * @param strategy Strategy to be employed by producers when Queue is full, or consumers when Queue is zero
 * @return bounded wait free Queue Factory backed by an Agrona OneToOneConcurrentArrayQueue
 */
public static <T> QueueFactory<T> singleWriterboundedNonBlockingQueue(final int queueSize, final WaitStrategy<T> strategy) {
    return () -> new Queue<T>(
                              new OneToOneConcurrentArrayQueue<>(
                                                                 queueSize),
                              strategy, strategy);

}
 
Example #15
Source File: Connectable.java    From cyclops with Apache License 2.0 2 votes vote down vote up
/**
 * Connect to this Connectable (Stream that is already emitting data)
 *
 * <pre>
 * {@code
 *
 * ReactiveSeq.range(0,Integer.MAX_VALUE)
                .limit(100)
                .peek(v->value=v)
                .peek(v->latch.countDown())
                .peek(System.out::println)
                .hotStream(exec)
                .connect()
                .limit(100)
                .futureOperations(ForkJoinPool.commonPool())
                .forEach(System.out::println)
 *
 * }
 * </pre>
 *
 *
 * @return Stream connected to the Connectable emitting data
 */
public default ReactiveSeq<T> connect() {
    return connect(new OneToOneConcurrentArrayQueue<T>(
                                                       256));
}