WorkManager 中的线程处理
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 WorkManager 使用入门中,我们提到 WorkManager 可以代表您异步执行后台工作。该基本实现可满足大多数应用的需求。关于更高级的用例(例如正确处理正在停止的工作),您应了解 WorkManager 中的线程处理和并发机制。
WorkManager 提供了四种不同类型的工作基元:
Worker
是最简单的实现,我们已在前面几节进行了介绍。WorkManager 会在后台线程中自动运行该基元(您可以将它替换掉)。请参阅工作器中的线程处理,详细了解 Worker
实例中的线程处理。
CoroutineWorker
是为 Kotlin 用户建议的实现。CoroutineWorker
实例公开了后台工作的一个挂起函数。默认情况下,这些实例运行默认的 Dispatcher
,但您可以进行自定义。请参阅 CoroutineWorker 中的线程处理,详细了解 CoroutineWorker
实例中的线程处理。
RxWorker
是为 RxJava 用户建议的实现。如果您有很多现有异步代码是用 RxJava 建模的,则应使用 RxWorker。与所有 RxJava 概念一样,您可以自由选择所需的线程处理策略。请参阅 RxWorker 中的线程处理,详细了解 RxWorker
实例中的线程处理。
ListenableWorker
是 Worker
、CoroutineWorker
和 RxWorker
的基类。这个类专为需要与基于回调的异步 API(例如 FusedLocationProviderClient
)进行交互并且不使用 RxJava 的 Java 开发者而设计。请参阅 ListenableWorker 中的线程处理,详细了解 ListenableWorker
实例中的线程处理。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Threading in WorkManager\n\nIn [Getting started with WorkManager](/topic/libraries/architecture/workmanager/basics),\nwe mentioned that WorkManager performs background work asynchronously on your\nbehalf. The basic implementation addresses the demands of most apps. For more\nadvanced use cases, such as correctly handling work being stopped, you should\nlearn about threading and concurrency in WorkManager.\n\nThere are four different types of work primitives provided by WorkManager:\n\n- [`Worker`](/reference/androidx/work/Worker) is the simplest implementation, and the one you have seen in previous sections. WorkManager automatically runs it on a background thread (that you can override). Read more about threading in `Worker` instances in [Threading in Worker](/topic/libraries/architecture/workmanager/advanced/worker).\n- [`CoroutineWorker`](/reference/kotlin/androidx/work/CoroutineWorker) is the recommended implementation for Kotlin users. `CoroutineWorker` instances expose a suspending function for background work. By default, they run a default `Dispatcher`, which you can customize. Read more about threading in `CoroutineWorker` instances in [Threading in CoroutineWorker](/topic/libraries/architecture/workmanager/advanced/coroutineworker).\n- [`RxWorker`](/reference/androidx/work/RxWorker) is the recommended implementation for RxJava users. RxWorkers should be used if a lot of your existing asynchronous code is modelled in RxJava. As with all RxJava concepts, you are free to choose the threading strategy of your choice. Read more about threading in `RxWorker` instances in [Threading in RxWorker](/topic/libraries/architecture/workmanager/advanced/rxworker).\n- [`ListenableWorker`](/reference/androidx/work/ListenableWorker) is the base class for `Worker`, `CoroutineWorker`, and `RxWorker`. It is intended for Java developers who have to interact with callback-based asynchronous APIs such as `FusedLocationProviderClient` and are not using RxJava. Read more about threading in `ListenableWorker` instances in [Threading in ListenableWorker](/topic/libraries/architecture/workmanager/advanced/listenableworker)."]]