# Sharing Thread Pools Across Libraries in Android Applications

DevFeed: [Sharing Thread Pools Across Libraries in Android Applications](<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: [Stories by Chao Zhang on Medium](<https://devfeed.tech/sources/stories-by-chao-zhang-on-medium.md>)

Topics: [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [Concurrent Programming](<https://devfeed.tech/topics/concurrent-programming.md>), [Android](<https://devfeed.tech/topics/android.md>), [Library](<https://devfeed.tech/topics/library.md>), [App](<https://devfeed.tech/topics/app.md>), [Network](<https://devfeed.tech/topics/network.md>), [Jetpack](<https://devfeed.tech/topics/jetpack.md>), [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.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>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [memory](<https://devfeed.tech/tags/memory.md>), [okhttp](<https://devfeed.tech/tags/okhttp.md>), [overhead](<https://devfeed.tech/tags/overhead.md>), [performance](<https://devfeed.tech/tags/performance.md>), [thread](<https://devfeed.tech/tags/thread.md>), [threading](<https://devfeed.tech/tags/threading.md>), [threads](<https://devfeed.tech/tags/threads.md>), [work](<https://devfeed.tech/tags/work.md>)

## AI overview

This Android development tutorial explains how creating threads for asynchronous tasks can increase memory pressure, scheduling overhead, and context-switching costs. It discusses using thread pools and proposes configuring libraries such as OkHttp and AndroidX WorkManager to share a common pool, while considering the benefits and costs.

## 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