{"id":209,"date":"2015-04-13T11:42:36","date_gmt":"2015-04-13T03:42:36","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=209"},"modified":"2026-08-18T12:09:29","modified_gmt":"2026-08-18T04:09:29","slug":"java%e4%b8%bb%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=209","title":{"rendered":"Java\u4e3b\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;\">Java \u7a0b\u5e8f\u4e00\u542f\u52a8\uff0c\u7acb\u523b\u6709\u4e00\u6761\u7ebf\u7a0b\u5728\u8dd1\uff0c\u901a\u5e38\u53eb<strong style=\"color: #4870ac;\">\u4e3b\u7ebf\u7a0b<\/strong>\uff08main thread\uff09\uff0c\u56e0\u4e3a\u5b83\u4ece\u7a0b\u5e8f\u5f00\u59cb\u5c31\u6267\u884c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e3b\u7ebf\u7a0b\u91cd\u8981\u5728\u4e24\u5934\uff1a\u5b83\u662f\u4ea7\u751f\u5176\u4ed6\u5b50\u7ebf\u7a0b\u7684\u7ebf\u7a0b\uff1b\u901a\u5e38\u5b83\u5fc5\u987b\u6700\u540e\u7ed3\u675f\uff0c\u56e0\u4e3a\u5b83\u8fd8\u8981\u505a\u5404\u79cd\u5173\u95ed\u52a8\u4f5c\u3002<\/p>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">\u4e3b\u7ebf\u7a0b\u867d\u662f\u81ea\u52a8\u521b\u5efa\u7684\uff0c\u4f46\u4ecd\u53ef\u7528 Thread \u5bf9\u8c61\u63a7\u5236\u3002\u5148\u8c03\u516c\u6709\u9759\u6001\u65b9\u6cd5 <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;\">currentThread()<\/code> \u62ff\u5230\u5f15\u7528\uff0c\u4e4b\u540e\u5c31\u80fd\u50cf\u63a7\u5236\u5176\u4ed6\u7ebf\u7a0b\u90a3\u6837\u63a7\u5236\u5b83\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;\">static Thread currentThread()<\/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;\">\u4e00\u3001\u63a7\u5236\u4e3b\u7ebf\u7a0b\u7684\u4f8b\u5b50<\/h2>\n<p style=\"margin: 1em 0px;\">\u62ff\u5230\u5f53\u524d\u7ebf\u7a0b\uff08\u8fd9\u91cc\u81ea\u7136\u662f\u4e3b\u7ebf\u7a0b\uff09\u5b58\u8fdb t\uff0c\u6253\u5370\u4fe1\u606f\uff0c\u518d\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;\">setName()<\/code> \u6539\u540d\u3002\u63a5\u7740\u4ece 5 \u5012\u6570\uff0c\u6bcf\u6b21\u6682\u505c <strong style=\"color: #4870ac;\">1000 \u6beb\u79d2\uff081 \u79d2\uff09<\/strong>\u2014\u2014\u539f\u6587\u5199\u6210\u201c1 \u6beb\u79d2\u201d\u662f\u7b14\u8bef\uff0c\u4ee3\u7801\u91cc\u662f <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.sleep(1000)<\/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;\">\/\/ Controlling the main Thread.\nclass CurrentThreadDemo {\n    public static void main(String args[]) {\n        Thread t = Thread.currentThread();\n        System.out.println(\"Current thread: \" + t);\n        t.setName(\"My Thread\");\n        System.out.println(\"After name change: \" + t);\n        try {\n            for (int n = 5; n &gt; 0; n--) {\n                System.out.println(n);\n                Thread.sleep(1000);\n            }\n        } catch (InterruptedException e) {\n            System.out.println(\"Main thread interrupted\");\n        }\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\"><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()<\/code> \u53ef\u80fd\u629b <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;\">InterruptedException<\/code>\uff08\u5176\u4ed6\u7ebf\u7a0b\u60f3\u53eb\u9192\u6c89\u7761\u7ebf\u7a0b\u65f6\uff09\u3002\u672c\u4f8b\u53ea\u6253\u5370\u662f\u5426\u88ab\u6253\u65ad\uff1b\u5b9e\u9645\u7a0b\u5e8f\u8981\u8ba4\u771f\u5904\u7406\u3002\u8f93\u51fa\u7c7b\u4f3c\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;\">Current thread: Thread[main,5,main]\nAfter name change: Thread[My Thread,5,main]<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u628a t \u4ea4\u7ed9 println \u65f6\uff0c\u663e\u793a\u987a\u5e8f\u662f\uff1a\u7ebf\u7a0b\u540d\u3001\u4f18\u5148\u7ea7\u3001\u7ec4\u540d\u3002\u9ed8\u8ba4\u4e3b\u7ebf\u7a0b\u540d\u53eb main\uff0c\u4f18\u5148\u7ea7 5\uff0c\u6240\u5c5e\u7ec4\u4e5f\u53eb main\u3002\u7ebf\u7a0b\u7ec4\uff08thread group\uff09\u662f\u628a\u82e5\u5e72\u7ebf\u7a0b\u5f53\u6574\u4f53\u63a7\u5236\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7531\u8fd0\u884c\u65f6\u5904\u7406\u3002\u6539\u540d\u540e\u518d\u6253\u5370\uff0c\u5c31\u4f1a\u770b\u5230\u65b0\u540d\u5b57\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\u3001sleep \/ setName \/ getName<\/h2>\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\u6cd5<\/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;\">\u8bf4\u660e<\/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;\">static void sleep(long milliseconds)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6309\u6beb\u79d2\u6302\u8d77\uff0c\u53ef\u80fd\u629b InterruptedException<\/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;\">static void sleep(long milliseconds, int nanoseconds)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u989d\u5916\u6307\u5b9a\u7eb3\u79d2\uff1b\u4ec5\u5f53\u8fd0\u884c\u65f6\u652f\u6301\u7eb3\u79d2\u5468\u671f\u65f6\u6709\u610f\u4e49<\/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;\">final void setName(String threadName)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8bbe\u7f6e\u7ebf\u7a0b\u540d\u79f0<\/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;\">final String getName()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8bfb\u53d6\u7ebf\u7a0b\u540d\u79f0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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\u4e3b\u7ebf\u7a0b\u81ea\u52a8\u8bde\u751f\u3001\u8d1f\u8d23\u6d3e\u751f\u548c\u6536\u5c3e\uff1b\u7528 currentThread() \u62ff\u5230\u5b83\uff0c\u5c31\u80fd\u6539\u540d\u3001sleep\uff0c\u5e76\u8ba9\u5b83\u901a\u5e38\u6700\u540e\u4e00\u4e2a\u9000\u51fa\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Java \u7a0b\u5e8f\u4e00\u542f\u52a8\uff0c\u7acb\u523b\u6709\u4e00\u6761\u7ebf\u7a0b\u5728\u8dd1\uff0c\u901a\u5e38\u53eb\u4e3b\u7ebf\u7a0b\uff08main thread\uff09\uff0c\u56e0\u4e3a\u5b83\u4ece\u7a0b\u5e8f\u5f00\u59cb\u5c31\u6267\u884c\u3002 \u4e3b [&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\/209"}],"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=209"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/314"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}