Search in sources :

Example 1 with Disruptor

use of com.lmax.disruptor.dsl.Disruptor in project disruptor by LMAX-Exchange.

the class DisruptorStressTest method shouldHandleLotsOfThreads.

@Test
public void shouldHandleLotsOfThreads() throws Exception {
    Disruptor<TestEvent> disruptor = new Disruptor<TestEvent>(TestEvent.FACTORY, 1 << 16, DaemonThreadFactory.INSTANCE, ProducerType.MULTI, new BusySpinWaitStrategy());
    RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer();
    disruptor.setDefaultExceptionHandler(new FatalExceptionHandler());
    int threads = max(1, Runtime.getRuntime().availableProcessors() / 2);
    int iterations = 200000;
    int publisherCount = threads;
    int handlerCount = threads;
    CyclicBarrier barrier = new CyclicBarrier(publisherCount);
    CountDownLatch latch = new CountDownLatch(publisherCount);
    TestEventHandler[] handlers = initialise(disruptor, new TestEventHandler[handlerCount]);
    Publisher[] publishers = initialise(new Publisher[publisherCount], ringBuffer, iterations, barrier, latch);
    disruptor.start();
    for (Publisher publisher : publishers) {
        executor.execute(publisher);
    }
    latch.await();
    while (ringBuffer.getCursor() < (iterations - 1)) {
        LockSupport.parkNanos(1);
    }
    disruptor.shutdown();
    for (Publisher publisher : publishers) {
        assertThat(publisher.failed, is(false));
    }
    for (TestEventHandler handler : handlers) {
        assertThat(handler.messagesSeen, is(not(0)));
        assertThat(handler.failureCount, is(0));
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Disruptor(com.lmax.disruptor.dsl.Disruptor) Test(org.junit.Test)

Example 2 with Disruptor

use of com.lmax.disruptor.dsl.Disruptor in project disruptor by LMAX-Exchange.

the class WorkerStressTest method shouldHandleLotsOfThreads.

@Test
public void shouldHandleLotsOfThreads() throws Exception {
    Disruptor<TestEvent> disruptor = new Disruptor<TestEvent>(TestEvent.FACTORY, 1 << 16, DaemonThreadFactory.INSTANCE, ProducerType.MULTI, new SleepingWaitStrategy());
    RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer();
    disruptor.setDefaultExceptionHandler(new FatalExceptionHandler());
    int threads = max(1, Runtime.getRuntime().availableProcessors() / 2);
    int iterations = 200000;
    int publisherCount = threads;
    int handlerCount = threads;
    CyclicBarrier barrier = new CyclicBarrier(publisherCount);
    CountDownLatch latch = new CountDownLatch(publisherCount);
    TestWorkHandler[] handlers = initialise(new TestWorkHandler[handlerCount]);
    Publisher[] publishers = initialise(new Publisher[publisherCount], ringBuffer, iterations, barrier, latch);
    disruptor.handleEventsWithWorkerPool(handlers);
    disruptor.start();
    for (Publisher publisher : publishers) {
        executor.execute(publisher);
    }
    latch.await();
    while (ringBuffer.getCursor() < (iterations - 1)) {
        LockSupport.parkNanos(1);
    }
    disruptor.shutdown();
    for (Publisher publisher : publishers) {
        assertThat(publisher.failed, is(false));
    }
    for (TestWorkHandler handler : handlers) {
        assertThat(handler.seen, is(not(0)));
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Disruptor(com.lmax.disruptor.dsl.Disruptor) Test(org.junit.Test)

Example 3 with Disruptor

use of com.lmax.disruptor.dsl.Disruptor in project disruptor by LMAX-Exchange.

the class MultiProducerWithTranslator method main.

public static void main(String[] args) throws InterruptedException {
    Disruptor<ObjectBox> disruptor = new Disruptor<ObjectBox>(ObjectBox.FACTORY, RING_SIZE, DaemonThreadFactory.INSTANCE, ProducerType.MULTI, new BlockingWaitStrategy());
    disruptor.handleEventsWith(new Consumer()).then(new Consumer());
    final RingBuffer<ObjectBox> ringBuffer = disruptor.getRingBuffer();
    Publisher p = new Publisher();
    IMessage message = new IMessage();
    ITransportable transportable = new ITransportable();
    String streamName = "com.lmax.wibble";
    System.out.println("publishing " + RING_SIZE + " messages");
    for (int i = 0; i < RING_SIZE; i++) {
        ringBuffer.publishEvent(p, message, transportable, streamName);
        Thread.sleep(10);
    }
    System.out.println("start disruptor");
    disruptor.start();
    System.out.println("continue publishing");
    while (true) {
        ringBuffer.publishEvent(p, message, transportable, streamName);
        Thread.sleep(10);
    }
}
Also used : BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) Disruptor(com.lmax.disruptor.dsl.Disruptor)

Example 4 with Disruptor

use of com.lmax.disruptor.dsl.Disruptor in project disruptor by LMAX-Exchange.

the class WaitForShutdown method main.

public static void main(String[] args) throws TimeoutException, InterruptedException {
    Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(LongEvent.FACTORY, 16, DaemonThreadFactory.INSTANCE);
    CountDownLatch shutdownLatch = new CountDownLatch(2);
    disruptor.handleEventsWith(new Handler(shutdownLatch)).then(new Handler(shutdownLatch));
    disruptor.start();
    long next = disruptor.getRingBuffer().next();
    disruptor.getRingBuffer().get(next).set(next);
    disruptor.getRingBuffer().publish(next);
    disruptor.shutdown(10, TimeUnit.SECONDS);
    shutdownLatch.await();
    System.out.println(value);
}
Also used : LongEvent(com.lmax.disruptor.support.LongEvent) EventHandler(com.lmax.disruptor.EventHandler) Disruptor(com.lmax.disruptor.dsl.Disruptor) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with Disruptor

use of com.lmax.disruptor.dsl.Disruptor in project disruptor by LMAX-Exchange.

the class DynamiclyAddHandler method main.

public static void main(String[] args) throws InterruptedException {
    ExecutorService executor = Executors.newCachedThreadPool(DaemonThreadFactory.INSTANCE);
    // Build a disruptor and start it.
    Disruptor<StubEvent> disruptor = new Disruptor<StubEvent>(StubEvent.EVENT_FACTORY, 1024, DaemonThreadFactory.INSTANCE);
    RingBuffer<StubEvent> ringBuffer = disruptor.start();
    // Construct 2 batch event processors.
    DynamicHandler handler1 = new DynamicHandler();
    BatchEventProcessor<StubEvent> processor1 = new BatchEventProcessor<StubEvent>(ringBuffer, ringBuffer.newBarrier(), handler1);
    DynamicHandler handler2 = new DynamicHandler();
    BatchEventProcessor<StubEvent> processor2 = new BatchEventProcessor<StubEvent>(ringBuffer, ringBuffer.newBarrier(processor1.getSequence()), handler2);
    // Dynamically add both sequences to the ring buffer
    ringBuffer.addGatingSequences(processor1.getSequence(), processor2.getSequence());
    // Start the new batch processors.
    executor.execute(processor1);
    executor.execute(processor2);
    // Remove a processor.
    // Stop the processor
    processor2.halt();
    // Wait for shutdown the complete
    handler2.awaitShutdown();
    // Remove the gating sequence from the ring buffer
    ringBuffer.removeGatingSequence(processor2.getSequence());
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent) ExecutorService(java.util.concurrent.ExecutorService) Disruptor(com.lmax.disruptor.dsl.Disruptor)

Aggregations

Disruptor (com.lmax.disruptor.dsl.Disruptor)7 CountDownLatch (java.util.concurrent.CountDownLatch)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 ExecutorService (java.util.concurrent.ExecutorService)2 Test (org.junit.Test)2 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1 EventHandler (com.lmax.disruptor.EventHandler)1 LongEvent (com.lmax.disruptor.support.LongEvent)1 StubEvent (com.lmax.disruptor.support.StubEvent)1 Executor (java.util.concurrent.Executor)1