import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.*;
@Configuration
public class ThreadPoolConfig {
public static final int THREADNUM = 200;
@Bean(name = "testPool",destroyMethod = "shutdown")
public ExecutorService testPool(){
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();
ExecutorService singleThreadPool = new ThreadPoolExecutor(10, 10,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1000000), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
return singleThreadPool;
}
}
import com.cc.spring.utils.ThreadPoolConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.concurrent.ExecutorService;
public class Test {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(ThreadPoolConfig.class);
ExecutorService executorService = (ExecutorService) context.getBean("testPool");
for(int i = 0; i < 2000 ; i++){
executorService.execute(
new Thread(){
@Override
public void run(){
System.out.println(Thread.currentThread().getName() +":"+ "123123" );
}
}
);
}
executorService.shutdown();
}
}