319安卓开发博客Android技术

Android - 线程池

2018-02-16  本文已影响573人  Allens_Jiang
老婆保佑,代码无BUG

前言

2018 准备换工作,也得好好准备面试,PS,小伙伴需要招人不,求推荐

面试题: 说说你对JAVA 线程池的理解


目录


一:线程池的优点

二:线程种类

三:使用介绍

1. FixedThreadPool

ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);  

特点:

eg:

    private void init_TestThread() {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            final int finalI = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    Log.d("TAG", "run: " + finalI + "  thread: "+Thread.currentThread().getName());
                }
            };
            fixedThreadPool.execute(runnable);
        }
    }

log:

02-11 02:52:42.609 4305-4464/com.cpsc.rxjavademo D/TAG: run: 1  thread: pool-3-thread-2
02-11 02:52:42.622 4305-4463/com.cpsc.rxjavademo D/TAG: run: 0  thread: pool-3-thread-1
02-11 02:52:42.631 4305-4465/com.cpsc.rxjavademo D/TAG: run: 2  thread: pool-3-thread-3
02-11 02:52:45.612 4305-4464/com.cpsc.rxjavademo D/TAG: run: 3  thread: pool-3-thread-2
02-11 02:52:45.625 4305-4463/com.cpsc.rxjavademo D/TAG: run: 4  thread: pool-3-thread-1
02-11 02:52:45.634 4305-4465/com.cpsc.rxjavademo D/TAG: run: 5  thread: pool-3-thread-3
02-11 02:52:48.614 4305-4464/com.cpsc.rxjavademo D/TAG: run: 6  thread: pool-3-thread-2
02-11 02:52:48.628 4305-4463/com.cpsc.rxjavademo D/TAG: run: 7  thread: pool-3-thread-1
02-11 02:52:48.636 4305-4465/com.cpsc.rxjavademo D/TAG: run: 8  thread: pool-3-thread-3
02-11 02:52:51.617 4305-4464/com.cpsc.rxjavademo D/TAG: run: 9  thread: pool-3-thread-2

2. CacheThreadPool

 ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

特点:

eg:

 private void init_TestThread() {
        ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int finalI = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    Log.d("TAG", "run: " + finalI + "  thread: "+Thread.currentThread().getName());
                }
            };
            fixedThreadPool.execute(runnable);
        }
    }

log:

02-11 02:54:16.162 4305-4510/com.cpsc.rxjavademo D/TAG: run: 0  thread: pool-4-thread-1
02-11 02:54:16.164 4305-4511/com.cpsc.rxjavademo D/TAG: run: 1  thread: pool-4-thread-2
02-11 02:54:16.169 4305-4512/com.cpsc.rxjavademo D/TAG: run: 2  thread: pool-4-thread-3
02-11 02:54:16.170 4305-4513/com.cpsc.rxjavademo D/TAG: run: 3  thread: pool-4-thread-4
02-11 02:54:16.172 4305-4514/com.cpsc.rxjavademo D/TAG: run: 4  thread: pool-4-thread-5
02-11 02:54:16.173 4305-4515/com.cpsc.rxjavademo D/TAG: run: 5  thread: pool-4-thread-6
02-11 02:54:16.176 4305-4516/com.cpsc.rxjavademo D/TAG: run: 6  thread: pool-4-thread-7
02-11 02:54:16.180 4305-4517/com.cpsc.rxjavademo D/TAG: run: 7  thread: pool-4-thread-8
02-11 02:54:16.182 4305-4518/com.cpsc.rxjavademo D/TAG: run: 8  thread: pool-4-thread-9
02-11 02:54:16.184 4305-4520/com.cpsc.rxjavademo D/TAG: run: 9  thread: pool-4-thread-10

3. SingleThreadExecutor

ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); 

特点

eg:


    private void init_TestThread() {
        ExecutorService fixedThreadPool = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int finalI = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    Log.d("TAG", "run: " + finalI + "  thread: "+Thread.currentThread().getName());
                }
            };
            fixedThreadPool.execute(runnable);
        }
    }

log:

