{"id":40333,"date":"2024-11-26T11:59:14","date_gmt":"2024-11-26T03:59:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40333\/"},"modified":"2024-11-26T11:59:14","modified_gmt":"2024-11-26T03:59:14","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:\/\/fwq.ai\/blog\/40333\/","title":{"rendered":"\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f"},"content":{"rendered":"<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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173232216325108.jpg\" class=\"aligncenter\" title=\"\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f\u63d2\u56fe\" alt=\"\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f\u63d2\u56fe\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3002\u68c0\u67e5\u7ebf\u7a0b\u662f\u5426\u5b8c\u6210\u7684\u65b9\u6cd5\uff1a isalive() \u5982\u679c\u7ebf\u7a0b\u4ecd\u5728\u8fd0\u884c\u5219\u8fd4\u56detrue\uff1b\u5426\u5219\uff0c\u8fd4\u56de false\u3002 \u7528\u4e8e\u6301\u7eed\u68c0\u67e5\u7ebf\u7a0b\u7684\u72b6\u6001\u3002 \u52a0\u5165() \u4f7f\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u7ebf\u7a0b\u7b49\u5f85\uff0c\u76f4\u5230\u6307\u5b9a\u7ebf\u7a0b\u5b8c\u6210\u3002 \u6709\u591a\u79cd\u53d8\u4f53\u5141\u8bb8\u60a8\u5b9a\u4e49\u6700\u957f\u7b49\u5f85\u65f6\u95f4\u3002 2\u3002\u4f7f\u7528 isalive() \u7684\u793a\u4f8b\uff1a \/\/ verifica se as threads est\u00e3o vivas class mythread implements runnable { thread thrd; mythread(string name) { thrd = new thread(this, name); thrd.start(); \/\/ inicia a thread } public void run() { system.out.println(thrd.getname() + &#8221; starting.&#8221;); try { for (int count = 0; count [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-40333","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=40333"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40333\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}