Java线程池(1)--cachedTheadPool

来源:互联网 发布:移动端手风琴效果 js 编辑:程序博客网 时间:2024/06/18 06:59

Java通过Executors提供四种线程池,分别为:newCachedThreadPool、newFixedThreadPool、newScheduledThreadPool 、newSingleThreadExecutor;
今天了解下第一种:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

在写之前我们先看下该线程池的参数:

public static ExecutorService newCachedThreadPool() {    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,                                  60L, TimeUnit.SECONDS,                                  new SynchronousQueue<Runnable>());}

核心线程数为0,最大线程数为Integer的最大值;意味着,任务队列中的任务会最大限度的复用已经空闲的线程,没有空闲线程时最大限度允许新建线程;我们看下线程的复用情况:
我们先写一个代码看下效果:

 public static void main(String[] args) {        testCachedThreadPool();    }    private static void testCachedThreadPool() {        ExecutorService executorService = Executors.newCachedThreadPool();        for (int i = 0; i < 100; i++) {            final int index = i;            executorService.execute(new Runnable() {                @Override                public void run() {                    System.out.println("index is " + index);                    System.out.println(Thread.currentThread().getName());                }            });        }    }

执行结果如下:实际线程的数量取决于for循环塞线程的速度和线程的执行时间,for循环塞下一个线程的时候,如果有空闲线程,则会复用之前的,从下面的结果就可以看出来;
index is 0
pool-1-thread-1
index is 1
pool-1-thread-2
index is 2
pool-1-thread-3
index is 3
pool-1-thread-4
index is 4
pool-1-thread-2
index is 7
pool-1-thread-1
index is 9
pool-1-thread-2
index is 5
pool-1-thread-4
index is 12
pool-1-thread-1
index is 14
pool-1-thread-1
index is 8
pool-1-thread-5
index is 6
pool-1-thread-3
index is 17
pool-1-thread-1
index is 13
pool-1-thread-7
index is 18
pool-1-thread-9
index is 15
pool-1-thread-4
index is 10
index is 21
pool-1-thread-10
index is 26
pool-1-thread-7
index is 11

我们做个小实验,给线程加个等待,减慢线程执行时间

private static void testCachedThreadPool() {        ExecutorService executorService = Executors.newCachedThreadPool();        for (int i = 0; i < 100; i++) {            final int index = i;            executorService.execute(new Runnable() {                @Override                public void run() {                    System.out.println("index is " + index);                    System.out.println(Thread.currentThread().getName());                    try {                        Thread.sleep(2000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            });        }    }

可以预期一下,结果肯定是线程基本上都无法复用了,因为塞线程的速度太快,单个线程执行时间太慢,看下结果:基本上一个任务一个线程
index is 9
pool-1-thread-10
index is 10
pool-1-thread-11
index is 12
pool-1-thread-13
index is 13
pool-1-thread-14
index is 11
pool-1-thread-12
index is 14
pool-1-thread-15
index is 17
pool-1-thread-18
index is 15
pool-1-thread-16
index is 18
pool-1-thread-19
index is 20
pool-1-thread-21
index is 22
pool-1-thread-23
index is 25
pool-1-thread-26
index is 16
pool-1-thread-17
index is 19
pool-1-thread-20
index is 24
pool-1-thread-25
index is 27
pool-1-thread-28
index is 21
pool-1-thread-22
index is 30
pool-1-thread-31
index is 28
pool-1-thread-29
index is 26
pool-1-thread-27
index is 29
pool-1-thread-30
index is 33
pool-1-thread-34
index is 36
pool-1-thread-37
index is 34
pool-1-thread-35
index is 37
pool-1-thread-38
index is 38
pool-1-thread-39
index is 32
pool-1-thread-33

简单总结下:newCachedThreadPool作为缓冲线程池,可用于不需要我们关注内部的实现的多线程场景,自身会根据一定策略执行多线程任务;并且最大限度考虑线程复用。

好啦,就这么多,下期再学习下其他的线程池~

原创粉丝点击