{"id":16262,"date":"2026-03-05T14:51:34","date_gmt":"2026-03-05T06:51:34","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262"},"modified":"2026-03-05T15:23:14","modified_gmt":"2026-03-05T07:23:14","slug":"%e5%9b%9b%e3%80%81%e5%8d%8f%e7%a8%8b%e5%8f%a5%e6%9f%84job","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262","title":{"rendered":"\u56db\u3001\u534f\u7a0b\u53e5\u67c4(Job)"},"content":{"rendered":"<p>\u5b98\u65b9\u51fa\u5904\uff1a<a href=\"https:\/\/developer.android.google.cn\/kotlin\/coroutines\/coroutines-adv?hl=zh-cn\">https:\/\/developer.android.google.cn\/kotlin\/coroutines\/coroutines-adv?hl=zh-cn<\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%E4%B8%80%E3%80%81Job%E4%BB%8B%E7%BB%8D\" title=\"\u4e00\u3001Job\u4ecb\u7ecd\">\u4e00\u3001Job\u4ecb\u7ecd<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%8E%AE_Job%EF%BC%9A%E5%8D%8F%E7%A8%8B%E7%9A%84%E9%81%A5%E6%8E%A7%E5%99%A8%E5%92%8C%E8%BA%AB%E4%BB%BD%E8%AF%81\" title=\"\ud83c\udfae Job\uff1a\u534f\u7a0b\u7684\u201c\u9065\u63a7\u5668\u201d\u548c\u201c\u8eab\u4efd\u8bc1\u201d\">\ud83c\udfae Job\uff1a\u534f\u7a0b\u7684\u201c\u9065\u63a7\u5668\u201d\u548c\u201c\u8eab\u4efd\u8bc1\u201d<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%A4%94_%E4%B8%A4%E7%A7%8D%E4%BD%BF%E7%94%A8_Job_%E7%9A%84%E6%96%B9%E5%BC%8F\" title=\"\ud83e\udd14 \u4e24\u79cd\u4f7f\u7528 Job \u7684\u65b9\u5f0f\">\ud83e\udd14 \u4e24\u79cd\u4f7f\u7528 Job \u7684\u65b9\u5f0f<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E7%94%A8_Job_%E6%8E%A7%E5%88%B6%E5%8D%95%E4%B8%AA%E5%8D%8F%E7%A8%8B%EF%BC%88%E6%9C%80%E5%B8%B8%E8%A7%81%EF%BC%89\" title=\"\u65b9\u5f0f\u4e00\uff1a\u7528 Job \u63a7\u5236\u5355\u4e2a\u534f\u7a0b\uff08\u6700\u5e38\u89c1\uff09\">\u65b9\u5f0f\u4e00\uff1a\u7528 Job \u63a7\u5236\u5355\u4e2a\u534f\u7a0b\uff08\u6700\u5e38\u89c1\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%B0%86_Job_%E4%BC%A0%E7%BB%99_CoroutineScope%EF%BC%88%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%EF%BC%89\" title=\"\u65b9\u5f0f\u4e8c\uff1a\u5c06 Job \u4f20\u7ed9 CoroutineScope\uff08\u9ad8\u7ea7\u7528\u6cd5\uff09\">\u65b9\u5f0f\u4e8c\uff1a\u5c06 Job \u4f20\u7ed9 CoroutineScope\uff08\u9ad8\u7ea7\u7528\u6cd5\uff09<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%93%8A_%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%E7%9A%84%E5%AF%B9%E6%AF%94\" title=\"\ud83d\udcca \u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4\">\ud83d\udcca \u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%92%A1_%E6%80%BB%E7%BB%93%E4%B8%80%E4%B8%8B\" title=\"\ud83d\udca1 \u603b\u7ed3\u4e00\u4e0b\">\ud83d\udca1 \u603b\u7ed3\u4e00\u4e0b<\/a><\/li><\/ul><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%E4%BA%8C%E3%80%81Scope%E4%BC%A0%E5%85%A5Job%E4%BD%9C%E4%B8%BA%E6%A0%B9Job%E7%9A%84%E6%84%8F%E4%B9%89\" title=\"\u4e8c\u3001Scope\u4f20\u5165Job\u4f5c\u4e3a\u6839Job\u7684\u610f\u4e49\">\u4e8c\u3001Scope\u4f20\u5165Job\u4f5c\u4e3a\u6839Job\u7684\u610f\u4e49<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%92%A1_%E6%A0%B8%E5%BF%83%E7%AD%94%E6%A1%88%EF%BC%9A%E5%AE%83%E4%BB%AC%E6%98%AF%E5%90%8C%E4%B8%80%E4%B8%AA%E6%93%8D%E4%BD%9C%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%85%A5%E5%8F%A3\" title=\"\ud83d\udca1 \u6838\u5fc3\u7b54\u6848\uff1a\u5b83\u4eec\u662f\u201c\u540c\u4e00\u4e2a\u64cd\u4f5c\u7684\u4e24\u4e2a\u5165\u53e3\u201d\">\ud83d\udca1 \u6838\u5fc3\u7b54\u6848\uff1a\u5b83\u4eec\u662f\u201c\u540c\u4e00\u4e2a\u64cd\u4f5c\u7684\u4e24\u4e2a\u5165\u53e3\u201d<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%94%8D_%E6%B7%B1%E5%85%A5%E4%B8%80%E5%B1%82%EF%BC%9AScope_%E5%92%8C_Job_%E7%9A%84%E5%85%B3%E7%B3%BB\" title=\"\ud83d\udd0d \u6df1\u5165\u4e00\u5c42\uff1aScope \u548c Job \u7684\u5173\u7cfb\">\ud83d\udd0d \u6df1\u5165\u4e00\u5c42\uff1aScope \u548c Job \u7684\u5173\u7cfb<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%8E%AF_%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%E7%9A%84%E5%AF%B9%E6%AF%94%E4%B8%8E%E9%80%89%E6%8B%A9\" title=\"\ud83c\udfaf \u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4\u4e0e\u9009\u62e9\">\ud83c\udfaf \u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4\u4e0e\u9009\u62e9<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%9A%80_%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%E9%80%9A%E8%BF%87%E6%A0%B9_Job_%E5%8F%96%E6%B6%88%E8%BF%99%E7%A7%8D%E7%81%B5%E6%B4%BB%E6%80%A7%EF%BC%9F\" title=\"\ud83d\ude80 \u4e3a\u4ec0\u4e48\u9700\u8981\u201c\u901a\u8fc7\u6839 Job \u53d6\u6d88\u201d\u8fd9\u79cd\u7075\u6d3b\u6027\uff1f\">\ud83d\ude80 \u4e3a\u4ec0\u4e48\u9700\u8981\u201c\u901a\u8fc7\u6839 Job \u53d6\u6d88\u201d\u8fd9\u79cd\u7075\u6d3b\u6027\uff1f<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16262\/#%F0%9F%93%9D_%E6%80%BB%E7%BB%93%E4%B8%8E%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5\" title=\"\ud83d\udcdd \u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5\">\ud83d\udcdd \u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81Job%E4%BB%8B%E7%BB%8D\"><\/span>\u4e00\u3001Job\u4ecb\u7ecd<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%8E%AE_Job%EF%BC%9A%E5%8D%8F%E7%A8%8B%E7%9A%84%E9%81%A5%E6%8E%A7%E5%99%A8%E5%92%8C%E8%BA%AB%E4%BB%BD%E8%AF%81\"><\/span>\ud83c\udfae Job\uff1a\u534f\u7a0b\u7684\u201c\u9065\u63a7\u5668\u201d\u548c\u201c\u8eab\u4efd\u8bc1\u201d<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6587\u6863\u91cc\u8bf4\u201c<strong>Job \u662f\u534f\u7a0b\u7684\u53e5\u67c4<\/strong>\u201d\uff0c\u610f\u601d\u662f\u5b83\u5c31\u50cf\u60a8\u62ff\u5230\u7684\u4e00\u4e2a<strong>\u9065\u63a7\u5668<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e2a\u9065\u63a7\u5668\uff0c\u60a8\u53ef\u4ee5\uff1a<\/p>\n<ul>\n<li><strong>\u63a7\u5236<\/strong>\uff1a\u968f\u65f6<strong>\u53d6\u6d88<\/strong>\u6b63\u5728\u8fd0\u884c\u7684\u534f\u7a0b\u3002<\/li>\n<li><strong>\u68c0\u67e5<\/strong>\uff1a\u67e5\u770b\u534f\u7a0b\u662f<strong>\u6d3b\u8dc3<\/strong>\u8fd8\u662f<strong>\u5df2\u5b8c\u6210<\/strong>\u3002<\/li>\n<li><strong>\u7b49\u5f85<\/strong>\uff1a\u7b49\u5f85\u534f\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002<\/li>\n<\/ul>\n<p>\u540c\u65f6\uff0c\u6bcf\u4e2a\u901a\u8fc7 <code>launch<\/code> \u6216 <code>async<\/code> \u542f\u52a8\u7684\u534f\u7a0b\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4e13\u5c5e\u7684 <code>Job<\/code> \u5b9e\u4f8b\u3002\u8fd9\u4e2a\u5b9e\u4f8b\u5c31\u662f\u8be5\u534f\u7a0b\u7684<strong>\u552f\u4e00\u8eab\u4efd\u8bc1<\/strong>\uff0c\u7528\u4e8e\u6807\u8bc6\u548c\u7ba1\u7406\u5b83\u81ea\u5df1\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%A4%94_%E4%B8%A4%E7%A7%8D%E4%BD%BF%E7%94%A8_Job_%E7%9A%84%E6%96%B9%E5%BC%8F\"><\/span>\ud83e\udd14 \u4e24\u79cd\u4f7f\u7528 Job \u7684\u65b9\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u60a8\u53ef\u4ee5\u5c06 Job \u7528\u5728\u4e24\u4e2a\u5c42\u9762\uff0c\u7406\u89e3\u5b83\u4eec\u7684\u533a\u522b\u5f88\u5173\u952e\uff1a<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E7%94%A8_Job_%E6%8E%A7%E5%88%B6%E5%8D%95%E4%B8%AA%E5%8D%8F%E7%A8%8B%EF%BC%88%E6%9C%80%E5%B8%B8%E8%A7%81%EF%BC%89\"><\/span>\u65b9\u5f0f\u4e00\uff1a\u7528 Job \u63a7\u5236\u5355\u4e2a\u534f\u7a0b\uff08\u6700\u5e38\u89c1\uff09<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5f53\u60a8\u7528 <code>launch<\/code> \u542f\u52a8\u534f\u7a0b\u65f6\uff0c\u4f1a\u7acb\u5373\u5f97\u5230\u4e00\u4e2a\u8fd4\u56de\u7684 <code>Job<\/code> \u5bf9\u8c61\u3002\u8fd9\u4e2a <code>job<\/code> \u5c31\u662f\u4e13\u95e8\u7528\u6765\u63a7\u5236<strong>\u90a3\u4e00\u4e2a<\/strong>\u534f\u7a0b\u7684\u3002<\/p>\n<p>kotlin<\/p>\n<pre><code>\/\/ \u5047\u8bbe scope \u662f\u4e4b\u524d\u521b\u5efa\u7684 CoroutineScope\nval job: Job = scope.launch {\n    \/\/ ... \u8fd9\u91cc\u662f\u534f\u7a0b\u8981\u6267\u884c\u7684\u4ee3\u7801\n}\n\n\/\/ \u60a8\u53ef\u4ee5\u7528\u8fd9\u4e2a job \u6765\u63a7\u5236\u8fd9\u4e2a\u7279\u5b9a\u7684\u534f\u7a0b\nif (someCondition) {\n    job.cancel() \/\/ \u53ea\u53d6\u6d88\u8fd9\u4e00\u4e2a\u534f\u7a0b\n}\n\njob.join()      \/\/ \u7b49\u5f85\u8fd9\u4e00\u4e2a\u534f\u7a0b\u5b8c\u6210<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%B0%86_Job_%E4%BC%A0%E7%BB%99_CoroutineScope%EF%BC%88%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%EF%BC%89\"><\/span>\u65b9\u5f0f\u4e8c\uff1a\u5c06 Job \u4f20\u7ed9 CoroutineScope\uff08\u9ad8\u7ea7\u7528\u6cd5\uff09<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u8fd9\u662f\u6587\u6863\u4e2d\u201c\u60a8\u8fd8\u53ef\u4ee5\u5c06 <code>Job<\/code> \u4f20\u9012\u7ed9 <code>CoroutineScope<\/code> \u4ee5\u8fdb\u4e00\u6b65\u7ba1\u7406\u5176\u751f\u547d\u5468\u671f\u201d\u8fd9\u53e5\u8bdd\u7684\u610f\u601d\u3002\u8fd9\u91cc\u7684 Job \u4e0d\u518d\u662f\u63a7\u5236\u5355\u4e2a\u534f\u7a0b\uff0c\u800c\u662f\u4f5c\u4e3a <strong>Scope \u7684\u201c\u6839Job\u201d<\/strong>\uff0c\u7528\u6765\u63a7\u5236<strong>\u8be5 Scope \u4e0b\u7684\u6240\u6709\u534f\u7a0b<\/strong>\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>kotlin<\/p>\n<pre><code>\/\/ 1. \u5148\u521b\u5efa\u4e00\u4e2a Job \u5b9e\u4f8b\uff0c\u6bd4\u5982\u53eb parentJob\nval parentJob = Job()\n\n\/\/ 2. \u521b\u5efa CoroutineScope \u65f6\uff0c\u628a parentJob \u4f5c\u4e3a\u4e0a\u4e0b\u6587\u7684\u4e00\u90e8\u5206\u4f20\u8fdb\u53bb\n\/\/    \u8fd9\u6837\uff0cparentJob \u5c31\u6210\u4e86\u8fd9\u4e2a Scope \u7684\u201c\u6839Job\u201d\nval scope = CoroutineScope(parentJob + Dispatchers.Main)\n\n\/\/ 3. \u7528\u8fd9\u4e2a scope \u542f\u52a8\u591a\u4e2a\u534f\u7a0b\nscope.launch {\n    \/\/ \u534f\u7a0b A\n    delay(1000)\n}\n\nscope.launch {\n    \/\/ \u534f\u7a0b B\n    delay(2000)\n}\n\n\/\/ 4. \u6b64\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6700\u521d\u7684\u90a3\u4e2a parentJob \u6765\u7ba1\u7406\u6240\u6709\u534f\u7a0b\nif (needToCleanUpAll) {\n    parentJob.cancel() \/\/ \u2b50\ufe0f \u53d6\u6d88\u6574\u4e2a scope\uff01\u6240\u6709\u901a\u8fc7 scope \u542f\u52a8\u7684\u534f\u7a0b\u90fd\u4f1a\u88ab\u53d6\u6d88\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%93%8A_%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%E7%9A%84%E5%AF%B9%E6%AF%94\"><\/span>\ud83d\udcca \u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u4e3a\u4e86\u5e2e\u52a9\u60a8\u66f4\u76f4\u89c2\u5730\u7406\u89e3\uff0c\u6211\u6574\u7406\u4e86\u8fd9\u4e2a\u5bf9\u6bd4\u8868\u683c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u65b9\u5f0f<\/th>\n<th style=\"text-align: left;\">\u4ee3\u7801\u793a\u4f8b<\/th>\n<th style=\"text-align: left;\">\u63a7\u5236\u7684 Job \u5bf9\u8c61<\/th>\n<th style=\"text-align: left;\">\u63a7\u5236\u8303\u56f4<\/th>\n<th style=\"text-align: left;\">\u5178\u578b\u573a\u666f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><strong>\u63a7\u5236\u5355\u4e2a\u534f\u7a0b<\/strong><\/td>\n<td style=\"text-align: left;\"><code>val job = scope.launch { ... }<\/code><\/td>\n<td style=\"text-align: left;\"><code>launch<\/code> \u8fd4\u56de\u7684 <code>job<\/code><\/td>\n<td style=\"text-align: left;\">\u4ec5\u8fd9\u4e00\u4e2a\u534f\u7a0b<\/td>\n<td style=\"text-align: left;\">\u9700\u8981\u5355\u72ec\u53d6\u6d88\u6216\u7b49\u5f85\u67d0\u4e00\u4e2a\u4efb\u52a1\u5b8c\u6210\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u63a7\u5236\u6574\u4e2a\u4f5c\u7528\u57df<\/strong><\/td>\n<td style=\"text-align: left;\"><code>val scope = CoroutineScope(Job())<\/code><\/td>\n<td style=\"text-align: left;\">\u521b\u5efa Scope \u65f6\u4f20\u5165\u7684 <code>Job<\/code><\/td>\n<td style=\"text-align: left;\">Scope \u5185<strong>\u6240\u6709<\/strong>\u534f\u7a0b<\/td>\n<td style=\"text-align: left;\">\u7edf\u4e00\u7ba1\u7406\u4e00\u7ec4\u76f8\u5173\u7684\u534f\u7a0b\uff0c\u4f8b\u5982\u5728\u9875\u9762\u9500\u6bc1\u65f6\uff0c\u4e00\u6b21\u6027\u53d6\u6d88\u6240\u6709\u4efb\u52a1\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%92%A1_%E6%80%BB%E7%BB%93%E4%B8%80%E4%B8%8B\"><\/span>\ud83d\udca1 \u603b\u7ed3\u4e00\u4e0b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong><code>Job<\/code> \u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61<\/strong>\uff0c\u60a8\u53ef\u4ee5\u6301\u6709\u5b83\u7684\u5f15\u7528\u3002<\/li>\n<li><strong>\u76f4\u63a5\u4f7f\u7528 <code>launch<\/code> \u8fd4\u56de\u7684 <code>Job<\/code><\/strong>\uff1a\u63a7\u5236\u7684\u662f<strong>\u5355\u4e2a\u534f\u7a0b<\/strong>\u3002<\/li>\n<li><strong>\u628a <code>Job<\/code> \u4f20\u7ed9 <code>CoroutineScope<\/code><\/strong>\uff1a\u8fd9\u4e2a <code>Job<\/code> \u5c31\u6210\u4e86 Scope \u7684\u201c\u6839\u201d\uff0c\u8c03\u7528\u5b83\u7684 <code>cancel()<\/code>\uff0c\u7b49\u4e8e<strong>\u4e00\u6b21\u6027\u53d6\u6d88\u6574\u4e2a Scope \u91cc\u6240\u6709\u7684\u534f\u7a0b<\/strong>\u3002<\/li>\n<\/ul>\n<h1><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E3%80%81Scope%E4%BC%A0%E5%85%A5Job%E4%BD%9C%E4%B8%BA%E6%A0%B9Job%E7%9A%84%E6%84%8F%E4%B9%89\"><\/span>\u4e8c\u3001Scope\u4f20\u5165Job\u4f5c\u4e3a\u6839Job\u7684\u610f\u4e49<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u7591\u95ee\uff1a\u4e00\u6b21\u6027\u53d6\u6d88\u6574\u4e2a Scope \u91cc\u6240\u6709\u7684\u534f\u7a0b\uff0c\u4e3a\u5565\u4e0d\u76f4\u63a5\u8c03\u7528Scope\u7684cancel\u65b9\u6cd5\u5462\uff1f<\/p>\n<p>\u60a8\u95ee\u5230\u4e86\u4e00\u4e2a\u975e\u5e38\u5173\u952e\u4e14\u7cbe\u5999\u7684\u8bbe\u8ba1\u70b9\uff01\u76f4\u63a5\u8c03\u7528 <code>Scope.cancel()<\/code> \u548c\u901a\u8fc7\u6839 <code>Job<\/code> \u53d6\u6d88\uff0c\u5728\u6548\u679c\u4e0a<strong>\u5b8c\u5168\u7b49\u4ef7<\/strong>\u3002\u4e4b\u6240\u4ee5\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u6838\u5fc3\u539f\u56e0\u5728\u4e8e<strong>\u8bbe\u8ba1\u7684\u7075\u6d3b\u6027\u548c\u5206\u5c42<\/strong>\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%92%A1_%E6%A0%B8%E5%BF%83%E7%AD%94%E6%A1%88%EF%BC%9A%E5%AE%83%E4%BB%AC%E6%98%AF%E5%90%8C%E4%B8%80%E4%B8%AA%E6%93%8D%E4%BD%9C%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%85%A5%E5%8F%A3\"><\/span>\ud83d\udca1 \u6838\u5fc3\u7b54\u6848\uff1a\u5b83\u4eec\u662f\u201c\u540c\u4e00\u4e2a\u64cd\u4f5c\u7684\u4e24\u4e2a\u5165\u53e3\u201d<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><code>Scope.cancel()<\/code> \u65b9\u6cd5\u5728\u5185\u90e8\u5b9e\u9645\u4e0a\u5c31\u662f\u8c03\u7528\u4e86\u5b83\u6301\u6709\u7684\u6839 <code>Job<\/code> \u7684 <code>cancel()<\/code> \u65b9\u6cd5\u3002\u6240\u4ee5\uff1a<\/p>\n<ul>\n<li><strong>\u76f4\u63a5\u8c03\u7528 <code>scope.cancel()<\/code><\/strong>\uff1a\u662f\u5b98\u65b9\u63a8\u8350\u7684\u3001\u6700\u76f4\u89c2\u7684\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u901a\u8fc7\u6839 <code>Job<\/code> \u53d6\u6d88<\/strong>\uff1a\u662f\u4e00\u79cd\u66f4\u5e95\u5c42\u3001\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u5c24\u5176\u5728\u9700\u8981<strong>\u7cbe\u7ec6\u63a7\u5236 Job \u751f\u547d\u5468\u671f<\/strong>\u6216<strong>\u89e3\u8026<\/strong>\u65f6\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u8bf4\u660e\uff0c\u8ba9\u6211\u4eec\u6df1\u5165\u4e00\u5c42\uff1a<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%94%8D_%E6%B7%B1%E5%85%A5%E4%B8%80%E5%B1%82%EF%BC%9AScope_%E5%92%8C_Job_%E7%9A%84%E5%85%B3%E7%B3%BB\"><\/span>\ud83d\udd0d \u6df1\u5165\u4e00\u5c42\uff1aScope \u548c Job \u7684\u5173\u7cfb<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5f53\u60a8\u521b\u5efa\u4e00\u4e2a <code>CoroutineScope<\/code> \u65f6\uff0c\u60a8\u5fc5\u987b\u7ed9\u5b83\u4e00\u4e2a <code>CoroutineContext<\/code>\uff0c\u800c\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d<strong>\u5fc5\u987b\u5305\u542b\u4e00\u4e2a <code>Job<\/code><\/strong>\u3002<\/p>\n<p>kotlin<\/p>\n<pre><code>\/\/ \u60a8\u521b\u5efa\u7684 Scope\uff0c\u5b83\u5185\u90e8\u6301\u6709\u4e86\u4e00\u4e2a Job\nval scope = CoroutineScope(Job() + Dispatchers.Main) <\/code><\/pre>\n<p>\u8fd9\u4e2a\u88ab\u4f20\u5165\u7684 <code>Job<\/code>\uff0c\u5c31\u662f\u8be5 Scope \u7684<strong>\u6839 Job<\/strong>\u3002<code>Scope<\/code> \u5bf9\u8c61\u672c\u8eab\u5e76\u4e0d\u5b9e\u73b0\u53d6\u6d88\u903b\u8f91\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u62e5\u6709 <code>coroutineContext<\/code> \u5c5e\u6027\u7684\u63a5\u53e3\u3002\u53d6\u6d88\u903b\u8f91\u662f\u7531<strong>Job<\/strong> \u5b9e\u73b0\u7684\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%8E%AF_%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%E7%9A%84%E5%AF%B9%E6%AF%94%E4%B8%8E%E9%80%89%E6%8B%A9\"><\/span>\ud83c\udfaf \u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4\u4e0e\u9009\u62e9<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u7ef4\u5ea6<\/th>\n<th style=\"text-align: left;\">\u65b9\u5f0f\u4e00\uff1a\u8c03\u7528 <code>scope.cancel()<\/code><\/th>\n<th style=\"text-align: left;\">\u65b9\u5f0f\u4e8c\uff1a\u8c03\u7528 <code>rootJob.cancel()<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><strong>\u8bed\u4e49\u6e05\u6670\u5ea6<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u6700\u9ad8<\/strong>\u3002<code>scope.cancel()<\/code> \u76f4\u63a5\u8868\u8fbe\u4e86\u201c\u53d6\u6d88\u8fd9\u4e2a\u4f5c\u7528\u57df\u91cc\u7684\u6240\u6709\u534f\u7a0b\u201d\u7684\u610f\u56fe\uff0c\u4ee3\u7801\u81ea\u89e3\u91ca\u6027\u6700\u5f3a\u3002<\/td>\n<td style=\"text-align: left;\">\u7a0d\u5f31\u3002<code>job.cancel()<\/code> \u672c\u8eab\u53ea\u8868\u8fbe\u201c\u53d6\u6d88\u8fd9\u4e2a Job\u201d\uff0c\u9700\u8981\u7ed3\u5408\u4e0a\u4e0b\u6587\u624d\u80fd\u7406\u89e3\u5b83\u5728\u53d6\u6d88\u6574\u4e2a\u4f5c\u7528\u57df\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u4f7f\u7528\u4fbf\u6377\u6027<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u6700\u65b9\u4fbf<\/strong>\u3002\u53ea\u8981\u6301\u6709 <code>scope<\/code> \u5f15\u7528\uff0c\u968f\u65f6\u53ef\u8c03\u7528\u3002<\/td>\n<td style=\"text-align: left;\">\u8f83\u7e41\u7410\u3002\u9700\u8981\u989d\u5916\u6301\u6709 <code>rootJob<\/code> \u7684\u5f15\u7528\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u8bbe\u8ba1\u7075\u6d3b\u6027<\/strong><\/td>\n<td style=\"text-align: left;\">\u8f83\u4f4e\u3002<code>scope<\/code> \u88ab\u9500\u6bc1\u65f6\uff0c\u5176\u5185\u90e8\u7684 <code>Job<\/code> \u901a\u5e38\u4e5f\u5e94\u968f\u4e4b\u53d6\u6d88\uff0c\u8fd9\u662f\u4e00\u79cd\u5f3a\u8026\u5408\u3002<\/td>\n<td style=\"text-align: left;\"><strong>\u6781\u9ad8<\/strong>\u3002<code>Job<\/code> \u7684\u751f\u547d\u5468\u671f\u53ef\u4ee5\u4e0e <code>Scope<\/code> \u89e3\u8026\u3002\u4e00\u4e2a <code>Job<\/code> \u53ef\u4ee5\u88ab\u591a\u4e2a <code>Scope<\/code> \u5171\u4eab\uff0c\u6216\u4f20\u9012\u7ed9\u5176\u4ed6\u7ec4\u4ef6\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%9A%80_%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%E9%80%9A%E8%BF%87%E6%A0%B9_Job_%E5%8F%96%E6%B6%88%E8%BF%99%E7%A7%8D%E7%81%B5%E6%B4%BB%E6%80%A7%EF%BC%9F\"><\/span>\ud83d\ude80 \u4e3a\u4ec0\u4e48\u9700\u8981\u201c\u901a\u8fc7\u6839 Job \u53d6\u6d88\u201d\u8fd9\u79cd\u7075\u6d3b\u6027\uff1f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6587\u6863\u4e2d\u7279\u610f\u63d0\u5230\u201c\u60a8\u8fd8\u53ef\u4ee5\u5c06 <code>Job<\/code> \u4f20\u9012\u7ed9 <code>CoroutineScope<\/code>\u201d\uff0c\u5c31\u662f\u4e3a\u4e86\u5e94\u5bf9<strong>\u66f4\u590d\u6742\u7684\u8bbe\u8ba1\u573a\u666f<\/strong>\u3002<\/p>\n<p><strong>\u573a\u666f\u4e00\uff1aJob \u5171\u4eab<\/strong><br \/>\n\u60a8\u53ef\u4ee5\u5728\u591a\u4e2a\u4f5c\u7528\u57df\u4e4b\u95f4\u5171\u4eab\u540c\u4e00\u4e2a\u6839 <code>Job<\/code>\u3002\u8fd9\u6837\uff0c\u53d6\u6d88\u8fd9\u4e00\u4e2a <code>Job<\/code>\uff0c\u5c31\u80fd\u540c\u65f6\u53d6\u6d88\u6240\u6709\u76f8\u5173\u4f5c\u7528\u57df\u91cc\u7684\u5168\u90e8\u534f\u7a0b\u3002\u8fd9\u662f <code>scope.cancel()<\/code> \u505a\u4e0d\u5230\u7684\u3002<\/p>\n<p>kotlin<\/p>\n<pre><code>\/\/ \u521b\u5efa\u4e00\u4e2a\u6839 Job\nval rootJob = Job()\n\n\/\/ \u521b\u5efa\u4e24\u4e2a\u4f5c\u7528\u57df\uff0c\u4f46\u5171\u4eab\u540c\u4e00\u4e2a\u6839 Job\nval scopeA = CoroutineScope(rootJob + Dispatchers.Main)\nval scopeB = CoroutineScope(rootJob + Dispatchers.IO)\n\nscopeA.launch { \/* \u4efb\u52a1 A1 *\/ }\nscopeA.launch { \/* \u4efb\u52a1 A2 *\/ }\nscopeB.launch { \/* \u4efb\u52a1 B1 *\/ }\n\n\/\/ \u4e00\u6b21\u53d6\u6d88\uff0c\u540c\u65f6\u53d6\u6d88 scopeA \u548c scopeB \u91cc\u7684\u6240\u6709\u534f\u7a0b\nrootJob.cancel() <\/code><\/pre>\n<p><strong>\u573a\u666f\u4e8c\uff1a\u751f\u547d\u5468\u671f\u89e3\u8026<\/strong><br \/>\n\u6709\u65f6\uff0c<code>Job<\/code> \u7684\u751f\u547d\u5468\u671f\u9700\u8981\u6bd4\u521b\u5efa\u5b83\u7684 <code>Scope<\/code> \u66f4\u957f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u80fd\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u521b\u5efa\u4e86\u4e00\u4e2a Scope\uff0c\u4f46\u5e0c\u671b\u5c06\u8fd9\u4e2a Scope \u5185\u542f\u52a8\u7684\u4e00\u4e2a<strong>\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u540e\u53f0\u4efb\u52a1<\/strong>\u7684 <code>Job<\/code> \u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\uff0c\u8ba9\u8c03\u7528\u65b9\u80fd\u72ec\u7acb\u4e8e Scope \u6765\u63a7\u5236\u5b83\u3002<\/p>\n<p>kotlin<\/p>\n<pre><code>fun startLongTask(): Job {\n    \/\/ \u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u4e00\u4e2a Scope\n    val scope = CoroutineScope(Dispatchers.Default)\n\n    \/\/ \u542f\u52a8\u4e00\u4e2a\u957f\u65f6\u95f4\u4efb\u52a1\uff0c\u5e76\u8fd4\u56de\u5b83\u7684 Job\n    val taskJob = scope.launch {\n        \/\/ \u6267\u884c\u5f88\u957f\u7684\u4efb\u52a1...\n    }\n\n    \/\/ \u51fd\u6570\u6267\u884c\u5b8c\u6bd5\uff0cscope \u672c\u8eab\u53ef\u80fd\u4e0d\u518d\u9700\u8981\uff0c\u4f46\u4efb\u52a1 Job \u88ab\u8fd4\u56de\u4e86\n    \/\/ \u8c03\u7528\u65b9\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u7684 taskJob \u5355\u72ec\u63a7\u5236\u8fd9\u4e2a\u4efb\u52a1\n    return taskJob \n    \/\/ \u6ce8\u610f\uff1a\u8fd9\u91cc scope \u6ca1\u6709\u53d6\u6d88\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u88ab\u6e05\u7406\uff0c\u8fd9\u4e0d\u662f\u597d\u5b9e\u8df5\uff0c\n    \/\/ \u53ea\u662f\u4e3a\u4e86\u8bf4\u660e Job \u53ef\u4ee5\u72ec\u7acb\u4e8e\u521b\u5efa\u5b83\u7684 Scope \u88ab\u4f20\u9012\u548c\u63a7\u5236\u3002\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%F0%9F%93%9D_%E6%80%BB%E7%BB%93%E4%B8%8E%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5\"><\/span>\ud83d\udcdd \u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u573a\u666f<\/th>\n<th style=\"text-align: left;\">\u63a8\u8350\u505a\u6cd5<\/th>\n<th style=\"text-align: left;\">\u539f\u56e0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><strong>\u65e5\u5e38\u5f00\u53d1\uff0c\u7ba1\u7406UI\u7ec4\u4ef6\u751f\u547d\u5468\u671f<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u76f4\u63a5\u8c03\u7528 <code>scope.cancel()<\/code><\/strong> (\u5982 <code>viewModelScope<\/code>\u3001<code>lifecycleScope<\/code>)<\/td>\n<td style=\"text-align: left;\">\u6700\u7b80\u5355\u3001\u6700\u76f4\u89c2\uff0c\u5b8c\u5168\u7b26\u5408\u7ed3\u6784\u5316\u5e76\u53d1\u7684\u539f\u5219\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u9700\u8981\u7cbe\u7ec6\u63a7\u5236\u591a\u4e2a\u76f8\u5173\u4f5c\u7528\u57df<\/strong><\/td>\n<td style=\"text-align: left;\">\u521b\u5efa\u5e76\u6301\u6709<strong>\u6839 <code>Job<\/code><\/strong>\uff0c\u901a\u8fc7\u5b83\u7edf\u4e00\u53d6\u6d88<\/td>\n<td style=\"text-align: left;\">\u63d0\u4f9b\u4e86\u8d85\u8d8a\u5355\u4e2a\u4f5c\u7528\u57df\u7684\u3001\u66f4\u5f3a\u5927\u7684\u63a7\u5236\u529b\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u9700\u8981\u5c06\u4efb\u52a1\u751f\u547d\u5468\u671f\u4e0e\u521b\u5efa\u8005\u89e3\u8026<\/strong><\/td>\n<td style=\"text-align: left;\">\u5c06\u4efb\u52a1\u81ea\u8eab\u7684 <code>Job<\/code> \u8fd4\u56de\u7ed9\u8c03\u7528\u65b9<\/td>\n<td style=\"text-align: left;\">\u5b9e\u73b0\u4e86\u66f4\u7075\u6d3b\u7684\u8bbe\u8ba1\uff0c\u8c03\u7528\u65b9\u53ef\u4ee5\u72ec\u7acb\u63a7\u5236\u8be5\u4efb\u52a1\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a<strong><code>scope.cancel()<\/code> \u662f\u5b98\u65b9\u63a8\u8350\u7684\u201c\u5feb\u6377\u65b9\u5f0f\u201d\uff0c\u800c\u901a\u8fc7\u6839 <code>Job<\/code> \u53d6\u6d88\u662f\u66f4\u5e95\u5c42\u3001\u66f4\u7075\u6d3b\u7684\u201c\u4e07\u80fd\u94a5\u5319\u201d\uff0c\u7528\u4e8e\u5e94\u5bf9\u66f4\u590d\u6742\u7684\u8bbe\u8ba1\u9700\u6c42\u3002<\/strong> <\/p>\n<button class=\"simplefavorite-button\" data-postid=\"16262\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u51fa\u5904\uff1ahttps:\/\/developer.android.google.cn\/kotlin\/co [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1254],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/16262"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16262"}],"version-history":[{"count":5,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/16262\/revisions"}],"predecessor-version":[{"id":16267,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/16262\/revisions\/16267"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}