一、初始化

1,直接调用

import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
//线程池所使用的缓冲队列
poolTaskExecutor.setQueueCapacity(10);
//线程池维护线程的最少数量
poolTaskExecutor.setCorePoolSize(2);
//线程池维护线程的最大数量
poolTaskExecutor.setMaxPoolSize(3);
//线程池维护线程所允许的空闲时间
poolTaskExecutor.setKeepAliveSeconds(300); poolTaskExecutor.setRejectedExecutionHandler(new CallerRunsPolicy());
poolTaskExecutor.initialize(); for (int i = 0; i < 25; i++) {
MessagePrinterTask m=new MessagePrinterTask("Message" + i);
poolTaskExecutor.execute(m);
System.out.println("add Thread:"+i);
} }
}

结果是

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJoAAAI3CAIAAADsgNVZAAARUklEQVR4nO2dTbqqyrJFbRcNoj1Ub2034n7045btxOsBr6Bg/kQm6GItIyZjfKdwFhuT0GkixAC9/fv37/9W/v3795///u8///3fAicyj8tt3P5Ybrdl/q1N3Yjz97kvw225rf9N99/bEnFKQZxSEKcUP4vzPg23sfxcNxfuMI9vP+QdPikpJH8d530abjnDdPcS56u44TePV36Rb83OIj8Pcc7jbV0p7GzeiXMeX9Po9fyyKbYuNhc2seJcN7ZNjXm8jdM2rLGhbYg36uxUtG748biIeR6ena83rPkufvetXcd5q0eax1e26wPSByYJHK2zybbKY6OTMXQA9uJM3/bb092eaPoa1At7tHe2ycONXXD52ZukfaTONo9VtreA9U4JQD/OYgfkIE77ZT5cZ6e4eUzeHVE/PLtx3qdhfYKvveFrYfIRYy7s8Wmc6S5043idOyWlcz1imocPhYZp2l7bbYc3znN/YZuP48z3t9W+dq/Og2dREXe0y0JXSAzilII4pSBOKYhTCuKUAkEmBYLMqC5u8giytIZhuseeyAiyAuk4X6gLsvrxEUGQFSjHe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