{"id":203801,"date":"2025-05-22T14:48:53","date_gmt":"2025-05-22T06:48:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203801\/"},"modified":"2025-05-22T14:48:53","modified_gmt":"2025-05-22T06:48:53","slug":"%e7%a1%ae%e5%ae%9a%e7%ba%bf%e7%a8%8b%e4%bd%95%e6%97%b6%e7%bb%93%e6%9d%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203801\/","title":{"rendered":"\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><strong>1\u3002\u68c0\u67e5\u7ebf\u7a0b\u662f\u5426\u5b8c\u6210\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>isalive()<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u7ebf\u7a0b\u4ecd\u5728\u8fd0\u884c\u5219\u8fd4\u56detrue\uff1b\u5426\u5219\uff0c\u8fd4\u56de false\u3002<\/li>\n<li>\u7528\u4e8e\u6301\u7eed\u68c0\u67e5\u7ebf\u7a0b\u7684\u72b6\u6001\u3002<\/li>\n<\/ul>\n<p><strong>\u52a0\u5165()<\/strong><\/p>\n<ul>\n<li>\u4f7f\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u7ebf\u7a0b\u7b49\u5f85\uff0c\u76f4\u5230\u6307\u5b9a\u7ebf\u7a0b\u5b8c\u6210\u3002<\/li>\n<li>\u6709\u591a\u79cd\u53d8\u4f53\u5141\u8bb8\u60a8\u5b9a\u4e49\u6700\u957f\u7b49\u5f85\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><strong>2\u3002\u4f7f\u7528 isalive() \u7684\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ verifica se as threads est\u00e3o vivas\nclass mythread implements runnable {\n    thread thrd;\n\n    mythread(string name) {\n        thrd = new thread(this, name);\n        thrd.start(); \/\/ inicia a thread\n    }\n\n    public void run() {\n        system.out.println(thrd.getname() + \" starting.\");\n        try {\n            for (int count = 0; count &lt; 10; count++) {\n                thread.sleep(400); \/\/ suspende a execu\u00e7\u00e3o por 400ms\n                system.out.println(\"in \" + thrd.getname() + \", count is \" + count);\n            }\n        } catch (interruptedexception exc) {\n            system.out.println(thrd.getname() + \" interrupted.\");\n        }\n        system.out.println(thrd.getname() + \" terminating.\");\n    }\n}\n\nclass morethreads {\n    public static void main(string[] args) {\n        system.out.println(\"main thread starting.\");\n        mythread mt1 = new mythread(\"child #1\");\n        mythread mt2 = new mythread(\"child #2\");\n        mythread mt3 = new mythread(\"child #3\");\n\n        \/\/ verifica se as threads ainda est\u00e3o vivas\n        do {\n            system.out.print(\".\");\n            try {\n                thread.sleep(100);\n            } catch (interruptedexception exc) {\n                system.out.println(\"main thread interrupted.\");\n            }\n        } while (mt1.thrd.isalive() || mt2.thrd.isalive() || mt3.thrd.isalive());\n\n        system.out.println(\"main thread ending.\");\n    }\n}\n\n<\/pre>\n<p><strong>\u91cd\u8981\u8981\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e3b\u7ebf\u7a0b\u4e0d\u65ad\u68c0\u67e5 isalive() \u76f4\u5230\u6240\u6709\u5b50\u7ebf\u7a0b\u90fd\u5b8c\u6210\u3002<\/li>\n<li>\u7ebf\u7a0b\u7684\u6267\u884c\u662f\u7531 java \u8c03\u5ea6\u7684\uff0c\u56e0\u6b64\u786e\u5207\u7684\u8f93\u51fa\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/li>\n<\/ul>\n<p><strong>3\u3002\u4f7f\u7528 join() \u7684\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ aguarda o t\u00e9rmino das threads com join()\nclass mythread implements runnable {\n    thread thrd;\n\n    mythread(string name) {\n        thrd = new thread(this, name);\n        thrd.start(); \/\/ inicia a thread\n    }\n\n    public void run() {\n        system.out.println(thrd.getname() + \" starting.\");\n        try {\n            for (int count = 0; count &lt; 10; count++) {\n                thread.sleep(400); \/\/ suspende a execu\u00e7\u00e3o por 400ms\n                system.out.println(\"in \" + thrd.getname() + \", count is \" + count);\n            }\n        } catch (interruptedexception exc) {\n            system.out.println(thrd.getname() + \" interrupted.\");\n        }\n        system.out.println(thrd.getname() + \" terminating.\");\n    }\n}\n\nclass jointhreads {\n    public static void main(string[] args) {\n        system.out.println(\"main thread starting.\");\n        mythread mt1 = new mythread(\"child #1\");\n        mythread mt2 = new mythread(\"child #2\");\n        mythread mt3 = new mythread(\"child #3\");\n\n        try {\n            mt1.thrd.join(); \/\/ aguarda child #1 terminar\n            system.out.println(\"child #1 joined.\");\n            mt2.thrd.join(); \/\/ aguarda child #2 terminar\n            system.out.println(\"child #2 joined.\");\n            mt3.thrd.join(); \/\/ aguarda child #3 terminar\n            system.out.println(\"child #3 joined.\");\n        } catch (interruptedexception exc) {\n            system.out.println(\"main thread interrupted.\");\n        }\n\n        system.out.println(\"main thread ending.\");\n    }\n}\n\n<\/pre>\n<p>\u9884\u671f\u8f93\u51fa\uff08\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff09\uff1a<\/p>\n<pre>Main thread starting.\nChild #1 starting.\nChild #2 starting.\nChild #3 starting.\nIn Child #1, count is 0\nIn Child #2, count is 0\nIn Child #3, count is 0\n...\nChild #1 terminating.\nChild #1 joined.\nChild #2 terminating.\nChild #2 joined.\nChild #3 terminating.\nChild #3 joined.\nMain thread ending.\n\n<\/pre>\n<p><strong>\u91cd\u8981\u8981\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>join() \u786e\u4fdd\u4e3b\u7ebf\u7a0b\u4ec5\u5728\u5b50\u7ebf\u7a0b\u5b8c\u6210\u540e\u7ee7\u7eed\u3002<\/li>\n<li>\u8f93\u51fa\u663e\u793a\u6bcf\u4e2a\u7ebf\u7a0b\u6309\u9884\u671f\u987a\u5e8f\u7ec8\u6b62\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/173251661467441b067b558.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f \u6536\u85cf \u5b66\u4e60\u6587\u7ae0&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203801","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203801"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203801\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}