当有 N 个生产者和 1 个消费者时,破坏者队列是否提供卓越的性能?我使用 Disruptor Queues 编写了一个包含多个生产者和单个消费者的程序。我发现结果与阻塞 arraybounded 队列并驾齐驱。后者表现更好。我在这里做错了吗?
public void asynchronous_execution( )
{
// Set up the executor and disruptor
ExecutorService exec = Executors.newCachedThreadPool();
Disruptor<valueEvent> disruptor =
new Disruptor<valueEvent>( valueEvent.EVENT_FACTORY,
globalVariables.RING_SIZE,
exec );
// Handler of Events
final EventHandler<valueEvent> handler = new EventHandler<valueEvent>()
{
public void onEvent(final valueEvent event, final long sequence,
final boolean endOfBatch) throws Exception
{ .... }
};
// Building Dependency Graph
disruptor.handleEventsWith(handler);
// Starting the disruptor
final RingBuffer<valueEvent> ringBuffer = disruptor.start();
// Producer Thread
final long[] runtime = new long [globalVariables.NUMBER_OF_THREADS];
final class ProducerThread extends Thread {
...
public void run()
{
..
long sequence = ringBuffer.next();
valueEvent event = ringBuffer.get(sequence);
event.setValue(globalVariables.WRITE_CODE);
// make the event available to EventProcessors
ringBuffer.publish(sequence);
...
}
...
};
ProducerThread[] threads = new ProducerThread[globalVariables.NUMBER_OF_THREADS];
for (int i = 0; i < globalVariables.NUMBER_OF_THREADS; i++) {
threads[i] = new ProducerThread( i );
threads[i].start();
}
....
}