WorkManager 中的執行緒
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在「開始使用 WorkManager」中,我們提到 WorkManager 代表您執行非同步背景作業。基本實作方式可滿足大部分應用程式的需求。如需更多進階用途 (例如正確處理停止作業),則應瞭解 WorkManager 中的執行緒和並行功能。
WorkManager 提供四種不同類型的作業基元:
Worker
是最簡單的實作方式,也是您在上一節看到的實作方式。WorkManager 在背景執行緒自動執行 (您可以覆寫)。如要進一步瞭解 Worker
執行個體中的執行緒,請參閱「工作站中的執行緒」。
CoroutineWorker
是 Kotlin 使用者的建議實作方式。CoroutineWorker
執行個體會公開背景作業的暫停使用函式。根據預設,會執行預設的 Dispatcher
,而您可以自訂該預設。如要進一步瞭解 CoroutineWorker
執行個體中的執行緒,請參閱「CoroutineWorker 中的執行緒」。
- 我們建議為 RxJava 使用者採用
RxWorker
實作方式。如果現有的非同步程式碼是以 RxJava 為模型建立,則應使用 RxWorker。和所有 RxJava 概念一樣,您可以自行選擇要採用的執行緒策略。如要進一步瞭解 RxWorker
執行個體中的執行緒功能,請參閱「RxWorker 中的執行緒功能」。
ListenableWorker
是 Worker
、CoroutineWorker
和 RxWorker
的基本類別。這個 API 重點在協助 Java 開發人員與回呼式的非同步 API (例如 FusedLocationProviderClient
) 互動,且未使用 RxJava。如要進一步瞭解 ListenableWorker
執行個體中的執行緒功能,請參閱「ReadableWorker 中的執行緒」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間: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)."]]