publicvoidonData(ByteBuffer bb){ // Grab the next sequence long sequence = ringBuffer.next(); try { // Get the entry in the Disruptor for the sequence LongEvent event = ringBuffer.get(sequence); // Fill with data event.set(bb.getLong(0)); } finally { ringBuffer.publish(sequence); } } }
publicclassLongEventMain{ publicstaticvoidmain(String[] args)throws Exception { // The factory for the event LongEventFactory factory = new LongEventFactory();
// Specify the size of the ring buffer, must be power of 2. int bufferSize = 1024;
// Construct the Disruptor Disruptor<LongEvent> disruptor = new Disruptor<>(factory, bufferSize, DaemonThreadFactory.INSTANCE);
// Connect the handler disruptor.handleEventsWith(new LongEventHandler());
// Start the Disruptor, starts all threads running disruptor.start();
// Get the ring buffer from the Disruptor to be used for publishing. RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8); for (long l = 0; true; l++) { bb.putLong(0, l); producer.onData(bb); Thread.sleep(1000); } } }
publicclassLongEventMain{ publicstaticvoidmain(String[] args)throws Exception { // Specify the size of the ring buffer, must be power of 2. int bufferSize = 1024;
// Construct the Disruptor Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE);
Run 0, Disruptor=26,553,372 ops/sec Run 1, Disruptor=28,727,377 ops/sec Run 2, Disruptor=29,806,259 ops/sec Run 3, Disruptor=29,717,682 ops/sec Run 4, Disruptor=28,818,443 ops/sec Run 5, Disruptor=29,103,608 ops/sec Run 6, Disruptor=29,239,766 ops/sec
3.1.2 Single Producer
1 2 3 4 5 6 7
Run 0, Disruptor=89,365,504 ops/sec Run 1, Disruptor=77,579,519 ops/sec Run 2, Disruptor=78,678,206 ops/sec Run 3, Disruptor=80,840,743 ops/sec Run 4, Disruptor=81,037,277 ops/sec Run 5, Disruptor=81,168,831 ops/sec Run 6, Disruptor=81,699,346 ops/sec
publicclassClearingEventHandler<T> implementsEventHandler<ObjectEvent<T>> { publicvoidonEvent(ObjectEvent<T> event, long sequence, boolean endOfBatch){ // Failing to call clear here will result in the // object associated with the event to live until // it is overwritten once the ring buffer has wrapped // around to the beginning. event.clear(); } }
LMAX Group is a global, high-growth, award-winning financial technology company. We operate one global marketplace for FX – enabling transparency, open access and a level playing field for all market participants.