Java Future.get() 不返回

发布于 2024-12-02 10:47:18 字数 2135 浏览 0 评论 0原文

从下面的代码或 ValueMutationEventHandler,为什么我不能执行 future2.get(),等待 future2 完成然后获取结果?

如果我执行 future2.get(),它将永远等待。

    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 );

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 );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-12-09 10:47:18

您可以使用 get(long timeout, TimeUnit unit) 以避免等待很长时间并使操作超时。

使用上面而不是 Thread.sleep(1000); ,你不需要 Thread.sleep(int)

如果 Future.get 没有返回某事,你可能需要检查BatchEventProcessor 查看那里发生了什么。如果它不返回任何内容,Future.get 也无法返回任何内容。将调试点放入 BatchEventProcessor 中,以确保它确实在您需要的预期时间范围内返回结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文