Creating a Manager for Multiple Threads

https://developer.android.com/training/multiple-threads/create-threadpool.html

事实上,线程池很少在 Android App 开发实践中被用到。用到的场景主要是网络请求,而主流的库比如 okhttp、Picasso 等都在内部封装了多线程。这很大程度上降低了开发难度,使得 Android 开发者可以专注于业务逻辑的开发。

Actually, I rarely use thread pool in my Android development experience. The thread pool use cases are mainly network requests, fortunately popular third party libraries have already encapsulated it, such as OkHttp, Picasso. This reduces the difficulty, and allows Android developers to focus on business logic development.

If only need one execution running at a time, an IntentService suits your needs.

However if you really need multiple tasks to run at the same time, you should use ThreadPoolExecutor, which runs a task from a queue when a thread in its pool becomes free. To run a task, all you have to do is add it to the queue.

Because A thread pool can run multiple parallel instances of a task, so you should ensure that the code is thread-safe. Enclose variables that can be accessed by more than one thread in a synchronized block.

OpenSourceCode weiyi$ grep -rw "ThreadPoolExecutor" .

com/example/android/displayingbitmaps/util/DiskLruCache.java
com/squareup/picasso/PicassoExecutorService.java
okhttp/okhttp/src/main/java/okhttp3/ConnectionPool.java