7.5.13

A Simple Sample Program for Spring's TaskExecutor

references:




@Test
public void test() throws Exception {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);

executor.initialize();

Runnable task = new Runnable() {

@Override
public void run() {
System.out.printf("Start thread : %d\n", Thread.currentThread().getId());
try {
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
} catch (InterruptedException ex) {
//..
}

System.out.printf("End thread id = %d\n", Thread.currentThread().getId());
}
};

for(int k=0; k<100; ++k)
{
try {
executor.execute(task);
} catch (TaskRejectedException ex) {
System.out.println("Thread rejected. Wait...");
TimeUnit.SECONDS.sleep(1);
}
}
}

No comments: