Android 四种构造线程池的默认方法

78 阅读2分钟

newCachedThreadPool

  • 创建一个核心线程为0,非核心线程数量为Int.MaxValue的线程池
  • 当线程池没有线程或线程池没有空闲线程时会创建新线程,否则就使用之前的线程
  • 没有核心线程数意味着,这个线程池当有任务进来没有空闲线程的话就会去创建新的线程,如果线程执行完了任务, 在60秒内没有新的任务进来,当前线程就会被回收
  • 如果当前需要执行任务200个,当下一个任务进入线程池后,只要当前线程池没有空闲的线程, 这里就会创建新的线程去执行任务,执行完等待60秒,没有任务进来就会回收
/**
 * return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
 *                                       60L, TimeUnit.SECONDS,
 *                                       new SynchronousQueue<Runnable>());
 */
private fun createNewCachedThreadPool(){
    val startTime = System.currentTimeMillis()
    val cachedThreadPool = Executors.newCachedThreadPool()
    for (i in 0 .. 200){
        cachedThreadPool.execute {
            Thread.sleep(2000)
            Log.e("TAG-DY", "createNewCachedThreadPool: $i")
            if (i == 200){
                val endTime = System.currentTimeMillis()
                Log.e("TAG-DY-Time", "createNewCachedThreadPoolTime: ${endTime - startTime}")
            }
        }
    }
}

newFixedThreadPool

  • 创建一个固定大小的线程池,池子里面只会有指定数量的线程,并且这些线程都是核心线程 不会被回收,
  • 如果线程都在执行任务,现在有新的任务进来,将会把任务存进LinkedBlockingQueue队列中进行等待,
  • 等线程中任务执行完成了再把队列的任务拿出来执行,
  • 比如200个任务,但是线程数只有10个,这样的话 只能一次并行执行10个任务,只有当每执行完一个任务, 那个核心线程才会空出来执行下一个
/**
 * return new ThreadPoolExecutor(nThreads, nThreads,
 *                                       0L, TimeUnit.MILLISECONDS,
 *                                       new LinkedBlockingQueue<Runnable>());
 */
private fun createNewFixedThreadPool(){
    val startTime = System.currentTimeMillis()
    val newFixedThreadPool = Executors.newFixedThreadPool(10)
    for (i in 0 .. 200){
        newFixedThreadPool.execute {
            Thread.sleep(2000)
            Log.e("TAG-DY", "createNewFixedThreadPool: $i")
            if (i == 200){
                val endTime = System.currentTimeMillis()
                Log.e("TAG-DY-Time", "createNewFixedThreadPool: ${endTime - startTime}")
            }
        }
    }
}

newScheduledThreadPool 跟newCachedThreadPool有点像,区别就是有核心线程,并且可以延时执行任务

  • 创建一个可指定核心线程数大小的线程池,池子里面有指定数量的核心线程,还有Int数最大值的非核心线程,
  • 而且当前线程池可以延时执行任务
  • 比如说有200个任务,让这个线程池运行,首先会把核心线程拿过来执行任务,这里我们的核心线程数是10个, 肯定是不够数的,这时会去创建新的线程,来运行任务,当非核心线程执行完后,这边默认存活时间是10秒, 等过了10秒后队列没有任务就会把非核心线程回收

newSingleThreadExecutor

  • 只有一个核心线程且只能有一个
  • 运行两百个任务需要等上一个执行完才能执行下一个
/**
 * return new FinalizableDelegatedExecutorService
 *             (new ThreadPoolExecutor(1, 1,
 *                                     0L, TimeUnit.MILLISECONDS,
 *                                     new LinkedBlockingQueue<Runnable>()));
 */
private fun createNewSingleThreadExecutor(){
    val startTime = System.currentTimeMillis()
    val newSingleThreadExecutor = Executors.newSingleThreadExecutor()
    for (i in 0 .. 200){
        newSingleThreadExecutor.execute {
            Thread.sleep(2000)
            Log.e("TAG-DY", "newSingleThreadExecutor: $i")
            if (i == 200){
                val endTime = System.currentTimeMillis()
                Log.e("TAG-DY-Time", "newSingleThreadExecutor: ${endTime - startTime}")
            }
        }
    }
}