references:
- http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch23s04.html
- http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html
@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:
Post a Comment