Exécuter des threads dans WorkManager
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Dans les Premiers pas avec WorkManager, nous avons vu que WorkManager effectue une tâche en arrière-plan de manière asynchrone en votre nom. L'intégration de base répond aux demandes de la plupart des applications. Apprenez-en davantage sur les exécutions de threads et la simultanéité dans WorkManager pour les cas d'utilisation plus avancés, tels que la gestion correcte des tâches en cours d'arrêt.
Quatre types différents de rôles primitifs fournis par WorkManager existent :
Worker
est l'intégration la plus simple, que nous avons vue dans les sections précédentes. Work Manager l'exécute automatiquement sur un thread d'arrière-plan (que vous pouvez ignorer). Pour en savoir plus sur l'exécution des threads dans les instances Worker
, consultez les exécutions de threads dans Worker.
CoroutineWorker
est l'intégration recommandée pour les utilisateurs de Kotlin. Les instances CoroutineWorker
exposent une fonction de suspension pour les tâches en arrière-plan. Par défaut, elles exécutent un Dispatcher
par défaut que vous pouvez personnaliser. Pour en savoir plus sur l'exécution de threads dans des instances CoroutineWorker
, consultez les exécutions de threads dans CoroutineWorker.
RxWorker
est l'intégration recommandée pour les utilisateurs de RxJava. Utilisez RxWorkers si une grande partie de votre code asynchrone existant est modélisée dans RxJava. Comme pour tous les concepts RxJava, vous êtes libre de choisir la stratégie d'exécution de threads de votre choix. Pour en savoir plus sur l'exécution de threads dans les instances RxWorker
, consultez les exécutions de threads dans RxWorker.
ListenableWorker
est la classe de base pour Worker
, CoroutineWorker
et RxWorker
. Elle est destinée aux développeurs Java qui doivent interagir avec des API asynchrones basées sur le rappel, telles que FusedLocationProviderClient
, et qui n'utilisent pas RxJava. Pour en savoir plus sur l'exécution de threads dans des instances ListenableWorker
, consultez les exécutions de threads dans ListenableWorker.
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/26 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)."]]