{"id":211,"date":"2015-04-13T11:43:36","date_gmt":"2015-04-13T03:43:36","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=211"},"modified":"2026-08-18T12:09:29","modified_gmt":"2026-08-18T04:09:29","slug":"java%e5%88%9b%e5%bb%ba%e7%ba%bf%e7%a8%8brunnable%e6%8e%a5%e5%8f%a3%e5%92%8cthread%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=211","title":{"rendered":"Java\u521b\u5efa\u7ebf\u7a0b(Runnable\u63a5\u53e3\u548cThread\u7c7b)"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u591a\u6570\u60c5\u51b5\u901a\u8fc7\u5b9e\u4f8b\u5316\u4e00\u4e2a Thread \u5bf9\u8c61\u6765\u521b\u5efa\u7ebf\u7a0b\u3002Java \u5b9a\u4e49\u4e86\u4e24\u79cd\u65b9\u5f0f\uff1a<strong style=\"color: #4870ac;\">\u5b9e\u73b0 Runnable \u63a5\u53e3<\/strong>\uff0c\u6216<strong style=\"color: #4870ac;\">\u7ee7\u627f Thread \u7c7b<\/strong>\u3002<\/p>\n<p><!--more--><\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u65b9\u5f0f<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5165\u53e3<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u9002\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">implements Runnable<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">new Thread(this, name) \u518d start()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e0d\u5fc5\u6539 Thread \u5176\u4ed6\u65b9\u6cd5\u65f6\u4f18\u5148<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">extends Thread<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">super(name) \u540e start()\uff0c\u91cd\u5199 run()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u786e\u5b9e\u8981\u589e\u5f3a \/ \u4fee\u6539 Thread \u65f6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u5b9e\u73b0 Runnable<\/h2>\n<p style=\"margin: 1em 0px;\">\u6700\u7b80\u5355\u7684\u529e\u6cd5\u662f\u5199\u4e00\u4e2a\u5b9e\u73b0 Runnable \u7684\u7c7b\u3002Runnable \u62bd\u8c61\u4e86\u4e00\u4e2a\u6267\u884c\u4ee3\u7801\u5355\u5143\u3002\u53ea\u9700\u5b9e\u73b0 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">public void run()<\/code>\u3002run \u91cc\u53ef\u4ee5\u50cf\u4e3b\u7ebf\u7a0b\u4e00\u6837\u8c03\u5176\u4ed6\u65b9\u6cd5\u3001\u5f15\u7528\u5176\u4ed6\u7c7b\u3001\u58f0\u660e\u53d8\u91cf\uff1b\u552f\u4e00\u4e0d\u540c\u662f\u5b83\u786e\u7acb\u4e86\u53e6\u4e00\u6761\u5e76\u53d1\u5165\u53e3\u3002run \u8fd4\u56de\uff0c\u8be5\u7ebf\u7a0b\u7ed3\u675f\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7136\u540e\u5728\u7c7b\u91cc\u5b9e\u4f8b\u5316 Thread\u3002\u5e38\u7528\u6784\u9020\u51fd\u6570\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Thread(Runnable threadOb, String threadName)<\/code>\u3002\u65b0\u7ebf\u7a0b\u5efa\u597d\u540e\u5e76\u4e0d\u4f1a\u8dd1\uff0c\u76f4\u5230\u8c03\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">void start()<\/code>\u2014\u2014\u672c\u8d28\u4e0a start \u4f1a\u53bb\u8c03 run\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ Create a second thread.\nclass NewThread implements Runnable {\n    Thread t;\n    NewThread() {\n        t = new Thread(this, \"Demo Thread\");\n        System.out.println(\"Child thread: \" + t);\n        t.start();\n    }\n    public void run() {\n        try {\n            for (int i = 5; i &gt; 0; i--) {\n                System.out.println(\"Child Thread: \" + i);\n                Thread.sleep(500);\n            }\n        } catch (InterruptedException e) {\n            System.out.println(\"Child interrupted.\");\n        }\n        System.out.println(\"Exiting child thread.\");\n    }\n}\n\nclass ThreadDemo {\n    public static void main(String args[]) {\n        new NewThread();\n        try {\n            for (int i = 5; i &gt; 0; i--) {\n                System.out.println(\"Main Thread: \" + i);\n                Thread.sleep(1000);\n            }\n        } catch (InterruptedException e) {\n            System.out.println(\"Main thread interrupted.\");\n        }\n        System.out.println(\"Main thread exiting.\");\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6784\u9020\u51fd\u6570\u91cc <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">new Thread(this, \"Demo Thread\")<\/code> \u8868\u793a\u65b0\u7ebf\u7a0b\u8981\u8c03 this \u4e0a\u7684 run\u3002start \u4e4b\u540e\u6784\u9020\u51fd\u6570\u8fd4\u56de main\uff0c\u4e3b\u7ebf\u7a0b\u8fdb\u5165\u81ea\u5df1\u7684 for\u3002\u4e24\u6761\u7ebf\u7a0b\u5171\u4eab CPU\uff0c\u76f4\u5230\u5404\u81ea\u5faa\u73af\u7ed3\u675f\u3002\u5178\u578b\u8f93\u51fa\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">Child thread: Thread[Demo Thread,5,main]\nMain Thread: 5\nChild Thread: 5\nChild Thread: 4\nMain Thread: 4\nChild Thread: 3\nChild Thread: 2\nMain Thread: 3\nChild Thread: 1\nExiting child thread.\nMain Thread: 2\nMain Thread: 1\nMain thread exiting.<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u591a\u7ebf\u7a0b\u91cc\u901a\u5e38\u4e3b\u7ebf\u7a0b\u5e94\u6700\u540e\u7ed3\u675f\u3002\u4e00\u4e9b\u8001 JVM \u82e5\u4e3b\u7ebf\u7a0b\u5148\u4e8e\u5b50\u7ebf\u7a0b\u7ed3\u675f\uff0c\u8fd0\u884c\u65f6\u53ef\u80fd\u201c\u6302\u8d77\u201d\u3002\u672c\u4f8b\u4e3b\u7ebf\u7a0b sleep 1000ms\u3001\u5b50\u7ebf\u7a0b 500ms\uff0c\u4fdd\u8bc1\u5b50\u7ebf\u7a0b\u5148\u5b8c\u3002\u540e\u9762\u4f1a\u8bb2\u66f4\u597d\u7684\u7b49\u5f85\u65b9\u5f0f\uff08join\uff09\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u6269\u5c55 Thread<\/h2>\n<p style=\"margin: 1em 0px;\">\u53e6\u4e00\u4e2a\u9014\u5f84\u662f\u65b0\u5efa\u7c7b\u6269\u5c55 Thread\uff0c\u518d\u521b\u5efa\u5b9e\u4f8b\u3002\u5fc5\u987b\u91cd\u8f7d run \u4f5c\u4e3a\u5165\u53e3\uff0c\u5e76\u8c03\u7528 start \u542f\u52a8\u3002\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">super(\"Demo Thread\")<\/code> \u8c03\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">public Thread(String threadName)<\/code>\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">class NewThread extends Thread {\n    NewThread() {\n        super(\"Demo Thread\");\n        System.out.println(\"Child thread: \" + this);\n        start();\n    }\n    public void run() {\n        try {\n            for (int i = 5; i &gt; 0; i--) {\n                System.out.println(\"Child Thread: \" + i);\n                Thread.sleep(500);\n            }\n        } catch (InterruptedException e) {\n            System.out.println(\"Child interrupted.\");\n        }\n        System.out.println(\"Exiting child thread.\");\n    }\n}\n\nclass ExtendThread {\n    public static void main(String args[]) {\n        new NewThread();\n        try {\n            for (int i = 5; i &gt; 0; i--) {\n                System.out.println(\"Main Thread: \" + i);\n                Thread.sleep(1000);\n            }\n        } catch (InterruptedException e) {\n            System.out.println(\"Main thread interrupted.\");\n        }\n        System.out.println(\"Main thread exiting.\");\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8f93\u51fa\u4e0e\u524d\u4e00\u7248\u76f8\u540c\u3002\u5b50\u7ebf\u7a0b\u7531\u5b9e\u4f8b\u5316 NewThread\uff08\u6d3e\u751f\u81ea Thread\uff09\u751f\u6210\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u9009\u54ea\u4e00\u79cd<\/h2>\n<p style=\"margin: 1em 0px;\">Thread \u5b9a\u4e49\u4e86\u591a\u79cd\u53ef\u88ab\u6d3e\u751f\u7c7b\u91cd\u8f7d\u7684\u65b9\u6cd5\uff0c\u4f46\u5fc5\u987b\u91cd\u8f7d\u7684\u53ea\u6709 run\u2014\u2014\u8fd9\u4e5f\u662f Runnable \u8981\u6c42\u7684\u90a3\u4e00\u4e2a\u3002\u5f88\u591a Java \u7a0b\u5e8f\u5458\u8ba4\u4e3a\uff1a\u7c7b\u53ea\u5728\u88ab\u52a0\u5f3a\u6216\u4fee\u6539\u65f6\u624d\u5e94\u8be5\u88ab\u6269\u5c55\u3002\u56e0\u6b64\u82e5\u4e0d\u91cd\u8f7d Thread \u7684\u5176\u4ed6\u65b9\u6cd5\uff0c\u6700\u597d\u53ea\u5b9e\u73b0 Runnable\u3002\u672c\u7ae0\u5176\u4f59\u90e8\u5206\u7528\u5b9e\u73b0 Runnable \u7684\u7c7b\u6765\u521b\u5efa\u7ebf\u7a0b\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1arun \u5199\u7ebf\u7a0b\u4f53\uff0cstart \u624d\u542f\u52a8\uff1b\u80fd\u5b9e\u73b0 Runnable \u5c31\u522b\u53bb\u7ee7\u627f Thread\uff0c\u4e3b\u7ebf\u7a0b\u7528\u66f4\u957f\u7684 sleep\uff08\u6216\u540e\u9762\u7684 join\uff09\u4fdd\u8bc1\u6700\u540e\u9000\u51fa\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u6570\u60c5\u51b5\u901a\u8fc7\u5b9e\u4f8b\u5316\u4e00\u4e2a Thread \u5bf9\u8c61\u6765\u521b\u5efa\u7ebf\u7a0b\u3002Java \u5b9a\u4e49\u4e86\u4e24\u79cd\u65b9\u5f0f\uff1a\u5b9e\u73b0 Runnable \u63a5\u53e3\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[178],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/211"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=211"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}