{"id":213,"date":"2015-04-13T11:44:26","date_gmt":"2015-04-13T03:44:26","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=213"},"modified":"2026-08-18T12:09:29","modified_gmt":"2026-08-18T04:09:29","slug":"java%e5%88%9b%e5%bb%ba%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=213","title":{"rendered":"Java\u521b\u5efa\u591a\u7ebf\u7a0b"},"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;\">\u5230\u76ee\u524d\u4e3a\u6b62\u53ea\u7528\u5230\u4e24\u6761\u7ebf\u7a0b\uff1a\u4e3b\u7ebf\u7a0b\u548c\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u3002\u7a0b\u5e8f\u53ef\u4ee5\u6309\u9700\u8981\u521b\u5efa\u66f4\u591a\u3002\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u521b\u5efa\u4e86<strong style=\"color: #4870ac;\">\u4e09\u4e2a\u5b50\u7ebf\u7a0b<\/strong> One\u3001Two\u3001Three\u3002<\/p>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">\u6bcf\u4e2a NewThread \u5728\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;\">start()<\/code>\uff0c\u4ece 5 \u6570\u5230 1\uff0c\u6bcf\u6b21\u7761 1 \u79d2\u3002\u4e3b\u7ebf\u7a0b\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;\">sleep(10000)<\/code> \u7761\u5341\u79d2\uff0c\u786e\u4fdd\u81ea\u5df1\u6700\u540e\u7ed3\u675f\u3002<\/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;\">\u7ebf\u7a0b<\/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;\">\u505a\u4ec0\u4e48<\/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;\">\u7b49\u5f85\u7b56\u7565<\/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;\">One \/ Two \/ Three<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5404\u81ea\u5012\u6570\u5e76 sleep(1000)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7ea6 5 \u79d2\u7ed3\u675f<\/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;\">main<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u542f\u52a8\u4e09\u4e2a\u5b50\u7ebf\u7a0b<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">sleep(10000) \u4fdd\u5e95\u6700\u540e\u9000\u51fa<\/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\u4e09\u4e2a\u5b50\u7ebf\u7a0b\u7684\u4ee3\u7801<\/h2>\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 implements Runnable {\n    String name;\n    Thread t;\n    NewThread(String threadname) {\n        name = threadname;\n        t = new Thread(this, name);\n        System.out.println(\"New 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(name + \": \" + i);\n                Thread.sleep(1000);\n            }\n        } catch (InterruptedException e) {\n            System.out.println(name + \"Interrupted\");\n        }\n        System.out.println(name + \" exiting.\");\n    }\n}\n\nclass MultiThreadDemo {\n    public static void main(String args[]) {\n        new NewThread(\"One\");\n        new NewThread(\"Two\");\n        new NewThread(\"Three\");\n        try {\n            Thread.sleep(10000);\n        } catch (InterruptedException e) {\n            System.out.println(\"Main thread Interrupted\");\n        }\n        System.out.println(\"Main thread exiting.\");\n    }\n}<\/code><\/pre>\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\u8f93\u51fa\u8bf4\u660e<\/h2>\n<p style=\"margin: 1em 0px;\">\u542f\u52a8\u540e\u4e09\u4e2a\u5b50\u7ebf\u7a0b\u5171\u4eab CPU\uff0c\u6253\u5370\u4ea4\u9519\u51fa\u73b0\u662f\u6b63\u5e38\u7684\uff08\u8c01\u5148\u8c01\u540e\u53d6\u51b3\u4e8e\u8c03\u5ea6\uff09\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;\">New thread: Thread[One,5,main]\nNew thread: Thread[Two,5,main]\nNew thread: Thread[Three,5,main]\nOne: 5\nTwo: 5\nThree: 5\nOne: 4\nTwo: 4\nThree: 4\n...\nOne exiting.\nTwo exiting.\nThree exiting.\nMain thread exiting.<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7528\u56fa\u5b9a 10 \u79d2 sleep \u53ea\u662f\u6743\u5b9c\u4e4b\u8ba1\uff1b\u5b50\u7ebf\u7a0b\u53d8\u591a\u6216\u53d8\u6162\u65f6\u53ef\u80fd\u4e0d\u591f\u3002\u66f4\u7a33\u7684\u505a\u6cd5\u662f\u5bf9\u6bcf\u6761\u7ebf\u7a0b <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;\">join()<\/code>\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\uff1a\u591a\u6761\u5b50\u7ebf\u7a0b new \u5b8c\u7acb\u523b start\uff0c\u5b83\u4eec\u4f1a\u4ea4\u9519\u62a2 CPU\uff1b\u4e3b\u7ebf\u7a0b\u8981\u7528\u8db3\u591f\u957f\u7684 sleep \u6216 join\uff0c\u4fdd\u8bc1\u81ea\u5df1\u6700\u540e\u4e00\u4e2a\u9000\u51fa\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u5230\u76ee\u524d\u4e3a\u6b62\u53ea\u7528\u5230\u4e24\u6761\u7ebf\u7a0b\uff1a\u4e3b\u7ebf\u7a0b\u548c\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u3002\u7a0b\u5e8f\u53ef\u4ee5\u6309\u9700\u8981\u521b\u5efa\u66f4\u591a\u3002\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u521b\u5efa\u4e86\u4e09\u4e2a\u5b50\u7ebf\u7a0b One\u3001T [&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\/213"}],"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=213"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":295,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions\/295"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}