{"id":162,"date":"2015-04-10T19:31:18","date_gmt":"2015-04-10T11:31:18","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=162"},"modified":"2026-08-18T12:09:28","modified_gmt":"2026-08-18T04:09:28","slug":"java%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=162","title":{"rendered":"Java\u591a\u7ebf\u7a0b\u673a\u5236"},"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;\">\u7ebf\u7a0b\u662f\u4e00\u4e2a\u7a0b\u5e8f\u5185\u90e8\u7684\u987a\u5e8f\u63a7\u5236\u6d41\uff1a\u4e00\u4e2a\u8fdb\u7a0b\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4efb\u52a1\uff0c\u4e00\u6761\u7ebf\u7a0b\u76f8\u5f53\u4e8e\u8fd9\u4e2a\u4efb\u52a1\u91cc\u7684\u4e00\u6761\u6267\u884c\u8def\u5f84\u3002\u591a\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u540c\u65f6\u8dd1\u591a\u4e2a\u7a0b\u5e8f\uff1b<strong style=\"color: #4870ac;\">\u591a\u7ebf\u7a0b<\/strong>\u662f\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u91cc\u6709\u591a\u6761\u987a\u5e8f\u6d41\u540c\u65f6\u6267\u884c\u3002Java \u7684\u7ebf\u7a0b\u901a\u8fc7 <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;\">java.lang.Thread<\/code> \u5b9e\u73b0\u3002<\/p>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">JVM \u542f\u52a8\u65f6\u4f1a\u6709\u4e00\u4e2a\u7531 <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 static void main()<\/code> \u5b9a\u4e49\u7684\u7ebf\u7a0b\u3002\u53ef\u4ee5 new Thread \u5b9e\u4f8b\u6765\u521b\u5efa\u65b0\u7ebf\u7a0b\uff1b\u6bcf\u4e2a\u7ebf\u7a0b\u9760\u5bf9\u5e94\u5bf9\u8c61\u7684 <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;\">run()<\/code> \u5b8c\u6210\u5de5\u4f5c\uff08\u7ebf\u7a0b\u4f53\uff09\uff0c\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;\">start()<\/code> \u624d\u4f1a\u771f\u6b63\u542f\u52a8\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\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;\">\u600e\u4e48\u505a<\/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;\">\u7279\u70b9<\/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;\">\u5b9e\u73b0 Runnable<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">new Thread(target)\uff0ctarget \u662f Runnable<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u591a\u4e2a\u7ebf\u7a0b\u53ef\u5171\u4eab\u540c\u4e00\u4efd\u6570\u636e<\/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;\">\u7ee7\u627f Thread<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5b50\u7c7b\u91cd\u5199 run()\uff0c\u518d new \u5b50\u7c7b\u5bf9\u8c61<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5360\u7528\u4e86\u552f\u4e00\u7684\u7ee7\u627f\u540d\u989d<\/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\u521b\u5efa\u548c\u542f\u52a8<\/h2>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e00\u79cd\uff1a\u5b9a\u4e49\u7c7b\u5b9e\u73b0 Runnable\uff1b<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;\">Runnable<\/code> \u53ea\u6709\u4e00\u4e2a\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;\">public void run()<\/code>\uff0c\u7528\u6765\u5199\u7ebf\u7a0b\u4f53\u3002run \u91cc\u53ef\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;\">Thread.currentThread()<\/code> \u62ff\u5230\u5f53\u524d\u7ebf\u7a0b\u5f15\u7528\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e8c\u79cd\uff1a\u5b9a\u4e49 Thread \u7684\u5b50\u7c7b\u5e76\u91cd\u5199 run\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;\">class MyThread extends Thread {\n    public void run() {\n        \/\/ \u7ebf\u7a0b\u4f53\n    }\n}\nMyThread myThread = new MyThread();<\/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\u7ebf\u7a0b\u63a7\u5236\u7684\u57fa\u672c\u65b9\u6cd5<\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><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;\">isAlive()<\/code>\uff1a\u7ebf\u7a0b\u662f\u5426\u8fd8\u201c\u6d3b\u201d\u7740\u3002<\/section>\n<\/li>\n<li>\n<section><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;\">getPriority()<\/code> \/ <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;\">setPriority()<\/code>\uff1a\u8bfb\u5199\u4f18\u5148\u7ea7\u3002<\/section>\n<\/li>\n<li>\n<section><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()<\/code>\uff1a\u5f53\u524d\u7ebf\u7a0b\u7761\u7720\u6307\u5b9a\u6beb\u79d2\u3002<\/section>\n<\/li>\n<li>\n<section><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>\uff1a\u5f53\u524d\u7ebf\u7a0b\u7b49\u5f85\u8be5\u7ebf\u7a0b\u7ed3\u675f\u518d\u7ee7\u7eed\u3002<\/section>\n<\/li>\n<li>\n<section><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;\">yield()<\/code>\uff1a\u8ba9\u51fa CPU\uff0c\u8fdb\u5165\u5c31\u7eea\u961f\u5217\u7b49\u5f85\u8c03\u5ea6\u3002<\/section>\n<\/li>\n<li>\n<section><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>\uff1a\u5f53\u524d\u7ebf\u7a0b\u8fdb\u5165\u5bf9\u8c61\u7684 wait pool\u3002<\/section>\n<\/li>\n<li>\n<section><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> \/ <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>\uff1a\u5524\u9192 wait pool \u4e2d\u4e00\u4e2a \/ \u5168\u90e8\u7b49\u5f85\u7ebf\u7a0b\u3002<\/section>\n<\/li>\n<\/ul>\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\u7ebf\u7a0b\u95f4\u901a\u4fe1\uff1a\u751f\u4ea7\u8005\u6d88\u8d39\u8005<\/h2>\n<p style=\"margin: 1em 0px;\">\u7528\u4ea7\u54c1\u76d2\uff08\u5bb9\u91cf 6\uff09\u8bf4\u660e wait \/ notify\uff1a\u6ee1\u4e86\u751f\u4ea7\u8005 wait\uff0c\u7a7a\u4e86\u6d88\u8d39\u8005 wait\uff1b\u5bf9\u65b9\u64cd\u4f5c\u540e notify\u3002push \/ pop \u90fd\u52a0 <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;\">synchronized<\/code>\uff0c\u6761\u4ef6\u7528 while \u5224\u65ad\uff0c\u907f\u514d\u88ab\u5524\u9192\u540e\u72b6\u6001\u5df2\u7ecf\u53d8\u4e86\u8fd8\u7ee7\u7eed\u5e72\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;\">public class ProducerConsumer {\n    public static void main(String[] args) {\n        ProductBox pb = new ProductBox();\n        Thread pThread = new Thread(new Producer(pb));\n        Thread cThread = new Thread(new Consumer(pb));\n        pThread.setPriority(Thread.MAX_PRIORITY);\n        pThread.start();\n        cThread.start();\n    }\n}\n\nclass Product {\n    int id;\n    public Product(int id) { this.id = id; }\n    public String toString() { return \"Product:\" + id; }\n}\n\nclass ProductBox {\n    Product[] productbox = new Product[6];\n    int index = 0;\n\n    public synchronized void push(Product p) {\n        while (index == productbox.length) {\n            try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); }\n        }\n        this.notify();\n        productbox[index] = p;\n        index++;\n    }\n\n    public synchronized Product pop() {\n        while (index == 0) {\n            try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); }\n        }\n        this.notify();\n        index--;\n        return productbox[index];\n    }\n}\n\nclass Producer implements Runnable {\n    ProductBox productbox;\n    public Producer(ProductBox productbox) { this.productbox = productbox; }\n    public void run() {\n        for (int i = 0; i &lt; 10; i++) {\n            Product p = new Product(i);\n            productbox.push(p);\n            System.out.println(\"produce:\" + p);\n            try { Thread.sleep((int) (Math.random() * 200)); } catch (InterruptedException e) { e.printStackTrace(); }\n        }\n    }\n}\n\nclass Consumer implements Runnable {\n    ProductBox productbox;\n    public Consumer(ProductBox productbox) { this.productbox = productbox; }\n    public void run() {\n        for (int i = 0; i &lt; 10; i++) {\n            Product p = productbox.pop();\n            System.out.println(\"consume:\" + p);\n            try { Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); }\n        }\n    }\n}<\/code><\/pre>\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\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u91cc\u7684\u4e00\u6761\u6267\u884c\u8def\u5f84\uff0crun \u662f\u7ebf\u7a0b\u4f53\u3001start \u624d\u542f\u52a8\uff1b\u5171\u4eab\u6570\u636e\u7528 Runnable\uff0c\u534f\u4f5c\u9760 synchronized + wait\/notify\uff0c\u6761\u4ef6\u5224\u65ad\u7528 while\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u7a0b\u662f\u4e00\u4e2a\u7a0b\u5e8f\u5185\u90e8\u7684\u987a\u5e8f\u63a7\u5236\u6d41\uff1a\u4e00\u4e2a\u8fdb\u7a0b\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4efb\u52a1\uff0c\u4e00\u6761\u7ebf\u7a0b\u76f8\u5f53\u4e8e\u8fd9\u4e2a\u4efb\u52a1\u91cc\u7684\u4e00\u6761\u6267\u884c\u8def\u5f84\u3002\u591a\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb [&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\/162"}],"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=162"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":2128,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions\/2128"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}