{"id":203855,"date":"2025-05-22T14:52:34","date_gmt":"2025-05-22T06:52:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203855\/"},"modified":"2025-05-22T14:52:34","modified_gmt":"2025-05-22T06:52: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:\/\/server.hk\/cnblog\/203855\/","title":{"rendered":"\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/173278266067482a4452e95.jpg\" class=\"aligncenter\"><\/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><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><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>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/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>\u7ec3\u4e60\u5c1d\u8bd5\u8fd9\u4e2a\u6269\u5c55\u7ebf\u7a0b \u6536\u85cf \u4e00\u5206&#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-203855","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203855","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=203855"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203855\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}