Search in sources :

Example 1 with SpscArrayQueue

use of org.jctools.queues.SpscArrayQueue in project apex-core by apache.

the class AbstractReservoirTest method testSpscBlockingQueuePerformance.

@Test
@Ignore
public void testSpscBlockingQueuePerformance() {
    final SpscArrayQueue<Object> spscArrayQueue = new SpscArrayQueue<>(CAPACITY);
    final ReentrantLock lock = new ReentrantLock();
    final Condition notFull = lock.newCondition();
    final long start = System.currentTimeMillis();
    final Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                int sleepMillis;
                for (int i = 0; i < COUNT; i++) {
                    sleepMillis = 0;
                    while (spscArrayQueue.poll() == null) {
                        sleep(sleepMillis);
                        sleepMillis = Math.min(10, sleepMillis + 1);
                    }
                    lock.lock();
                    notFull.signal();
                    lock.unlock();
                }
            } catch (InterruptedException e) {
                logger.error("Interrupted", e);
                throw new RuntimeException(e);
            }
        }
    });
    t.start();
    final Object o = new Byte[128];
    try {
        for (int i = 0; i < COUNT; i++) {
            if (!spscArrayQueue.offer(o)) {
                lock.lockInterruptibly();
                while (!spscArrayQueue.offer(o)) {
                    notFull.await();
                }
                lock.unlock();
            }
        }
        t.join();
    } catch (InterruptedException e) {
        logger.error("Interrupted", e);
        throw new RuntimeException(e);
    }
    logger.debug("Time {}", System.currentTimeMillis() - start);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) SpscArrayQueue(org.jctools.queues.SpscArrayQueue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Condition (java.util.concurrent.locks.Condition)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 SpscArrayQueue (org.jctools.queues.SpscArrayQueue)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1