# Reduce, Reuse & Recycle your thread pools ♻

DevFeed: [Reduce, Reuse & Recycle your thread pools ♻](<https://devfeed.tech/articles/reduce-reuse-recycle-your-thread-pools-25909.md>)

Original publisher: [Read original article](<https://chao2zhang.medium.com/reduce-reuse-recycle-your-thread-pools-%EF%B8%8F-81e2f54d8a1d?source=rss-d19045640fe------2>)

Author: Chao Zhang

Published: 2021-12-20T18:26:36Z

Content type: tutorial

Language: en

Sources: [Chao Zhang](<https://devfeed.tech/sources/stories-by-chao-zhang-on-medium.md>)

Topics: [threadpools](<https://devfeed.tech/topics/threadpools.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [mobile-app-performance](<https://devfeed.tech/topics/mobile-app-performance.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [ExecutorService](<https://devfeed.tech/topics/executorservice.md>), [Library](<https://devfeed.tech/topics/library.md>), [scheduling](<https://devfeed.tech/topics/scheduling.md>), [Jetpack](<https://devfeed.tech/topics/jetpack.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [app-development](<https://devfeed.tech/tags/app-development.md>), [app-performance](<https://devfeed.tech/tags/app-performance.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [costs](<https://devfeed.tech/tags/costs.md>), [health](<https://devfeed.tech/tags/health.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [manage](<https://devfeed.tech/tags/manage.md>), [memory](<https://devfeed.tech/tags/memory.md>), [memory-pressure](<https://devfeed.tech/tags/memory-pressure.md>), [okhttp](<https://devfeed.tech/tags/okhttp.md>), [overhead](<https://devfeed.tech/tags/overhead.md>), [performance](<https://devfeed.tech/tags/performance.md>), [pool](<https://devfeed.tech/tags/pool.md>), [reduce](<https://devfeed.tech/tags/reduce.md>), [resources](<https://devfeed.tech/tags/resources.md>), [reuse](<https://devfeed.tech/tags/reuse.md>), [runtime](<https://devfeed.tech/tags/runtime.md>), [schedules](<https://devfeed.tech/tags/schedules.md>), [thread](<https://devfeed.tech/tags/thread.md>), [thread-pools](<https://devfeed.tech/tags/thread-pools.md>)

## AI overview

Android thread pools can be consolidated to reduce thread count and resource overhead, but merging changes concurrency behavior and requires auditing every library. The sample app routes OkHttp (including Retrofit and Coil), WorkManager, and coroutines through a shared ThreadPoolExecutor; WorkManager needs on-demand initialization and a custom Configuration.Provider, while coroutine dispatchers must be injectable and converted from the executor. The sample's active threads fell from 35 to 19. A separate A/B test in a large app measured a 2% reduction in 90th-percentile cold launch time, a context-specific result. Pool defaults differ, and missed configuration can leave extra pools active; the author recommends naming and auditing threads, validating changes with A/B tests, and guarding against regressions.

## Source excerpt

Photo: Héctor J. Rivas from Unsplash Real-world applications today are mostly multi-threaded. This means developers should be mindful of managing the concurrency of their applications. Mastering the threading can help boost an app's performance. On the other hand, using concurrency without fully understanding it could lead to problems that negatively impact the app's health. For Android applications, every thread is mapped to a system-level thread at runtime. Each thread costs a minimum of 64k of memory on Android, If we always create a thread for any new asynchronous task, we will create memory pressure on the app. The app performance may suffer because spawning up new threads and context switching among threads are both taking up time and resources. If a thread is referenced even if it is not active, it will be kept in memory and can't be cleaned up by the garbage collector. Thread pools can help us manage concurrency more efficiently. ThreadPoolExecutor creates a pool of worker threads and schedules the tasks for them to execute. It can grow the pool size to meet the demand as new tasks arrive, and it can shrink the pool when threads are idle and no longer need to be kept alive. Thread pool, therefore, improves the app performance by reducing the per-task overhead and controls the resource usage by bounding the resources. Thread pool seems the solution to our concurrency headache, and many libraries have adopted this technique, such as OkHttp and AndroidX WorkManager. Each library maintains its own thread pool by default. When we include these libraries in our application, since each library spins up new threads by itself without the awareness of other thread pools, we come full circle. Hundreds of threads exist in our application because those libraries do not know each other. In this story, we are going to walk through an example of configuring libraries to share a common thread pool. We will also summarize the benefits and costs of managing thread pools, as it