异步后台处理
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
与持久性工作一样,异步工作是后台工作的第二个组件。虽然持久性工作和异步工作都发生在后台,但它们最终却截然不同。
异步工作是指:
- 瞬间发生。
- 无需在应用重启或设备重新启动后保持不变。
- 发生在主线程外,或阻塞主线程。
这与持久性工作相反,持久性工作可以安排在未来执行,并且仍将通过应用重启和设备重新启动来调度。异步工作的一个示例可能是将 HTTP 请求发送到主线程以外,仅在请求到达时返回其结果。
Java 和 Kotlin
您处理异步工作的方式取决于您遵循的整体应用架构。如果您使用的是 Java 编程语言应用,您的需求与使用 Kotlin 时有所不同。
|
Kotlin |
Java |
解决方案 |
协程。 |
Java 线程。 |
深入阅读 |
如需了解协程的完整概览,请参阅协程指南。 |
如需了解详情,请参阅 Java 线程页面。 |
深入阅读
如需详细了解持久性工作,请参阅持久性工作概览。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):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)."]]