非同步背景處理
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
非同步工作是背景工作和永久作業的第二項元件。雖然永久和非同步工作都是在背景執行,但最終的情況並不相同。
非同步工作是指:
- 當下發生情境。
- 不會在應用程式重新啟動或裝置重新啟動後保留下來。
- 發生在主執行緒之外,或封鎖主執行緒。
這與持續性工作不同,後者可能會排定日後的執行時程,且仍會安排在應用程式重新啟動和裝置重新啟動後保持執行的狀態。例如,傳送一個從主執行緒傳送 HTTP 要求,只在收到時傳回結果的非同步工作。
Java 和 Kotlin
處理非同步工作的方式取決於您採用的整體應用程式架構。如果您使用 Java 程式設計語言應用程式,您的需求會與使用 Kotlin 不同。
|
Kotlin |
Java |
解決方案 |
協同程式。 |
Java 執行緒。 |
其他資訊 |
如需協同程式的完整總覽,請參閱協同程式指南。 |
詳情請參閱 Java 執行緒頁面。 |
其他資訊
如要進一步瞭解持續性工作,請參閱永久性工作總覽。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],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)."]]