02-11 02:55:26.466 4305-4557/com.cpsc.rxjavademo D/TAG: run: 0  thread: pool-5-thread-1
02-11 02:55:29.466 4305-4557/com.cpsc.rxjavademo D/TAG: run: 1  thread: pool-5-thread-1
02-11 02:55:32.472 4305-4557/com.cpsc.rxjavademo D/TAG: run: 2  thread: pool-5-thread-1
02-11 02:55:35.473 4305-4557/com.cpsc.rxjavademo D/TAG: run: 3  thread: pool-5-thread-1
02-11 02:55:38.475 4305-4557/com.cpsc.rxjavademo D/TAG: run: 4  thread: pool-5-thread-1
02-11 02:55:41.476 4305-4557/com.cpsc.rxjavademo D/TAG: run: 5  thread: pool-5-thread-1
02-11 02:55:44.478 4305-4557/com.cpsc.rxjavademo D/TAG: run: 6  thread: pool-5-thread-1
02-11 02:55:47.479 4305-4557/com.cpsc.rxjavademo D/TAG: run: 7  thread: pool-5-thread-1
02-11 02:55:50.480 4305-4557/com.cpsc.rxjavademo D/TAG: run: 8  thread: pool-5-thread-1
02-11 02:55:53.481 4305-4557/com.cpsc.rxjavademo D/TAG: run: 9  thread: pool-5-thread-1

4. ScheduledThreadPool

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);  

特点

eg:

 private void init_TestThread() {
        ExecutorService fixedThreadPool = Executors.newScheduledThreadPool(3);
        for (int i = 0; i < 10; i++) {
            final int finalI = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    Log.d("TAG", "run: " + finalI + "  thread: "+Thread.currentThread().getName());
                }
            };
            fixedThreadPool.execute(runnable);
        }
    }

log:


02-11 03:41:32.230 6981-7045/com.cpsc.rxjavademo D/TAG: run: 0  thread: pool-1-thread-1
02-11 03:41:32.236 6981-7046/com.cpsc.rxjavademo D/TAG: run: 1  thread: pool-1-thread-2
02-11 03:41:32.239 6981-7047/com.cpsc.rxjavademo D/TAG: run: 2  thread: pool-1-thread-3
02-11 03:41:35.233 6981-7045/com.cpsc.rxjavademo D/TAG: run: 3  thread: pool-1-thread-1
02-11 03:41:35.238 6981-7046/com.cpsc.rxjavademo D/TAG: run: 4  thread: pool-1-thread-2
02-11 03:41:35.240 6981-7047/com.cpsc.rxjavademo D/TAG: run: 5  thread: pool-1-thread-3
02-11 03:41:38.234 6981-7045/com.cpsc.rxjavademo D/TAG: run: 6  thread: pool-1-thread-1
02-11 03:41:38.240 6981-7046/com.cpsc.rxjavademo D/TAG: run: 7  thread: pool-1-thread-2
02-11 03:41:38.242 6981-7047/com.cpsc.rxjavademo D/TAG: run: 8  thread: pool-1-thread-3
02-11 03:41:41.237 6981-7045/com.cpsc.rxjavademo D/TAG: run: 9  thread: pool-1-thread-1

四:源码分析

 public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
  public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
 public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }


    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE,
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
              new DelayedWorkQueue());
    }

五:ThreadPoolExecutor参数

 public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {}

参数说明

参数 说明
corePoolSize 线程池中核心线程的数量
maximumPoolSize 线程池中最大线程数量
keepAliveTime 非核心线程的超时时长,当系统中非核心线程闲置时间超过keepAliveTime之后,则会被回收。如果ThreadPoolExecutor的allowCoreThreadTimeOut属性设置为true,则该参数也表示核心线程的超时时长
unit 用于指定keepAliveTime参数单位,有纳秒、微秒、毫秒、秒、分、时、天等
workQueue 线程池中的任务队列,该队列主要用来存储已经被提交但是尚未执行的任务。存储在这里的任务是由ThreadPoolExecutor的execute方法提交来的。

参考

深入理解java线程池—ThreadPoolExecutor

Android多线程:线程池ThreadPool 全面解析

上一篇下一篇

猜你喜欢

热点阅读