Question

From the code below or ValueMutationEventHandler, why can't I do future2.get(), wait for future2 to complete and then get the results ?

If I do future2.get(), it'll wait forever.

    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeoutException;

    import junit.framework.Assert;

    import org.junit.Test;

    import com.lmax.disruptor.BatchEventProcessor;
    import com.lmax.disruptor.ClaimStrategy;
    import com.lmax.disruptor.RingBuffer;
    import com.lmax.disruptor.WaitStrategy;

    int numPublisher = 1;
    int numConsumer = 1;
    int parties = numPublisher + numConsumer;
    CyclicBarrier barrier = new CyclicBarrier(parties);

    RingBuffer<ValueEvent> ringBuffer = new RingBuffer<ValueEvent>(
            ValueEvent.EVENT_FACTORY, 8192,
            ClaimStrategy.Option.MULTI_THREADED,
            WaitStrategy.Option.YIELDING
    );

    int iteration = 10;
    ValuePublisher valuePublisher = new ValuePublisher(
            barrier, ringBuffer, iteration
    );

    ExecutorService execService = Executors.newFixedThreadPool(2);
    Future future = execService.submit(valuePublisher);

    ValueMutationEventHandler eventHandler = new ValueMutationEventHandler(Operation.ADDITION);

    BatchEventProcessor<ValueEvent> eventProcessor = new BatchEventProcessor<ValueEvent>(ringBuffer, 
            ringBuffer.newDependencyBarrier(),
            eventHandler
    );

    barrier.await();
    Future future2 = execService.submit(eventProcessor);

    //////////////////////////////
    // Why do I need sleep here? Why doesn't future2.get works?
    /////////////////////////////
    Thread.sleep(1000);

    Assert.assertEquals(eventHandler.getValue(), 45L );
Was it helpful?

Solution

You can use get(long timeout, TimeUnit unit) in order not to wait a long time and timeout the the operation.

Use above instead of Thread.sleep(1000); , you don't need Thread.sleep(int)

If Future.get does not return sth you may need to check BatchEventProcessor to see whats going on in there. If it does not return anthing Future.get can't return anything as well. Place a debug point into BatchEventProcessor to make sure it really returns a result in the expected time frame you require.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top