{"id":826,"date":"2025-12-20T15:49:41","date_gmt":"2025-12-20T07:49:41","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=826"},"modified":"2025-12-20T15:49:41","modified_gmt":"2025-12-20T07:49:41","slug":"thread%e7%9a%84join%e6%96%b9%e6%b3%95_thread-join-2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/20\/thread%e7%9a%84join%e6%96%b9%e6%b3%95_thread-join-2\/","title":{"rendered":"Thread\u7684join\u65b9\u6cd5_thread.join"},"content":{"rendered":"\n<p><code>Thread<\/code> \u7684 <code>join()<\/code> \u65b9\u6cd5\u662f Python \u4e2d\u7528\u4e8e\u7ebf\u7a0b\u540c\u6b65\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u6765<strong>\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\uff0c\u76f4\u5230\u76ee\u6807\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210<\/strong>\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c<code>join()<\/code> \u65b9\u6cd5\u7528\u4e8e\u7b49\u5f85\u4e00\u4e2a\u7ebf\u7a0b\u7684\u5b8c\u6210\uff0c\u4ece\u800c\u4f7f\u4e3b\u7ebf\u7a0b\u80fd\u591f\u7b49\u5f85\u5de5\u4f5c\u7ebf\u7a0b\u7684\u7ed3\u675f\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <code>Thread.join()<\/code> \u7684\u57fa\u672c\u4f5c\u7528<\/h3>\n\n\n\n<p><code>join()<\/code> \u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u8ba9\u8c03\u7528\u6b64\u65b9\u6cd5\u7684\u7ebf\u7a0b\uff08\u901a\u5e38\u662f\u4e3b\u7ebf\u7a0b\uff09\u7b49\u5f85\u88ab\u8c03\u7528\u7ebf\u7a0b\u7684\u5b8c\u6210\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c<strong>\u5f53\u524d\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u76ee\u6807\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5<\/strong>\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u786e\u4fdd\u7ebf\u7a0b\u4e2d\u7684\u67d0\u4e9b\u4efb\u52a1\u5b8c\u6210\u540e\u518d\u6267\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <code>join()<\/code> \u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nthread.join(timeout=None)\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>timeout<\/strong>\uff1a\u662f\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u6700\u5927\u7b49\u5f85\u65f6\u95f4\u3002\u5982\u679c\u8d85\u65f6\uff0c<code>join()<\/code> \u4f1a\u8fd4\u56de\u5e76\u7ee7\u7eed\u6267\u884c\u3002\u5982\u679c\u6ca1\u6709\u4f20\u9012 <code>timeout<\/code> \u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u4f1a\u4e00\u76f4\u7b49\u5f85\u76ee\u6807\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u793a\u4f8b\uff1a\u57fa\u672c\u4f7f\u7528<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport threading\nimport time\n\n# \u76ee\u6807\u7ebf\u7a0b\u7684\u51fd\u6570\ndef worker():\n    print(&quot;Worker thread started&quot;)\n    time.sleep(2)\n    print(&quot;Worker thread finished&quot;)\n\n# \u521b\u5efa\u7ebf\u7a0b\nthread = threading.Thread(target=worker)\n\n# \u542f\u52a8\u7ebf\u7a0b\nthread.start()\n\n# \u4e3b\u7ebf\u7a0b\u7b49\u5f85worker\u7ebf\u7a0b\u5b8c\u6210\nthread.join()\n\nprint(&quot;Main thread finished&quot;)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWorker thread started\nWorker thread finished\nMain thread finished\n\n<\/pre><\/div>\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u5728\u8c03\u7528 <code>thread.join()<\/code> \u540e\u7b49\u5f85\uff0c\u76f4\u5230 <code>worker<\/code> \u7ebf\u7a0b\u5b8c\u6210\u6267\u884c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <code>join()<\/code> \u65b9\u6cd5\u7684\u5de5\u4f5c\u673a\u5236<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7ebf\u7a0b\u542f\u52a8<\/strong>\uff1a\u5f53\u8c03\u7528 <code>start()<\/code> \u65b9\u6cd5\u65f6\uff0c\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u76ee\u6807\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u7ebf\u7a0b\u8c03\u7528 <code>join()<\/code><\/strong>\uff1a\u4e3b\u7ebf\u7a0b\u8c03\u7528 <code>join()<\/code> \u540e\u4f1a\u8fdb\u5165<strong>\u7b49\u5f85\u72b6\u6001<\/strong>\uff0c\u76f4\u5230\u76ee\u6807\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u3002<\/li>\n\n\n\n<li><strong>\u7ebf\u7a0b\u5b8c\u6210<\/strong>\uff1a\u5f53\u76ee\u6807\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u6062\u590d\u6267\u884c\uff0c\u7ee7\u7eed\u8fdb\u884c\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u5e26\u6709\u8d85\u65f6\u53c2\u6570\u7684 <code>join()<\/code><\/h3>\n\n\n\n<p>\u5982\u679c\u6307\u5b9a\u4e86 <code>timeout<\/code> \u53c2\u6570\uff0c\u5219\u4e3b\u7ebf\u7a0b\u4f1a\u7b49\u5f85\u76ee\u6807\u7ebf\u7a0b\u5b8c\u6210\uff0c\u4f46\u6700\u591a\u7b49\u5f85\u6307\u5b9a\u7684\u65f6\u95f4\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport threading\nimport time\n\ndef worker():\n    print(&quot;Worker thread started&quot;)\n    time.sleep(5)\n    print(&quot;Worker thread finished&quot;)\n\nthread = threading.Thread(target=worker)\nthread.start()\n\n# \u4e3b\u7ebf\u7a0b\u6700\u591a\u7b49\u5f85 2 \u79d2\nthread.join(timeout=2)\n\n# \u5373\u4f7f\u7ebf\u7a0b\u8fd8\u6ca1\u6267\u884c\u5b8c\uff0c\u4e3b\u7ebf\u7a0b\u4e5f\u4f1a\u7ee7\u7eed\u6267\u884c\nprint(&quot;Main thread finished&quot;)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWorker thread started\nMain thread finished\nWorker thread finished\n\n<\/pre><\/div>\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u53ea\u7b49\u5f85\u4e86 2 \u79d2\uff0c\u4e4b\u540e\u5373\u4f7f\u76ee\u6807\u7ebf\u7a0b\u8fd8\u5728\u6267\u884c\uff0c\u5b83\u4e5f\u4f1a\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>join()<\/code> \u4f1a\u5bfc\u81f4\u4e3b\u7ebf\u7a0b\u6216\u8c03\u7528\u7ebf\u7a0b\u963b\u585e\uff0c\u76f4\u5230\u76ee\u6807\u7ebf\u7a0b\u7ed3\u675f\u3002<\/li>\n\n\n\n<li>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u8c03\u7528 <code>join()<\/code> \u662f\u5e38\u7528\u7684\u7ebf\u7a0b\u540c\u6b65\u65b9\u6cd5\uff0c\u4f46\u9700\u8981\u907f\u514d\u6b7b\u9501\u6216\u4e0d\u5fc5\u8981\u7684\u7b49\u5f85\u3002<\/li>\n\n\n\n<li><code>join()<\/code> \u901a\u5e38\u7528\u4e8e<strong>\u786e\u4fdd\u7ebf\u7a0b\u5728\u9000\u51fa\u524d\u5b8c\u6210\u4efb\u52a1<\/strong>\uff0c\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u5e0c\u671b\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u67d0\u4e9b\u8ba1\u7b97\u6216\u8d44\u6e90\u6e05\u7406\u540e\uff0c\u4e3b\u7ebf\u7a0b\u518d\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u591a\u4e2a\u7ebf\u7a0b\u7684 <code>join()<\/code> \u4f7f\u7528<\/h3>\n\n\n\n<p>\u5982\u679c\u6709\u591a\u4e2a\u7ebf\u7a0b\u9700\u8981\u6267\u884c\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u6309\u987a\u5e8f\u8c03\u7528\u6bcf\u4e2a\u7ebf\u7a0b\u7684 <code>join()<\/code> \u6765\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport threading\nimport time\n\ndef worker(n):\n    print(f&quot;Worker {n} started&quot;)\n    time.sleep(2)\n    print(f&quot;Worker {n} finished&quot;)\n\nthreads = &#x5B;]\nfor i in range(3):\n    thread = threading.Thread(target=worker, args=(i,))\n    threads.append(thread)\n    thread.start()\n\n# \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\nfor thread in threads:\n    thread.join()\n\nprint(&quot;All threads finished&quot;)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWorker 0 started\nWorker 1 started\nWorker 2 started\nWorker 0 finished\nWorker 1 finished\nWorker 2 finished\nAll threads finished\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>join()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u7ebf\u7a0b\u5728\u6267\u884c\u5b8c\u6210\u4e4b\u524d\u4e0d\u4f1a\u88ab\u4e3b\u7ebf\u7a0b\u8df3\u8fc7\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>join()<\/code> \u4f7f\u5f97\u7ebf\u7a0b\u80fd\u591f\u540c\u6b65\u6267\u884c\uff0c\u7279\u522b\u662f\u5728\u591a\u4e2a\u7ebf\u7a0b\u9700\u8981\u5b8c\u6210\u4efb\u52a1\u4e4b\u540e\u624d\u80fd\u8fdb\u884c\u67d0\u4e9b\u540e\u7eed\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u8d85\u65f6\u53c2\u6570\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u957f\u65f6\u95f4\u7b49\u5f85\u800c\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5361\u6b7b\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f60\u6709\u4ec0\u4e48\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u6216\u8005\u95ee\u9898\uff0c\u60f3\u8fdb\u4e00\u6b65\u63a2\u8ba8 <code>join()<\/code> \u65b9\u6cd5\u7684\u4f7f\u7528\u5417\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thread \u7684 join() \u65b9\u6cd5\u662f Python \u4e2d\u7528\u4e8e\u7ebf\u7a0b\u540c\u6b65\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/20\/thread%e7%9a%84join%e6%96%b9%e6%b3%95_thread-join-2\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-826","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=826"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/826\/revisions"}],"predecessor-version":[{"id":827,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/826\/revisions\/827"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}