Elaborazione asincrona in background
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Il lavoro asincrono è il secondo componente del lavoro in background, insieme al lavoro persistente. Il lavoro persistente e asincrono si svolge in background, ma in ultima analisi sono molto diversi.
Il lavoro asincrono è quello che:
- Si svolge sul momento.
- Non deve rimanere invariato tra i riavvii dell'app o del dispositivo.
- Si verifica al di fuori del thread principale o blocca il thread principale.
Questo è in contrasto con il lavoro persistente, che potresti pianificare per un'esecuzione futura e che rimane programmata mediante riavvii delle app e dei dispositivi.
Un esempio di lavoro asincrono può essere l'invio di una richiesta HTTP dal thread principale, restituendo il risultato solo quando arriva.
Java e Kotlin
Il modo in cui gestisci il lavoro asincrono dipende dall'architettura complessiva dell'app
che segui. Se lavori con un'app Java Programming Language, le tue esigenze
sono diverse rispetto a quando lavori con Kotlin.
|
Kotlin |
Java |
Soluzione |
Coroutine. |
Thread Java. |
Continua a leggere |
Per una panoramica completa delle Coroutine, consulta la Guida alle coroutine. |
Per ulteriori informazioni, consulta la pagina Thread Java. |
Continua a leggere
Per saperne di più sul lavoro persistente, consulta la panoramica sul lavoro persistente.
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,["# Asynchronous background processing\n\nAsynchronous work is the second component of background work, alongside\npersistent work. While both persistent and asynchronous work take place in the\nbackground, they are ultimately quite different.\n\nAsynchronous work is that which:\n\n- Takes place in the moment.\n- Does not need to persist across app restarts or device reboots.\n- Occurs off the main thread, or blocks the main thread.\n\nThis is in contrast to persistent work, which you may schedule for future\nexecution and which remains scheduled through app restarts and device reboots.\nAn example of asynchronous work may be sending an HTTP request off the main\nthread, returning its result only when it arrives.\n\nJava and Kotlin\n---------------\n\nThe way you handle asynchronous work depends on the overall app architecture\nyou follow. If you are working with a Java Programming Language app, your needs\nare different than if you are working with Kotlin.\n\n| | Kotlin | Java |\n| Solution | Coroutines. | Java threads. |\n| Further reading | For a full overview of Coroutines, see the [Coroutines guide](/kotlin/coroutines). | See the [Java Threads](/guide/background/asynchronous/java-threads) page for more information. |\n|-----------------|------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|\n\nFurther reading\n---------------\n\nFor more information on persistent work, see the [persistent work overview](/guide/background/persistent)."]]