Android interview to talk about the principle of the thread pool

Three-step answer questions

  1. The thread pool is used to doing?

  2. Thread pool is how to achieve the core principle

  3. What are the benefits thread pool, how we use it better

The thread pool is used to doing?

If the number of concurrent threads of many, and each thread is executed in a very short time the task is over, so often create threads will greatly reduce the efficiency of the system, because of frequent thread creation and destruction of threads it takes time.

Two words: efficiency

Thread pool is how to achieve the core principle

The real core

Producer - consumer model

Principle thread pool

Specific implementation strategy

BlockingQueue<Runnable> workQueue;  //任务缓存队列,用来存放等待执行的任务

private final HashSet<Worker> workers = new HashSet<Worker>();  //用来存放工作集

private volatile ThreadFactory threadFactory;   //线程工厂,用来创建线程

Thread pool policy

What are the benefits thread pool, how we use it better

Depending on the scene, configure different policies, cpu-intensive and intensive io

such as:

  1. Send Message scene
  2. Batch decrypt the file

Guess you like

Origin blog.csdn.net/u011077027/article/details/93380839