{"id":40324,"date":"2024-11-26T17:10:34","date_gmt":"2024-11-26T09:10:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40324\/"},"modified":"2024-11-26T17:10:34","modified_gmt":"2024-11-26T09:10:34","slug":"%e7%bb%83%e4%b9%a0%e5%b0%9d%e8%af%95%e8%bf%99%e4%b8%aa%e6%89%a9%e5%b1%95%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40324\/","title":{"rendered":"\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173206246170211.jpg\" class=\"aligncenter\" title=\"\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b\u63d2\u56fe\" alt=\"\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u672c\u7ec3\u4e60\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u901a\u8fc7\u76f4\u63a5\u6269\u5c55 thread \u7c7b\uff08\u800c\u4e0d\u662f\u5b9e\u73b0 runnable \u63a5\u53e3\uff09\u6765\u5728 java \u4e2d\u521b\u5efa\u7ebf\u7a0b\u3002\u901a\u8fc7\u8fd9\u6837\u505a\uff0c\u60a8\u7684\u7c7b\u7ee7\u627f\u4e86 thread \u65b9\u6cd5\uff0c\u8fd9\u4f7f\u5f97\u76f4\u63a5\u64cd\u4f5c\u7ebf\u7a0b\u53d8\u5f97\u66f4\u5bb9\u6613\uff0c\u800c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u5355\u72ec\u7684\u7ebf\u7a0b\u3002<\/p>\n<p><strong>\u953b\u70bc\u6b65\u9aa4<\/strong><\/p>\n<p><strong>\u6269\u5c55 thread \u7c7b\uff1a<\/strong><br \/> \u4f60\u7684\u7c7b\u5fc5\u987b\u7ee7\u627f\u81ea thread \u5e76\u91cd\u5199 run() \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u7c7b\u6784\u9020\u51fd\u6570\uff1a<\/strong><br \/> \u4f7f\u7528 super(name) \u6784\u9020\u51fd\u6570\u4e3a\u7ebf\u7a0b\u547d\u540d\uff0c\u5e76\u901a\u8fc7\u76f4\u63a5\u8c03\u7528 start() \u5f00\u59cb\u6267\u884c\u3002<\/p>\n<p><strong>\u91cd\u5199 run() \u65b9\u6cd5\uff1a<\/strong><br \/> \u8be5\u65b9\u6cd5\u5b9a\u4e49\u4e86\u7ebf\u7a0b\u7684\u884c\u4e3a\u3002\u5728\u8fd9\u91cc\uff0c\u7ebf\u7a0b\u6253\u5370\u5b83\u7684\u540d\u79f0\u5e76\u8fd0\u884c\u4e00\u4e2a\u5faa\u73af\u3002<br \/> \u5b8c\u6574\u7684\u529f\u80fd\u793a\u4f8b\uff1a<\/p>\n<p>\u4e0b\u9762\u662f\u4ee3\u7801\uff1a<\/p>\n<pre>\/\/ define a classe que estende thread\nclass mythread extends thread {\n    \/\/ constr\u00f3i uma nova thread\n    mythread(string name) {\n        super(name); \/\/ nomeia a thread\n        start();     \/\/ inicia a thread\n    }\n\n    \/\/ come\u00e7a a execu\u00e7\u00e3o da nova thread\n    public void run() {\n        system.out.println(getname() + \" starting.\");\n        try {\n            for (int count = 0; count &lt; 10; count++) {\n                thread.sleep(400); \/\/ pausa por 400ms\n                system.out.println(\"in \" + getname() + \", count is \" + count);\n            }\n        } catch (interruptedexception exc) {\n            system.out.println(getname() + \" interrupted.\");\n        }\n        system.out.println(getname() + \" terminating.\");\n    }\n}\n\n\/\/ classe principal para demonstrar a execu\u00e7\u00e3o das threads\nclass extendthread {\n    public static void main(string[] args) {\n        system.out.println(\"main thread starting.\");\n\n        \/\/ cria uma nova thread\n        mythread mt = new mythread(\"child #1\");\n\n        \/\/ executa a thread principal\n        for (int i = 0; i &lt; 50; i++) {\n            system.out.print(\".\");\n            try {\n                thread.sleep(100); \/\/ pausa por 100ms\n            } catch (interruptedexception exc) {\n                system.out.println(\"main thread interrupted.\");\n            }\n        }\n\n        system.out.println(\"main thread ending.\");\n    }\n}\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4ee3\u7801\u5982\u4f55\u5de5\u4f5c<\/strong><br \/><strong>\u8f85\u52a9\u7ebf\u7a0b\u521b\u5efa\uff1a<\/strong><br \/> \u201cchild #1\u201d\u7ebf\u7a0b\u88ab\u521b\u5efa\u5e76\u901a\u8fc7 start() \u7acb\u5373\u542f\u52a8\u3002<\/p>\n<p><strong>\u8f85\u52a9\u7ebf\u7a0b\u6267\u884c\uff1a<\/strong><br \/> \u8be5\u7ebf\u7a0b\u6267\u884c\u4e00\u4e2a\u5faa\u73af\uff0c\u6253\u5370\u6d88\u606f\u5e76\u5728\u8fed\u4ee3\u4e4b\u95f4\u6682\u505c 400 \u6beb\u79d2\u3002<\/p>\n<p><strong>\u4e3b\u7ebf\u7a0b\u6267\u884c\uff1a<\/strong><br \/> \u540c\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4ee5 100 \u6beb\u79d2\u7684\u95f4\u9694\u6253\u5370\u70b9\uff08\u201c.\u201d\uff09\u3002<\/p>\n<p>\u7a0b\u5e8f\u8f93\u51fa\uff08\u793a\u4f8b\uff09<\/p>\n<pre>Main thread starting.\nChild #1 starting.\n.In Child #1, count is 0\n..In Child #1, count is 1\n...In Child #1, count is 2\n... (continua)\nMain thread ending.\nChild #1 terminating.\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u89c2\u5bdf<\/strong><br \/> \u4e3b\u7ebf\u7a0b\u548c\u8f85\u52a9\u7ebf\u7a0b\u540c\u65f6\u8fd0\u884c\u3002<br \/> run()\u65b9\u6cd5\u5305\u542b\u4e86\u8f85\u52a9\u7ebf\u7a0b\u7684\u903b\u8f91\uff0c\u800c\u4e3b\u7ebf\u7a0b\u5219\u7ee7\u7eed\u72ec\u7acb\u6267\u884c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b\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>\u5728\u672c\u7ec3\u4e60\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u901a\u8fc7\u76f4\u63a5\u6269\u5c55 thread \u7c7b\uff08\u800c\u4e0d\u662f\u5b9e\u73b0 runnable \u63a5\u53e3\uff09\u6765\u5728 java \u4e2d\u521b\u5efa\u7ebf\u7a0b\u3002\u901a\u8fc7\u8fd9\u6837\u505a\uff0c\u60a8\u7684\u7c7b\u7ee7\u627f\u4e86 thread \u65b9\u6cd5\uff0c\u8fd9\u4f7f\u5f97\u76f4\u63a5\u64cd\u4f5c\u7ebf\u7a0b\u53d8\u5f97\u66f4\u5bb9\u6613\uff0c\u800c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u5355\u72ec\u7684\u7ebf\u7a0b\u3002 \u953b\u70bc\u6b65\u9aa4 \u6269\u5c55 thread \u7c7b\uff1a \u4f60\u7684\u7c7b\u5fc5\u987b\u7ee7\u627f\u81ea thread \u5e76\u91cd\u5199 run() \u65b9\u6cd5\u3002 \u7c7b\u6784\u9020\u51fd\u6570\uff1a \u4f7f\u7528 super(name) \u6784\u9020\u51fd\u6570\u4e3a\u7ebf\u7a0b\u547d\u540d\uff0c\u5e76\u901a\u8fc7\u76f4\u63a5\u8c03\u7528 start() \u5f00\u59cb\u6267\u884c\u3002 \u91cd\u5199 run() \u65b9\u6cd5\uff1a \u8be5\u65b9\u6cd5\u5b9a\u4e49\u4e86\u7ebf\u7a0b\u7684\u884c\u4e3a\u3002\u5728\u8fd9\u91cc\uff0c\u7ebf\u7a0b\u6253\u5370\u5b83\u7684\u540d\u79f0\u5e76\u8fd0\u884c\u4e00\u4e2a\u5faa\u73af\u3002 \u5b8c\u6574\u7684\u529f\u80fd\u793a\u4f8b\uff1a \u4e0b\u9762\u662f\u4ee3\u7801\uff1a \/\/ define a classe que estende thread class mythread extends thread { \/\/ constr\u00f3i uma nova thread mythread(string name) { super(name); \/\/ nomeia a thread [&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-40324","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40324","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=40324"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40324\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}