ExecutorService_NewFixedThreadPool

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 可重用固定线程集合
 * @author zhoucheng
 *
 */
public class FixedThreadPool_Demo {
    public static void main(String[] agrs){
        ExecutorService threadPool = Executors.newFixedThreadPool(3);//创建可以容纳3个线程的线程池
        for(int i=1;i<5;i++){
            final int taskId = i;
            System.out.println(i);
            threadPool.execute(new Runnable(){
                @Override
                public void run() {
                    for(int j=1;j<5;j++){
                         try {  
                                Thread.sleep(1000*2);// 为了测试出效果,让每次任务执行都需要一定时间  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                         System.out.println("第" + taskId + "次任务的第" + j + "次执行"+"-"+Thread.currentThread().getName());
                    }
                }
            });
        }
        System.out.println("任务已提交。。。");
        //shutdown方法调用之后,马上拒绝接收新的任务,但是之前已经提交的任务会继续执行。
        threadPool.shutdown();// 
        System.out.println("线程已关闭。。。");
    }
}

可以看到,任务全部提交完之后,并发执行,首先执行完前面3个线程,等到空闲下来的线程,则开始执行第4个线程。在FixedThreadPool中,有一个固定大小的池,如果当前需要执行的任务超过了池大小,那么多于的任务等待状态,直到有空闲下来的线程执行任务,而当执行的任务小于池大小,空闲的线程也不会去销毁

1
2
3
4
任务已提交。。。
线程已关闭。。。
第2次任务的第1次执行-pool-1-thread-2
第1次任务的第1次执行-pool-1-thread-1
第3次任务的第1次执行-pool-1-thread-3
第2次任务的第2次执行-pool-1-thread-2
第1次任务的第2次执行-pool-1-thread-1
第3次任务的第2次执行-pool-1-thread-3
第2次任务的第3次执行-pool-1-thread-2
第3次任务的第3次执行-pool-1-thread-3
第1次任务的第3次执行-pool-1-thread-1
第2次任务的第4次执行-pool-1-thread-2
第1次任务的第4次执行-pool-1-thread-1
第3次任务的第4次执行-pool-1-thread-3
第4次任务的第1次执行-pool-1-thread-2
第4次任务的第2次执行-pool-1-thread-2
第4次任务的第3次执行-pool-1-thread-2
第4次任务的第4次执行-pool-1-thread-2

注意:
newFixedThreadPool线程池的线程是不会释放的,即使它是Idle。这就会产生性能问题,比如如果线程池的大小为200,当全部使用完毕后,所有的线程会继续留在池中,相应的内存和线程切换(while(true)+sleep循环)都会增加。如果要避免这个问题,就必须直接使用ThreadPoolExecutor()来构造。可以像通用的线程池一样设置“最大线程数”、“最小线程数”和“空闲线程keepAlive的时间”。这个就是线程池基本用法。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容