spring-线程池(3)

一、初始化

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

结果是

 

posted @ 2015-01-13 14:14  crazyYong  阅读(438)  评论(0编辑  收藏  举报