{"id":6666,"date":"2024-12-19T20:41:20","date_gmt":"2024-12-19T12:41:20","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6666"},"modified":"2024-12-19T20:41:20","modified_gmt":"2024-12-19T12:41:20","slug":"%e7%bc%96%e7%a8%8b%e8%af%ad%e8%a8%80swift-unstructured-concurrency-%e5%92%8c-structured-concurrency","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6666","title":{"rendered":"[\u7f16\u7a0b\u8bed\u8a00]Swift Unstructured Concurrency \u548c  structured Concurrency"},"content":{"rendered":"<p><strong>Unstructured Concurrency\uff08\u975e\u7ed3\u6784\u5316\u5e76\u53d1\uff09<\/strong> \u548c <strong>Structured Concurrency\uff08\u7ed3\u6784\u5316\u5e76\u53d1\uff09<\/strong> \u662f\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u4e24\u79cd\u4e0d\u540c\u7684\u6a21\u578b\uff0c\u5b83\u4eec\u5728\u4efb\u52a1\u7ba1\u7406\u3001\u751f\u547d\u5468\u671f\u548c\u9519\u8bef\u5904\u7406\u4e0a\u6709\u663e\u8457\u7684\u533a\u522b\u3002\u4e0b\u9762\u662f\u5b83\u4eec\u7684\u4e3b\u8981\u533a\u522b\uff1a<\/p>\n<h3>1. <strong>\u4efb\u52a1\u751f\u547d\u5468\u671f\u7ba1\u7406<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>Unstructured Concurrency<\/strong>\uff08\u975e\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u5728\u975e\u7ed3\u6784\u5316\u5e76\u53d1\u6a21\u578b\u4e2d\uff0c\u4efb\u52a1\u7684\u751f\u547d\u5468\u671f\u7531\u5f00\u53d1\u8005\u624b\u52a8\u7ba1\u7406\uff0c\u4efb\u52a1\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u70b9\u542f\u52a8\u548c\u7ed3\u675f\u3002<\/li>\n<li>\u5f00\u53d1\u8005\u9700\u8981\u81ea\u5df1\u786e\u4fdd\u4efb\u52a1\u7684\u6b63\u786e\u5b8c\u6210\uff0c\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3001\u4efb\u52a1\u672a\u5b8c\u6210\u6216\u8005\u672a\u6b63\u786e\u53d6\u6d88\u7684\u60c5\u51b5\u3002<\/li>\n<li>\u4f8b\u5982\uff0c\u5728\u4f7f\u7528 <code>DispatchQueue<\/code> \u6216 <code>Thread<\/code> \u65f6\uff0c\u4efb\u52a1\u53ef\u4ee5\u542f\u52a8\u5e76\u72ec\u7acb\u8fd0\u884c\uff0c\u76f4\u5230\u5b83\u4eec\u88ab\u663e\u5f0f\u5730\u53d6\u6d88\u6216\u5b8c\u6210\u3002\u8fd9\u79cd\u65b9\u5f0f\u5bb9\u6613\u5bfc\u81f4\u4e00\u4e9b\u672a\u5904\u7406\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u4eec\u6ca1\u6709\u660e\u786e\u7684\u4e0a\u4e0b\u6587\u6765\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Structured Concurrency<\/strong>\uff08\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u5728\u7ed3\u6784\u5316\u5e76\u53d1\u6a21\u578b\u4e2d\uff0c\u4efb\u52a1\u7684\u751f\u547d\u5468\u671f\u7531\u5176\u7236\u4efb\u52a1\u7ba1\u7406\u3002\u4efb\u52a1\u4f1a\u5728\u5176\u7236\u4efb\u52a1\u7684\u8303\u56f4\u5185\u521b\u5efa\uff0c\u7236\u4efb\u52a1\u8d1f\u8d23\u7b49\u5f85\u5b50\u4efb\u52a1\u7684\u5b8c\u6210\uff0c\u786e\u4fdd\u6240\u6709\u4efb\u52a1\u5728\u7236\u4efb\u52a1\u7ed3\u675f\u524d\u5b8c\u6210\u3002<\/li>\n<li>\u7ed3\u6784\u5316\u5e76\u53d1\u901a\u8fc7\u8fd9\u79cd\u660e\u786e\u7684\u7ed3\u6784\u6765\u907f\u514d\u4efb\u52a1\u60ac\u7a7a\u6216\u9057\u6f0f\uff0c\u4fdd\u8bc1\u4efb\u52a1\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u7684\u8d44\u6e90\u7ba1\u7406\u3002<\/li>\n<li>\u4f8b\u5982\uff0cSwift \u7684 <code>TaskGroup<\/code> \u548c <code>actor<\/code> \u5c31\u662f\u7ed3\u6784\u5316\u5e76\u53d1\u7684\u793a\u4f8b\u3002\u4efb\u52a1\u5728 <code>TaskGroup<\/code> \u6216 <code>actor<\/code> \u4e2d\u7684\u4f5c\u7528\u57df\u5185\u88ab\u7ba1\u7406\uff0c\u7236\u4efb\u52a1\u4f1a\u7b49\u5f85\u5b50\u4efb\u52a1\u7684\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>2. <strong>\u9519\u8bef\u5904\u7406<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>Unstructured Concurrency<\/strong>\uff08\u975e\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u5728\u975e\u7ed3\u6784\u5316\u5e76\u53d1\u4e2d\uff0c\u4efb\u52a1\u4e4b\u95f4\u7684\u9519\u8bef\u4f20\u64ad\u548c\u6355\u83b7\u53ef\u80fd\u6bd4\u8f83\u6df7\u4e71\uff0c\u5f00\u53d1\u8005\u9700\u8981\u624b\u52a8\u5904\u7406\u9519\u8bef\u5e76\u786e\u4fdd\u4efb\u52a1\u95f4\u7684\u5f02\u5e38\u4e0d\u4f1a\u5bfc\u81f4\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u4efb\u52a1\u53ef\u80fd\u4f1a\u5728\u4e0d\u9884\u671f\u7684\u60c5\u51b5\u4e0b\u5931\u8d25\uff0c\u5e76\u4e14\u5f02\u5e38\u53ef\u80fd\u88ab\u9057\u6f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Structured Concurrency<\/strong>\uff08\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u7ed3\u6784\u5316\u5e76\u53d1\u901a\u8fc7\u5c06\u4efb\u52a1\u9650\u5236\u5728\u7236\u4efb\u52a1\u7684\u4f5c\u7528\u57df\u5185\u6765\u7b80\u5316\u9519\u8bef\u5904\u7406\u3002\u7236\u4efb\u52a1\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u5b50\u4efb\u52a1\u7684\u9519\u8bef\uff0c\u6216\u8005\u901a\u8fc7\u4f20\u9012\u9519\u8bef\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li>\u56e0\u4e3a\u4efb\u52a1\u5728\u660e\u786e\u7684\u7ed3\u6784\u4e2d\u8fdb\u884c\u7ba1\u7406\uff0c\u6240\u4ee5\u9519\u8bef\u5904\u7406\u66f4\u52a0\u96c6\u4e2d\u548c\u53ef\u9760\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>TaskGroup<\/code>\uff0c\u7236\u4efb\u52a1\u53ef\u4ee5\u7b49\u5f85\u5e76\u5904\u7406\u5b50\u4efb\u52a1\u7684\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3. <strong>\u8d44\u6e90\u7ba1\u7406<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>Unstructured Concurrency<\/strong>\uff08\u975e\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u8d44\u6e90\u7ba1\u7406\u66f4\u4f9d\u8d56\u5f00\u53d1\u8005\u624b\u52a8\u7ba1\u7406\u3002\u5f00\u53d1\u8005\u9700\u8981\u786e\u4fdd\u8d44\u6e90\uff08\u5982\u5185\u5b58\u3001\u6587\u4ef6\u53e5\u67c4\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\uff09\u88ab\u6b63\u786e\u91ca\u653e\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u6216\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n<li>\u5728\u975e\u7ed3\u6784\u5316\u5e76\u53d1\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u6ca1\u6709\u663e\u5f0f\u53d6\u6d88\u6216\u5b8c\u6210\uff0c\u76f8\u5173\u7684\u8d44\u6e90\u53ef\u80fd\u4f1a\u957f\u671f\u5360\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Structured Concurrency<\/strong>\uff08\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u7531\u4e8e\u4efb\u52a1\u751f\u547d\u5468\u671f\u88ab\u660e\u786e\u7ba1\u7406\uff0c\u8d44\u6e90\u4e5f\u88ab\u66f4\u597d\u5730\u63a7\u5236\u3002\u5728\u7ed3\u6784\u5316\u5e76\u53d1\u4e2d\uff0c\u7236\u4efb\u52a1\u4f1a\u7b49\u5f85\u5b50\u4efb\u52a1\u7684\u5b8c\u6210\uff0c\u8fd9\u4f7f\u5f97\u4efb\u52a1\u548c\u8d44\u6e90\u7684\u7ba1\u7406\u66f4\u5177\u53ef\u63a7\u6027\u548c\u5b89\u5168\u6027\u3002<\/li>\n<li>\u5f53\u4efb\u52a1\u5b8c\u6210\u65f6\uff0c\u76f8\u5173\u7684\u8d44\u6e90\u4e5f\u4f1a\u88ab\u6b63\u786e\u91ca\u653e\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>4. <strong>\u5e76\u53d1\u4efb\u52a1\u7684\u53ef\u89c1\u6027<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>Unstructured Concurrency<\/strong>\uff08\u975e\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u5728\u975e\u7ed3\u6784\u5316\u5e76\u53d1\u4e2d\uff0c\u4efb\u52a1\u7684\u542f\u52a8\u548c\u6267\u884c\u5e38\u5e38\u662f\u5206\u6563\u7684\uff0c\u4efb\u52a1\u4e4b\u95f4\u6ca1\u6709\u660e\u786e\u7684\u5173\u7cfb\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e00\u4e9b\u4efb\u52a1\u65e0\u6cd5\u88ab\u8ddf\u8e2a\u3002<\/li>\n<li>\u4efb\u52a1\u53ef\u80fd\u662f\u5728\u4efb\u4f55\u5730\u65b9\u542f\u52a8\u7684\uff0c\u65e0\u6cd5\u786e\u4fdd\u5b83\u4eec\u4f1a\u5728\u7a0b\u5e8f\u7684\u67d0\u4e2a\u7279\u5b9a\u70b9\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Structured Concurrency<\/strong>\uff08\u7ed3\u6784\u5316\u5e76\u53d1\uff09\uff1a<\/p>\n<ul>\n<li>\u5728\u7ed3\u6784\u5316\u5e76\u53d1\u4e2d\uff0c\u4efb\u52a1\u662f\u6709\u7236\u5b50\u5173\u7cfb\u7684\uff0c\u6bcf\u4e2a\u4efb\u52a1\u7684\u751f\u547d\u5468\u671f\u90fd\u660e\u786e\u7531\u7236\u4efb\u52a1\u7ba1\u7406\u3002\u56e0\u6b64\uff0c\u4efb\u52a1\u7684\u53ef\u89c1\u6027\u548c\u7ba1\u7406\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<li>\u5b50\u4efb\u52a1\u53ea\u5728\u7236\u4efb\u52a1\u7684\u4f5c\u7528\u57df\u5185\u6709\u6548\uff0c\u786e\u4fdd\u4e86\u4efb\u52a1\u7684\u987a\u5e8f\u548c\u6267\u884c\u72b6\u6001\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>5. <strong>\u793a\u4f8b<\/strong><\/h3>\n<ul>\n<li><strong>Unstructured Concurrency \u793a\u4f8b\uff08\u975e\u7ed3\u6784\u5316\u5e76\u53d1\uff09<\/strong>\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">\/\/ \u4f7f\u7528 DispatchQueue \u542f\u52a8\u5f02\u6b65\u4efb\u52a1\nDispatchQueue.global().async {\n    \/\/ \u5f02\u6b65\u4efb\u52a1\n    print(&quot;Task 1&quot;)\n}\n\nDispatchQueue.global().async {\n    \/\/ \u53e6\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\n    print(&quot;Task 2&quot;)\n}<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Task 1<\/code> \u548c <code>Task 2<\/code> \u88ab\u72ec\u7acb\u5730\u542f\u52a8\uff0c\u4e14\u6ca1\u6709\u7236\u4efb\u52a1\u6765\u7ba1\u7406\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\uff0c\u5b83\u4eec\u7684\u6267\u884c\u987a\u5e8f\u548c\u5b8c\u6210\u60c5\u51b5\u65e0\u6cd5\u786e\u4fdd\u3002<\/p>\n<ul>\n<li><strong>Structured Concurrency \u793a\u4f8b\uff08\u7ed3\u6784\u5316\u5e76\u53d1\uff09<\/strong>\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">import Foundation\n\nfunc fetchData() async -&gt; String {\n    return &quot;Data Loaded&quot;\n}\n\nfunc performTasks() async {\n    \/\/ \u521b\u5efa\u4e00\u4e2a TaskGroup\n    await withTaskGroup(of: String.self) { group in\n        group.addTask {\n            await fetchData() \/\/ \u5f02\u6b65\u4efb\u52a1 1\n        }\n        group.addTask {\n            await fetchData() \/\/ \u5f02\u6b65\u4efb\u52a1 2\n        }\n\n        for await result in group {\n            print(result)\n        }\n    }\n}\n\nTask {\n    await performTasks()\n}<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u4efb\u52a1\u90fd\u5728 <code>TaskGroup<\/code> \u4e2d\u7ba1\u7406\uff0c\u786e\u4fdd\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u7531\u7236\u4efb\u52a1\uff08<code>performTasks<\/code>\uff09\u7ba1\u7406\u3002\u6240\u6709\u5b50\u4efb\u52a1\u90fd\u4f1a\u5728\u7236\u4efb\u52a1\u7ed3\u675f\u524d\u5b8c\u6210\uff0c\u786e\u4fdd\u4e86\u4efb\u52a1\u7684\u53ef\u8ffd\u8e2a\u6027\u548c\u8d44\u6e90\u7684\u5408\u7406\u91ca\u653e\u3002<\/p>\n<h3><strong>\u603b\u7ed3<\/strong>\uff1a<\/h3>\n<ul>\n<li><strong>\u975e\u7ed3\u6784\u5316\u5e76\u53d1<\/strong>\uff1a\u4efb\u52a1\u7684\u751f\u547d\u5468\u671f\u4e0d\u53d7\u7236\u4efb\u52a1\u7ba1\u7406\uff0c\u5f00\u53d1\u8005\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5e76\u53d1\u4efb\u52a1\uff0c\u5bb9\u6613\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u548c\u9519\u8bef\u5904\u7406\u4e0d\u5f53\u3002<\/li>\n<li><strong>\u7ed3\u6784\u5316\u5e76\u53d1<\/strong>\uff1a\u4efb\u52a1\u5728\u7236\u4efb\u52a1\u7684\u4f5c\u7528\u57df\u5185\u8fdb\u884c\u7ba1\u7406\uff0c\u786e\u4fdd\u4efb\u52a1\u6709\u660e\u786e\u7684\u751f\u547d\u5468\u671f\uff0c\u7236\u4efb\u52a1\u8d1f\u8d23\u7b49\u5f85\u6240\u6709\u5b50\u4efb\u52a1\u5b8c\u6210\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u53ef\u9760\u6027\u548c\u6613\u4e8e\u7ba1\u7406\u7684\u5e76\u53d1\u6a21\u578b\u3002<\/li>\n<\/ul>\n<p>\u7ed3\u6784\u5316\u5e76\u53d1\u901a\u8fc7\u7b80\u5316\u4efb\u52a1\u7ba1\u7406\u548c\u9519\u8bef\u5904\u7406\uff0c\u63d0\u4f9b\u4e86\u6bd4\u975e\u7ed3\u6784\u5316\u5e76\u53d1\u66f4\u52a0\u53ef\u9760\u548c\u53ef\u7ef4\u62a4\u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unstructured Concurrency\uff08\u975e\u7ed3\u6784\u5316\u5e76\u53d1\uff09 \u548c Structured Concurren&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6666\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-6666","post","type-post","status-publish","format-standard","hentry","category-135"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6666"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6666\/revisions"}],"predecessor-version":[{"id":6667,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6666\/revisions\/6667"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}