{"id":223,"date":"2015-04-15T20:41:03","date_gmt":"2015-04-15T12:41:03","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=223"},"modified":"2026-08-18T12:09:29","modified_gmt":"2026-08-18T04:09:29","slug":"java%e7%ba%bf%e7%a8%8b%e9%97%b4%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=223","title":{"rendered":"Java\u7ebf\u7a0b\u95f4\u901a\u4fe1"},"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;\">\u540c\u6b65\u65b9\u6cd5\u80fd\u6321\u4f4f\u5176\u4ed6\u7ebf\u7a0b\u540c\u65f6\u8fdb\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f46\u8fd8\u4e0d\u591f\u7cbe\u5de7\u3002\u751f\u4ea7\u8005\u4e00\u8fb9\u9020\u6570\u636e\u3001\u6d88\u8d39\u8005\u4e00\u8fb9\u7528\uff0c\u8fd8\u8981\u4e92\u76f8\u7b49\u5bf9\u65b9\u5e72\u5b8c\u2014\u2014\u5982\u679c\u9760\u8f6e\u8be2\u6b7b\u5faa\u73af\u76ef\u6761\u4ef6\uff0c\u4f1a\u6d6a\u8d39\u5927\u91cf CPU\u3002<\/p>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">Java \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;\">wait()<\/code>\u3001<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;\">notify()<\/code>\u3001<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;\">notifyAll()<\/code> \u505a\u7ebf\u7a0b\u95f4\u901a\u4fe1\u3002\u5b83\u4eec\u662f Object \u7684 final \u65b9\u6cd5\uff0c\u5fc5\u987b\u5728 synchronized \u65b9\u6cd5\uff08\u6216\u5757\uff09\u91cc\u8c03\u7528\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;\">\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;\">\u4f5c\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;\">wait()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u653e\u5f03\u7ba1\u7a0b\u8fdb\u5165\u7761\u7720\uff0c\u76f4\u5230\u540c\u5bf9\u8c61\u4e0a\u6709\u4eba notify\uff1b\u53e6\u6709\u53ef\u9650\u65f6\u7684\u91cd\u8f7d<\/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;\">notify()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5524\u9192\u540c\u4e00\u5bf9\u8c61\u4e0a\u67d0\u4e2a\u6b63\u5728 wait \u7684\u7ebf\u7a0b<\/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;\">notifyAll()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5524\u9192\u5168\u90e8\u7b49\u5f85\u7ebf\u7a0b\uff0c\u4f18\u5148\u7ea7\u9ad8\u7684\u5148\u8dd1<\/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\u9519\u8bef\u5199\u6cd5\uff1a\u540c\u6b65\u4e86\u4ecd\u4f1a\u8d85\u8f66<\/h2>\n<p style=\"margin: 1em 0px;\">\u56db\u4e2a\u7c7b\uff1aQ \u662f\u961f\u5217\uff0cProducer \/ Consumer \u662f\u7ebf\u7a0b\uff0cPC \u628a\u5b83\u4eec\u4e32\u8d77\u6765\u3002put \/ get \u867d\u7136 synchronized\uff0c\u4f46\u6ca1\u6709\u201c\u6709\u6ca1\u6709\u65b0\u6570\u636e\u201d\u7684\u7ea6\u5b9a\uff0c\u751f\u4ea7\u8005\u53ef\u4ee5\u8fde\u5199\uff0c\u6d88\u8d39\u8005\u53ef\u4ee5\u8fde\u8bfb\u540c\u4e00\u4e2a\u503c\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 Q {\n    int n;\n    synchronized int get() {\n        System.out.println(\"Got: \" + n);\n        return n;\n    }\n    synchronized void put(int n) {\n        this.n = n;\n        System.out.println(\"Put: \" + n);\n    }\n}\nclass Producer implements Runnable {\n    Q q;\n    Producer(Q q) {\n        this.q = q;\n        new Thread(this, \"Producer\").start();\n    }\n    public void run() {\n        int i = 0;\n        while (true) { q.put(i++); }\n    }\n}\nclass Consumer implements Runnable {\n    Q q;\n    Consumer(Q q) {\n        this.q = q;\n        new Thread(this, \"Consumer\").start();\n    }\n    public void run() {\n        while (true) { q.get(); }\n    }\n}\nclass PC {\n    public static void main(String args[]) {\n        Q q = new Q();\n        new Producer(q);\n        new Consumer(q);\n        System.out.println(\"Press Control-C to stop.\");\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u53ef\u80fd\u51fa\u73b0\uff1aPut: 1 \u4e4b\u540e Got: 1 \u8fde\u6253\u4e94\u6b21\uff0c\u7136\u540e Put: 2 \u5230 7 \u6d88\u8d39\u8005\u6839\u672c\u6765\u4e0d\u53ca\u62ff\u3002\u8f93\u51fa\u968f CPU \u548c\u8d1f\u8f7d\u53d8\u5316\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\u6b63\u786e\u5199\u6cd5\uff1await \/ notify \u5bf9\u5411\u63e1\u624b<\/h2>\n<p style=\"margin: 1em 0px;\">\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;\">valueSet<\/code> \u6807\u8bb0\u683c\u5b50\u91cc\u6709\u6ca1\u6709\u65b0\u503c\u3002get \u53d1\u73b0\u6ca1\u6709\u5c31 wait\uff0c\u53d6\u8d70\u540e notify \u751f\u4ea7\u8005\uff1bput \u53d1\u73b0\u8fd8\u6ca1\u88ab\u53d6\u8d70\u5c31 wait\uff0c\u653e\u5165\u540e notify \u6d88\u8d39\u8005\u3002\u6761\u4ef6\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;\">while<\/code> \u800c\u4e0d\u662f if\uff0c\u907f\u514d\u865a\u5047\u5524\u9192\u540e\u72b6\u6001\u5df2\u53d8\u4ecd\u5f80\u4e0b\u8d70\u3002\uff08\u539f\u6587\u6b63\u786e\u793a\u4f8b\u62ec\u53f7\u9519\u4f4d\uff0c\u8fd9\u91cc\u6309\u8bed\u4e49\u4fee\u597d\u3002\uff09<\/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 Q {\n    int n;\n    boolean valueSet = false;\n    synchronized int get() {\n        while (!valueSet) {\n            try { wait(); } catch (InterruptedException e) {\n                System.out.println(\"InterruptedException caught\");\n            }\n        }\n        System.out.println(\"Got: \" + n);\n        valueSet = false;\n        notify();\n        return n;\n    }\n    synchronized void put(int n) {\n        while (valueSet) {\n            try { wait(); } catch (InterruptedException e) {\n                System.out.println(\"InterruptedException caught\");\n            }\n        }\n        this.n = n;\n        valueSet = true;\n        System.out.println(\"Put: \" + n);\n        notify();\n    }\n}\nclass Producer implements Runnable {\n    Q q;\n    Producer(Q q) {\n        this.q = q;\n        new Thread(this, \"Producer\").start();\n    }\n    public void run() {\n        int i = 0;\n        while (true) { q.put(i++); }\n    }\n}\nclass Consumer implements Runnable {\n    Q q;\n    Consumer(Q q) {\n        this.q = q;\n        new Thread(this, \"Consumer\").start();\n    }\n    public void run() {\n        while (true) { q.get(); }\n    }\n}\nclass PCFixed {\n    public static void main(String args[]) {\n        Q q = new Q();\n        new Producer(q);\n        new Consumer(q);\n        System.out.println(\"Press Control-C to stop.\");\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6b63\u786e\u8f93\u51fa\u6210\u5bf9\u51fa\u73b0\uff1aPut: 1 \/ Got: 1 \/ Put: 2 \/ Got: 2 \u2026 \u751f\u4ea7\u8005\u4e0d\u4f1a\u8d85\u8f66\uff0c\u6d88\u8d39\u8005\u4e5f\u4e0d\u4f1a\u628a\u540c\u4e00\u4e2a\u6570\u62ff\u4e24\u904d\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\uff1asynchronized \u53ea\u80fd\u4e92\u65a5\uff0c\u4e0d\u80fd\u8868\u8fbe\u201c\u7b49\u5230\u6709\u6570\u636e\u201d\uff1b\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u8981\u5728\u540c\u6b65\u65b9\u6cd5\u91cc wait \/ notify \u5bf9\u5411\u63e1\u624b\uff0c\u6761\u4ef6\u7528 while \u5224\u65ad\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u6b65\u65b9\u6cd5\u80fd\u6321\u4f4f\u5176\u4ed6\u7ebf\u7a0b\u540c\u65f6\u8fdb\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f46\u8fd8\u4e0d\u591f\u7cbe\u5de7\u3002\u751f\u4ea7\u8005\u4e00\u8fb9\u9020\u6570\u636e\u3001\u6d88\u8d39\u8005\u4e00\u8fb9\u7528\uff0c\u8fd8\u8981\u4e92\u76f8\u7b49\u5bf9\u65b9\u5e72\u5b8c\u2014\u2014\u5982 [&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\/223"}],"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=223"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":311,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions\/311"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}