2

多くのスレッドを同時に (並行して) 実行するプログラムを作成しています。 TaskExecutor を使用しています。

@Autowired TaskExecutor threadPoolTaskExecutor;
@Test
public  void testSpringTaskExecutor() 
                         throws InterruptedException  {
    assertNotNull(threadPoolTaskExecutor);
    for (int k = 0; k < 5; k++) {
        Runnable myThread = 
                        new Workflow(new AtomicInteger(k));
        threadPoolTaskExecutor.execute(myThread);
    }
    Thread.sleep(500);
    logger.info("Finished all threads");
}   

コードをテストしたところ、AssertionError 例外が発生しました。Spring Framework を使用して実行を管理しています。

ここにログ画面があります:

Exception in thread "main" java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:92)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.junit.Assert.assertNotNull(Assert.java:526)

どなたかアイデアをお持ちください :) ありがとう

4

1 に答える 1

1

私は解決策を見つけました.threadPoolTask​​Executorを初期化する必要があります. オブジェクトが初期化され、スレッドを実行できます。

初期化メソッドは次のとおりです。

 public void initialize() {
                     logger.info("Creating ThreadPoolExecutor");
                     BlockingQueue   queue = createQueue(this.queueCapacity);
                    executorService = new ThreadPoolExecutor  (
                             this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
                             queue, this.threadFactory, this.rejectedExecutionHandler);
                 }

executorService の定義は次のとおりです。

private ThreadPoolExecutor executorService;

Andrew と Pace と Ingo に感謝します:)

于 2013-03-21T13:05:38.627 に答える