{"id":67064,"date":"2025-05-03T13:44:29","date_gmt":"2025-05-03T05:44:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/67064\/"},"modified":"2025-05-03T13:44:29","modified_gmt":"2025-05-03T05:44:29","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b-cap-%e7%bc%96%e7%a8%8b-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/67064\/","title":{"rendered":"\u591a\u7ebf\u7a0b Cap \u7f16\u7a0b"},"content":{"rendered":"<p><strong>\u4e3b\u8981\u6280\u80fd\u548c\u6982\u5ff5<\/strong><br \/> \u2022 \u4e86\u89e3\u521b\u5efa\u591a\u7ebf\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6<br \/> \u2022 \u4e86\u89e3 thread \u7c7b\u548c runnable<br \/> \u63a5\u53e3 \u2022 \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b<br \/> \u2022 \u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<br \/> \u2022 \u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f<br \/> \u2022 \u4f7f\u7528\u7ebf\u7a0b\u4f18\u5148\u7ea7<br \/> \u2022 \u4e86\u89e3\u7ebf\u7a0b\u540c\u6b65<br \/> \u2022 \u4f7f\u7528\u540c\u6b65\u65b9\u6cd5<br \/> \u2022 \u4f7f\u7528\u540c\u6b65\u5757<br \/> \u2022 \u4fc3\u8fdb\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1<br \/> \u2022 \u6682\u505c\u3001\u6062\u590d\u548c\u505c\u6b62\u7ebf\u7a0b<\/p>\n<p><strong>\u7ebf\u7a0b\uff1a<\/strong>\u8fd9\u4e9b\u662f\u7a0b\u5e8f\u5185\u72ec\u7acb\u7684\u6267\u884c\u8def\u5f84\u3002<br \/><strong>\u591a\u4efb\u52a1\uff1a<\/strong>\u5b83\u53ef\u4ee5\u57fa\u4e8e\u8fdb\u7a0b\uff08\u591a\u4e2a\u7a0b\u5e8f\uff09\u6216\u7ebf\u7a0b\uff08\u540c\u4e00\u7a0b\u5e8f\u4e2d\u7684\u591a\u4e2a\u4efb\u52a1\uff09\u3002<br \/><strong>\u4f18\u70b9\uff1a<\/strong><br \/> \u5229\u7528\u7a7a\u95f2\u65f6\u95f4\u63d0\u9ad8\u6548\u7387\u3002<br \/> \u66f4\u597d\u5730\u5229\u7528\u591a\u6838\/\u591a\u7cfb\u7edf\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u521b\u5efa\u548c\u7ba1\u7406<\/strong><\/p>\n<p><strong>\u7c7b\u548c\u63a5\u53e3\uff1a<\/strong><br \/><strong>thread\uff1a<\/strong>\u5c01\u88c5\u7ebf\u7a0b\u7684\u7c7b\u3002<br \/><strong>runnable\uff1a<\/strong>\u7528\u4e8e\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u7684\u63a5\u53e3\u3002<\/p>\n<p><strong>\u5e38\u7528\u7ebf\u7a0b\u7c7b\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>getname()\uff1a\u8fd4\u56de\u7ebf\u7a0b\u7684\u540d\u79f0\u3002<\/li>\n<li>getpriority()\uff1a\u8fd4\u56de\u4f18\u5148\u7ea7\u3002<\/li>\n<li>isalive()\uff1a\u68c0\u67e5\u7ebf\u7a0b\u662f\u5426\u4ecd\u5728\u6267\u884c\u3002<\/li>\n<li>join()\uff1a\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002<\/li>\n<li>run()\uff1a\u5b9a\u4e49\u7ebf\u7a0b\u7684\u5165\u53e3\u70b9\u3002<\/li>\n<li>sleep(long ms)\uff1a\u6682\u505c\u7ebf\u7a0b\u4e00\u6bb5\u65f6\u95f4\u3002<\/li>\n<li>start(): \u5f00\u59cb\u7ebf\u7a0b\u6267\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u521b\u5efa\u4e3b\u9898\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b9e\u73b0\u53ef\u8fd0\u884c\uff1a <\/li>\n<\/ul>\n<pre>class mythread implements runnable {\n    string threadname;\n\n    mythread(string name) {\n        threadname = name;\n    }\n\n    public void run() {\n        system.out.println(threadname + \" starting.\");\n        try {\n            for (int i = 0; i &lt; 10; i++) {\n                thread.sleep(400);\n                system.out.println(\"in \" + threadname + \", count is \" + i);\n            }\n        } catch (interruptedexception e) {\n            system.out.println(threadname + \" interrupted.\");\n        }\n        system.out.println(threadname + \" terminating.\");\n    }\n}\n\npublic class usethreads {\n    public static void main(string[] args) {\n        system.out.println(\"main thread starting.\");\n\n        mythread mythread = new mythread(\"child #1\");\n        thread thread = new thread(mythread);\n        thread.start();\n\n        for (int i = 0; i &lt; 50; i++) {\n            system.out.print(\".\");\n            try {\n                thread.sleep(100);\n            } catch (interruptedexception e) {\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\uff1a<\/p>\n<pre>main thread starting.\n.\nchild #1 starting.\n..\nin child #1, count is 0\n...\nin child #1, count is 1\n...\nmain thread ending.\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ebf\u7a0b\u7c7b\u6269\u5c55\uff1a<\/strong><\/p>\n<pre>class MyThread extends Thread {\n    MyThread(String name) {\n        super(name);\n    }\n\n    public void run() {\n        System.out.println(getName() + \" starting.\");\n        try {\n            for (int i = 0; i &lt; 10; i++) {\n                Thread.sleep(400);\n                System.out.println(\"In \" + getName() + \", count is \" + i);\n            }\n        } catch (InterruptedException e) {\n            System.out.println(getName() + \" interrupted.\");\n        }\n        System.out.println(getName() + \" terminating.\");\n    }\n}\n\npublic class UseThreads {\n    public static void main(String[] args) {\n        System.out.println(\"Main thread starting.\");\n\n        MyThread thread = new MyThread(\"Child #1\");\n        thread.start();\n\n        for (int i = 0; i &lt; 50; i++) {\n            System.out.print(\".\");\n            try {\n                Thread.sleep(100);\n            } catch (InterruptedException e) {\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>\u6ce8\u610f\uff1asleep()\u65b9\u6cd5\u4f7f\u5f97\u8c03\u7528\u5b83\u7684\u7ebf\u7a0b\u6682\u505c\u6267\u884c<br \/> \u5728\u6307\u5b9a\u7684\u6beb\u79d2\u65f6\u95f4\u5185\u3002<\/p>\n<p>\u4e66\u684c<br \/><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173206386394955.jpg\" class=\"aligncenter\" title=\"\u591a\u7ebf\u7a0b Cap \u7f16\u7a0b\u63d2\u56fe\" alt=\"\u591a\u7ebf\u7a0b Cap \u7f16\u7a0b\u63d2\u56fe\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u591a\u7ebf\u7a0b Cap \u7f16\u7a0b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u8981\u6280\u80fd\u548c\u6982\u5ff5 \u2022 \u4e86\u89e3\u521b\u5efa\u591a\u7ebf\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6 \u2022 \u4e86\u89e3 thread \u7c7b\u548c runnable \u63a5\u53e3 \u2022 \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b \u2022 \u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b \u2022 \u786e\u5b9a\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f \u2022 \u4f7f\u7528\u7ebf\u7a0b\u4f18\u5148\u7ea7 \u2022 \u4e86\u89e3\u7ebf\u7a0b\u540c\u6b65 \u2022 \u4f7f\u7528\u540c\u6b65\u65b9\u6cd5 \u2022 \u4f7f\u7528\u540c\u6b65\u5757 \u2022 \u4fc3\u8fdb\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1 \u2022 \u6682\u505c\u3001\u6062\u590d\u548c\u505c\u6b62\u7ebf\u7a0b \u7ebf\u7a0b\uff1a\u8fd9\u4e9b\u662f\u7a0b\u5e8f\u5185\u72ec\u7acb\u7684\u6267\u884c\u8def\u5f84\u3002\u591a\u4efb\u52a1\uff1a\u5b83\u53ef\u4ee5\u57fa\u4e8e\u8fdb\u7a0b\uff08\u591a\u4e2a\u7a0b\u5e8f\uff09\u6216\u7ebf\u7a0b\uff08\u540c\u4e00\u7a0b\u5e8f\u4e2d\u7684\u591a\u4e2a\u4efb\u52a1\uff09\u3002\u4f18\u70b9\uff1a \u5229\u7528\u7a7a\u95f2\u65f6\u95f4\u63d0\u9ad8\u6548\u7387\u3002 \u66f4\u597d\u5730\u5229\u7528\u591a\u6838\/\u591a\u7cfb\u7edf\u3002 \u7ebf\u7a0b\u521b\u5efa\u548c\u7ba1\u7406 \u7c7b\u548c\u63a5\u53e3\uff1athread\uff1a\u5c01\u88c5\u7ebf\u7a0b\u7684\u7c7b\u3002runnable\uff1a\u7528\u4e8e\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u7684\u63a5\u53e3\u3002 \u5e38\u7528\u7ebf\u7a0b\u7c7b\u65b9\u6cd5\uff1a getname()\uff1a\u8fd4\u56de\u7ebf\u7a0b\u7684\u540d\u79f0\u3002 getpriority()\uff1a\u8fd4\u56de\u4f18\u5148\u7ea7\u3002 isalive()\uff1a\u68c0\u67e5\u7ebf\u7a0b\u662f\u5426\u4ecd\u5728\u6267\u884c\u3002 join()\uff1a\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002 run()\uff1a\u5b9a\u4e49\u7ebf\u7a0b\u7684\u5165\u53e3\u70b9\u3002 sleep(long ms)\uff1a\u6682\u505c\u7ebf\u7a0b\u4e00\u6bb5\u65f6\u95f4\u3002 start(): \u5f00\u59cb\u7ebf\u7a0b\u6267\u884c\u3002 \u521b\u5efa\u4e3b\u9898\uff1a \u5b9e\u73b0\u53ef\u8fd0\u884c\uff1a class mythread implements runnable { string threadname; mythread(string name) { threadname [&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-67064","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67064","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=67064"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67064\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=67064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=67064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=67064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}