{"id":1565,"date":"2016-04-29T15:00:30","date_gmt":"2016-04-29T07:00:30","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1565"},"modified":"2026-08-30T19:33:22","modified_gmt":"2026-08-30T11:33:22","slug":"java%e7%ba%bf%e7%a8%8b%e4%b8%adyield%e4%b8%8ejoin%e6%96%b9%e6%b3%95%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1565","title":{"rendered":"Java\u7ebf\u7a0b\u4e2dyield\u4e0ejoin\u65b9\u6cd5\u7684\u533a\u522b"},"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\u7ebf\u7a0b\u95ee\u9898\u5e38\u88ab\u9762\u8bd5\u5b98\u95ee\u5230\u3002\u5373\u4fbf\u65e5\u5e38\u5f88\u5c11\u5199\u590d\u6742\u5e76\u53d1\uff0c\u5f04\u6e05\u8c03\u5ea6\u548c\u4f18\u5148\u7ea7\u4e5f\u80fd\u589e\u52a0\u4fe1\u5fc3\u3002\u6b64\u524d\u8ba8\u8bba\u8fc7 wait() \u548c sleep() \u7684\u533a\u522b\uff0c\u8fd9\u91cc\u5bf9\u6bd4 <strong style=\"color: #4870ac;\">join()<\/strong> \u4e0e <strong style=\"color: #4870ac;\">yield()<\/strong>\u3002\u539f\u6587\u8f6c\u81ea ImportNew\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;\">\u4e00\u3001Java \u7ebf\u7a0b\u8c03\u5ea6\u80cc\u666f<\/h2>\n<p style=\"margin: 1em 0px;\">JVM \u5fc5\u987b\u5b9e\u73b0\u4e00\u4e2a\u57fa\u4e8e\u4f18\u5148\u7ea7\u7684\u8c03\u5ea6\u7a0b\u5e8f\uff1a\u6bcf\u4e2a\u7ebf\u7a0b\u6709\u4e00\u4e2a\u6b63\u6574\u6570\u4f18\u5148\u7ea7\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6539\uff0cJVM \u4e0d\u4f1a\u56e0\u4e3a\u7ebf\u7a0b\u5df2\u7ecf\u8dd1\u4e86\u4e00\u6bb5\u65f6\u95f4\u5c31\u64c5\u81ea\u6539\u4f18\u5148\u7ea7\u3002\u7ea6\u5b9a\u662f\u64cd\u4f5c\u7cfb\u7edf\u5e94\u9009\u62e9\u5f53\u524d\u6700\u9ad8\u4f18\u5148\u7ea7\u7684 Java \u7ebf\u7a0b\u8fd0\u884c\uff1b\u66f4\u9ad8\u4f18\u5148\u7ea7\u5230\u6765\u65f6\u53ef\u4ee5\u62a2\u5360\u4f4e\u4f18\u5148\u7ea7\u3002\u8fd9\u4e2a\u7ea6\u5b9a\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u5e76\u4e0d\u603b\u662f\u6210\u7acb\uff0c\u6709\u65f6\u4ecd\u53ef\u80fd\u8dd1\u4f4e\u4f18\u5148\u7ea7\u7ebf\u7a0b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Java \u5e76\u4e0d\u9650\u5b9a\u5fc5\u987b\u7528\u65f6\u95f4\u7247\uff0c\u4f46\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u3002\u62a2\u5360\u548c\u65f6\u95f4\u7247\u5e38\u88ab\u6df7\u6dc6\uff1a\u62a2\u5360\u6307\u9ad8\u4f18\u5148\u7ea7\u53ef\u4ee5\u538b\u8fc7\u4f4e\u4f18\u5148\u7ea7\uff1b\u540c\u4f18\u5148\u7ea7\u4e4b\u95f4\u4e0d\u80fd\u4e92\u76f8\u62a2\u5360\uff0c\u901a\u5e38\u9760\u65f6\u95f4\u7247\u8f6e\u8f6c\uff0c\u4f46\u90a3\u4e0d\u662f Java \u89c4\u8303\u7684\u786c\u6027\u8981\u6c42\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\u7406\u89e3\u7ebf\u7a0b\u4f18\u5148\u7ea7<\/h2>\n<p style=\"margin: 1em 0px;\">\u5c24\u5176\u5bf9\u7406\u89e3 yield() \u5f88\u91cd\u8981\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u672a\u6307\u5b9a\u65f6\uff0c\u7ebf\u7a0b\u5e26\u666e\u901a\u4f18\u5148\u7ea7\u3002<\/section>\n<\/li>\n<li>\n<section>\u8303\u56f4 1 \u5230 10\uff1a10 \u6700\u9ad8\uff0c1 \u6700\u4f4e\uff0c5 \u666e\u901a\u3002<\/section>\n<\/li>\n<li>\n<section>\u6700\u9ad8\u4f18\u5148\u7ea7\u5728\u6267\u884c\u65f6\u88ab\u7ed9\u4e88\u4f18\u5148\uff0c\u4f46\u4e0d\u4fdd\u8bc1 start \u540e\u7acb\u523b\u8fdb\u5165\u8fd0\u884c\u3002<\/section>\n<\/li>\n<li>\n<section>\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\uff0c\u76f8\u5bf9\u7ebf\u7a0b\u6c60\u91cc\u7b49\u5f85\u7684\u7ebf\u7a0b\uff0c\u5f80\u5f80\u62e5\u6709\u66f4\u9ad8\u6216\u81f3\u5c11\u4e0d\u66f4\u4f4e\u7684\u4f18\u5148\u7ea7\u3002<\/section>\n<\/li>\n<li>\n<section>\u7531\u8c03\u5ea6\u7a0b\u5e8f\u51b3\u5b9a\u8c01\u771f\u6b63\u6267\u884c\uff1b\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;\">t.setPriority()<\/code> \u8bbe\u5b9a\uff0c\u6700\u597d\u5728 start \u4e4b\u524d\u3002<\/section>\n<\/li>\n<li>\n<section>\u53ef\u7528\u5e38\u91cf MIN_PRIORITY\u3001MAX_PRIORITY\u3001NORM_PRIORITY\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\u3001yield() \u65b9\u6cd5<\/h2>\n<p style=\"margin: 1em 0px;\">yield \u7684\u610f\u601d\u662f\u653e\u624b\u3001\u653e\u5f03\u3002\u8c03\u7528\u5b83\u7b49\u4e8e\u544a\u8bc9\u865a\u62df\u673a\uff1a\u6211\u4e50\u610f\u8ba9\u51fa\u5904\u7406\u5668\u3002\u8fd9\u53ea\u662f\u6697\u793a\uff0c\u8c03\u5ea6\u5668\u53ef\u4ee5\u5ffd\u7565\u3002Thread \u4e2d\u5b9a\u4e49\u4e3a\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;\">public static native void yield();<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u9759\u6001 native \u65b9\u6cd5\u3002<\/section>\n<\/li>\n<li>\n<section>\u63d0\u793a\u628a\u8fd0\u884c\u673a\u4f1a\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u4e2d\u76f8\u540c\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u3002<\/section>\n<\/li>\n<li>\n<section>\u4e0d\u80fd\u4fdd\u8bc1\u5f53\u524d\u7ebf\u7a0b\u7acb\u523b\u56de\u5230\u53ef\u8fd0\u884c\u72b6\u6001\u3002<\/section>\n<\/li>\n<li>\n<section>\u53ea\u4ece\u8fd0\u884c\u8f6c\u5230\u53ef\u8fd0\u884c\uff0c\u4e0d\u4f1a\u8fdb\u5165\u7b49\u5f85\u6216\u963b\u585e\u3002<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u793a\u4f8b\uff1a\u751f\u4ea7\u8005\u8bbe\u4e3a\u6700\u4f4e\u4f18\u5148\u7ea7\uff0c\u6d88\u8d39\u8005\u6700\u9ad8\u3002\u4e0d\u8c03\u7528 yield \u65f6\uff0c\u901a\u5e38\u6d88\u8d39\u8005\u5148\u5168\u90e8\u6253\u5b8c\u518d\u6253\u751f\u4ea7\u8005\uff1b\u8c03\u7528 yield \u540e\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u66f4\u53ef\u80fd\u4ea4\u66ff\u6253\u5370\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;\">package test.core.threads;\npublic class YieldExample {\n    public static void main(String[] args) {\n        Thread producer = new Producer();\n        Thread consumer = new Consumer();\n        producer.setPriority(Thread.MIN_PRIORITY);\n        consumer.setPriority(Thread.MAX_PRIORITY);\n        producer.start();\n        consumer.start();\n    }\n}\nclass Producer extends Thread {\n    public void run() {\n        for (int i = 0; i &lt; 5; i++) {\n            System.out.println(\"I am Producer : Produced Item \" + i);\n            Thread.yield();\n        }\n    }\n}\nclass Consumer extends Thread {\n    public void run() {\n        for (int i = 0; i &lt; 5; i++) {\n            System.out.println(\"I am Consumer : Consumed Item \" + i);\n            Thread.yield();\n        }\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;\">\u56db\u3001join() \u65b9\u6cd5<\/h2>\n<p style=\"margin: 1em 0px;\">\u5728\u67d0\u4e2a\u7ebf\u7a0b\u5b9e\u4f8b\u4e0a\u8c03\u7528 join()\uff0c\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u8be5\u5b9e\u4f8b\u6267\u884c\u7ed3\u675f\uff08\u201c\u7b49\u8fd9\u6761\u7ebf\u7a0b\u6b7b\u6389\u201d\uff09\u3002\u4e5f\u53ef\u4ee5\u8bbe\u8d85\u65f6\uff0c\u8d85\u65f6\u540e\u4e3b\u7ebf\u7a0b\u4e0e\u4efb\u52a1\u7ebf\u7a0b\u91cd\u65b0\u5e73\u7b49\u7ade\u4e89\u3002join \u4f9d\u8d56\u64cd\u4f5c\u7cfb\u7edf\u8ba1\u65f6\uff0c\u4e0d\u8981\u5047\u5b9a\u5b83\u7cbe\u786e\u7b49\u5230\u4f60\u6307\u5b9a\u7684\u6beb\u79d2\u6570\u3002\u548c sleep \u4e00\u6837\uff0cjoin \u901a\u8fc7\u629b\u51fa InterruptedException \u54cd\u5e94\u4e2d\u65ad\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 final void join() throws InterruptedException\n\npackage test.core.threads;\npublic class JoinExample {\n    public static void main(String[] args) throws InterruptedException {\n        Thread t = new Thread(new Runnable() {\n            public void run() {\n                System.out.println(\"First task started\");\n                System.out.println(\"Sleeping for 2 seconds\");\n                try {\n                    Thread.sleep(2000);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n                System.out.println(\"First task completed\");\n            }\n        });\n        Thread t1 = new Thread(new Runnable() {\n            public void run() {\n                System.out.println(\"Second task completed\");\n            }\n        });\n        t.start();  \/\/ Line 15\n        t.join();   \/\/ Line 16\uff1a\u7b49 t \u7ed3\u675f\u518d\u542f\u52a8 t1\n        t1.start();\n    }\n}\n\/\/ Output:\n\/\/ First task started\n\/\/ Sleeping for 2 seconds\n\/\/ First task completed\n\/\/ Second task completed<\/code><\/pre>\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<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;\">\u4fdd\u8bc1\uff1f<\/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;\">yield()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u63d0\u793a\u8ba9\u51fa CPU \u7ed9\u540c\u4f18\u5148\u7ea7\u7ebf\u7a0b<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e0d\u4fdd\u8bc1\uff0c\u8c03\u5ea6\u5668\u53ef\u5ffd\u7565<\/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;\">join()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5f53\u524d\u7ebf\u7a0b\u963b\u585e\u76f4\u5230\u76ee\u6807\u7ebf\u7a0b\u7ed3\u675f<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4f1a\u7b49\u5230\u7ed3\u675f\uff08\u53ef\u8bbe\u8d85\u65f6\uff09<\/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\uff1ayield \u53ea\u662f\u8ba9\u51fa CPU \u7684\u6697\u793a\uff0cjoin \u662f\u5b9e\u6253\u5b9e\u7b49\u5230\u53e6\u4e00\u6761\u7ebf\u7a0b\u7ed3\u675f\uff1b\u4f18\u5148\u7ea7\u80fd\u5f71\u54cd\u8c03\u5ea6\uff0c\u4f46\u4e0d\u80fd\u4fdd\u8bc1\u987a\u5e8f\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b\u95ee\u9898\u5e38\u88ab\u9762\u8bd5\u5b98\u95ee\u5230\u3002\u5373\u4fbf\u65e5\u5e38\u5f88\u5c11\u5199\u590d\u6742\u5e76\u53d1\uff0c\u5f04\u6e05\u8c03\u5ea6\u548c\u4f18\u5148\u7ea7\u4e5f\u80fd\u589e\u52a0\u4fe1\u5fc3\u3002\u6b64\u524d\u8ba8\u8bba\u8fc7 wait() \u548c  [&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\/1565"}],"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=1565"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":3905,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1565\/revisions\/3905"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}