线程池知识点
关于池的概念以及优点,最重要的就是减少创建,提高复用,缓解压力,提高系统性能,统一管理。
部分代码,具体的使用调用相关API即可,不再赘述,人家的博客写的挺好!
public void TestThread(){
//newFixedThreadPool方法返回的是接口ExecutorService类型(ThreadPoolExecutor是(中间其实先继承了一个抽象类,抽象类实现了下边接口)ExecutorService的实现类)
final ExecutorService executorService = Executors.newFixedThreadPool(5);
for(int i =0;i < 20;i++){
final Runnable runnable = new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName()+"线程被调用了。");
}
};
executorService.execute(runnable);
}
/* ExecutorService executorService1 = Executors.newCachedThreadPool();
ExecutorService executorService2 = Executors.newScheduledThreadPool(5);*/
}