Threading in WorkManager
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Nella Guida introduttiva a WorkManager abbiamo accennato al fatto che WorkManager esegue il lavoro in background in modo asincrono per tuo conto. L'implementazione di base soddisfa le esigenze della maggior parte delle app. Per casi d'uso più avanzati, ad esempio per gestire correttamente l'interruzione del lavoro, scopri di più sull'organizzazione in thread e sulla contemporaneità in WorkManager.
Sono disponibili quattro diversi tipi di primitive di lavoro fornite da WorkManager:
Worker
è l'implementazione più semplice
e quella che hai visto nelle sezioni precedenti. WorkManager lo esegue automaticamente su un thread in background (che puoi sostituire). Scopri di più sull'organizzazione in thread nelle istanze Worker
in Threading nel worker.
CoroutineWorker
è
l'implementazione consigliata per gli utenti di Kotlin. CoroutineWorker
istanze espongono una funzione di sospensione per il lavoro in background. Per impostazione predefinita, viene eseguito un Dispatcher
predefinito, che puoi personalizzare. Scopri di più sull'organizzazione in thread nelle istanze CoroutineWorker
in Threading in CoroutineWorker.
RxWorker
è l'implementazione consigliata per gli utenti di RxJava. Occorre utilizzare RxWorkers se gran parte del codice asincrono esistente è modellata in RxJava. Come per tutti i concetti di RxJava, puoi scegliere liberamente la strategia di organizzazione in thread. Scopri di più sul
threading nelle istanze RxWorker
in Threading in RxWorker.
ListenableWorker
è la classe
base per Worker
, CoroutineWorker
e RxWorker
. È destinato agli sviluppatori Java che devono interagire con API asincrone basate su callback come FusedLocationProviderClient
e non utilizzano RxJava. Scopri di più
sull'organizzazione in thread delle istanze ListenableWorker
in Threading inPE WatchableWorker.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-07-26 UTC.
[null,null,["Ultimo aggiornamento 2025-07-26 UTC."],[],[],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)."]]