org.jctools.queues.atomic.MpmcAtomicArrayQueue Java Examples

The following examples show how to use org.jctools.queues.atomic.MpmcAtomicArrayQueue. 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: TestQueues2.java    From util4j with Apache License 2.0 6 votes vote down vote up
protected static QueueGroupExecutor buildStageByMpMc(int min,int max,int maxPendingTask)
{
	int maxQueueCount=maxPendingTask;
	//多生产多消费者队列(线程竞争队列)
	Queue<Runnable> bossQueue=new MpmcAtomicArrayQueue<>(maxQueueCount);
	QueueFactory qf=new QueueFactory() {
		@Override
		public RunnableQueue buildQueue() {
			//多生产单消费者队列(PS:bossQueue决定了一个队列只能同时被一个线程处理)
			Queue<Runnable> queue=MpscLinkedQueue.newMpscLinkedQueue();
			return new RunnableQueueWrapper(queue);
		}
	};
	QueueGroupManager kqm=new DefaultQueueManager(qf);
	DefaultQueueGroupExecutor.Builder b=new DefaultQueueGroupExecutor.Builder();
	b.setAssistExecutor(Executors.newSingleThreadExecutor());
	return b.setMaxPoolSize(max).setCorePoolSize(min).setBossQueue(bossQueue).setQueueGroupManagerr(kqm).build();
}
 
Example #2
Source File: TestQueueGroup3.java    From util4j with Apache License 2.0 6 votes vote down vote up
protected static QueueGroupExecutor buildByMpMc(int min,int max,int maxPendingTask)
{
	int maxQueueCount=maxPendingTask;
	//多生产多消费者队列(线程竞争队列)
	Queue<Runnable> bossQueue=new MpmcAtomicArrayQueue<>(maxQueueCount);
	QueueFactory qf=new QueueFactory() {
		@Override
		public RunnableQueue buildQueue() {
			//多生产单消费者队列(PS:bossQueue决定了一个队列只能同时被一个线程处理)
			Queue<Runnable> queue=MpscLinkedQueue.newMpscLinkedQueue();
			return new RunnableQueueWrapper(queue);
		}
	};
	QueueGroupManager kqm=new DefaultQueueManager(qf);
	NioQueueGroupExecutor.Builder b=new NioQueueGroupExecutor.Builder();
	b.setAssistExecutor(Executors.newSingleThreadExecutor());
	NioQueueGroupExecutor e= b.setMaxPoolSize(max).setCorePoolSize(min).setBossQueue(bossQueue).setQueueGroupManagerr(kqm)
			.build();
	for(int i=0;i<max;i++)
	{
		e.addWorkerIfNecessary();
	}
	return e;
}
 
Example #3
Source File: MpmcPollTest.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Test
public void atomic() {
    for (int i = 0; i < 100000; i++) {
        MpmcAtomicArrayQueue<Integer> q = new MpmcAtomicArrayQueue<>(128);
        
        q.offer(1);
        
        Integer[] result = new Integer[] { 2, 2 };

        race(() -> {
            result[0] = q.poll();
        }, () -> {
            result[1] = q.poll();
        }, exec);
        
        assertTrue(Arrays.toString(result), (result[0] == null && result[1] == 1) || (result[0] == 1 && result[1] == null));
    }
}
 
Example #4
Source File: MpmcBenchmark.java    From tutorials with MIT License 6 votes vote down vote up
@Setup(Level.Trial)
public void setUp() {
    switch (implementation) {
        case PARAM_UNSAFE:
            queue = new MpmcArrayQueue<>(CAPACITY);
            break;
        case PARAM_AFU:
            queue = new MpmcAtomicArrayQueue<>(CAPACITY);
            break;
        case PARAM_JDK:
            queue = new ArrayBlockingQueue<>(CAPACITY);
            break;
        default:
            throw new UnsupportedOperationException("Unsupported implementation " + implementation);
    }
}
 
Example #5
Source File: ObjectPoolBenchmark.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Setup
public void setUp() {
    tracer = new ElasticApmTracerBuilder().buildAndStart();
    blockingQueueObjectPool = QueueBasedObjectPool.ofRecyclable(new ArrayBlockingQueue<>(256), true, () -> new Transaction(tracer));
    jctoolsQueueObjectPool = QueueBasedObjectPool.ofRecyclable(new MpmcArrayQueue<>(256), true, () -> new Transaction(tracer));
    jctoolsAtomicQueueObjectPool = QueueBasedObjectPool.ofRecyclable(new MpmcAtomicArrayQueue<>(256), true, () -> new Transaction(tracer));
    agronaQueueObjectPool = QueueBasedObjectPool.ofRecyclable(new ManyToManyConcurrentArrayQueue<>(256), true, () -> new Transaction(tracer));
    threadLocalObjectPool = new ThreadLocalObjectPool<>(64, true, () -> new Transaction(tracer));
}
 
Example #6
Source File: QueueBasedObjectPoolTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void preAllocationShouldCreateObjectsInPool() {
    // we have to use a power of two as capacity, otherwise actual capacity will differ
    int capacity = 8;

    ObjectPool<TestRecyclable> pool = QueueBasedObjectPool.ofRecyclable(new MpmcAtomicArrayQueue<>(capacity), true, TestRecyclable::new);

    assertThat(pool.getGarbageCreated()).isEqualTo(0);
    assertThat(pool.getObjectsInPool()).isEqualTo(capacity);
}
 
Example #7
Source File: QueueBasedObjectPoolTest.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Override
protected QueueBasedObjectPool<TestRecyclable> createObjectPool(int maxSize) {
    return QueueBasedObjectPool.ofRecyclable(new MpmcAtomicArrayQueue<>(maxSize), false, TestRecyclable::new);
}
 
Example #8
Source File: BookkeeperObjectPoolTest.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Override
protected BookkeeperObjectPool<TestRecyclable> createObjectPool(int maxSize) {
    ObjectPool<TestRecyclable> queuePool = QueueBasedObjectPool.ofRecyclable(new MpmcAtomicArrayQueue<>(maxSize), false, TestRecyclable::new);
    return new BookkeeperObjectPool<>(queuePool);
}