简体   繁体   中英

AssertionError when using a TaskExecutor

I am writing a program that executes many Threads at the same time (in parallel) , I am using a 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");
}   

When I tested my code an AssertionError exeption was raised . I'm using the Spring Framework to manage the execution .

here is the log screen :

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)

Any one have any ideas please :) Thank you

I found the solution , I have to initialize the threadPoolTaskExecutor so when we use assertNotNull(threadPoolTaskExecutor); the object will be initialized and we can execute our threads .

Here is the initialize method :

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

and here is the executorService definition :

private ThreadPoolExecutor executorService;

Thank You Andrew and Pace and Ingo for your help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM