2011-09-01 2 views
1

以下のコードまたはValueMutationEventHandlerからfuture2get()を実行してfuture2の完了を待ってから結果を取得できないのはなぜですか?Java Future.get()が返さない

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

答えて

2

あなたは、長い時間を待って、操作をタイムアウトしないようにget(long timeout, TimeUnit unit)を使用することができます。

使用上記の代わりに、Thread.sleep(1000); Future.getはあなたがいただきましたが、そこで起こって見ることがBatchEventProcessorをチェックする必要があるかもしれませんSTHを返さない場合は、Thread.sleep(int)

を必要としません。 Future.getは何も返せません。 BatchEventProcessorにデバッグポイントを配置し、必要な予想時間内に実際に結果を返すようにします。