การประมวลผลในเบื้องหลังแบบอะซิงโครนัส
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
งานแบบไม่พร้อมกันเป็นคอมโพเนนต์ที่ 2 ของงานในเบื้องหลัง ซึ่งทำงานควบคู่กับ
งานที่ต่อเนื่อง แม้ว่าทั้งงานแบบต่อเนื่องและงานแบบไม่พร้อมกันจะเกิดขึ้นใน
เบื้องหลัง แต่ในท้ายที่สุดก็มีความแตกต่างกันอย่างมาก
การทำงานแบบอะซิงโครนัสคือการทำงานที่
- เกิดขึ้นในขณะนั้น
- ไม่จำเป็นต้องคงอยู่เมื่อรีสตาร์ทแอปหรือรีบูตอุปกรณ์
- เกิดขึ้นนอกเทรดหลักหรือบล็อกเทรดหลัก
ซึ่งแตกต่างจากงานที่ทำงานอย่างต่อเนื่องซึ่งคุณอาจกำหนดเวลาสำหรับการดำเนินการในอนาคต และยังคงกำหนดเวลาไว้แม้จะรีสตาร์ทแอปและรีบูตอุปกรณ์
ตัวอย่างของงานแบบอะซิงโครนัสอาจเป็นการส่งคำขอ HTTP ออกจากเทรดหลัก
และแสดงผลลัพธ์เมื่อได้รับเท่านั้น
Java และ Kotlin
วิธีจัดการงานแบบอะซิงโครนัสขึ้นอยู่กับสถาปัตยกรรมแอปโดยรวม
ที่คุณใช้ หากคุณทำงานกับแอปภาษาโปรแกรม Java ความต้องการของคุณจะแตกต่างจากกรณีที่คุณทำงานกับ Kotlin
|
Kotlin |
Java |
โซลูชัน |
โครูทีน |
เธรด Java |
อ่านเพิ่มเติม |
ดูภาพรวมทั้งหมดของ Coroutines ได้ที่คู่มือ Coroutines |
ดูข้อมูลเพิ่มเติมได้ที่หน้าเธรด Java |
อ่านเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับงานที่คงอยู่ได้ที่ภาพรวมของงานที่คงอยู่
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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-27 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)."]]