{"id":1565,"date":"2016-04-29T15:00:30","date_gmt":"2016-04-29T07:00:30","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1565"},"modified":"2016-06-02T11:17:31","modified_gmt":"2016-06-02T03:17:31","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":"<div id=\"media\" class=\"rich_media_thumb_wrp\"><img decoding=\"async\" id=\"js_cover\" class=\"rich_media_thumb\" src=\"http:\/\/mmbiz.qpic.cn\/mmbiz\/YE1dmj1Pw7l7GIvibGjWE2G1nia3UuTo7VbY5JoRyCtbYRaUUtjSVf2M1X8mwAgqwiau3zENibs6iaHehj0xUV8twOg\/640?tp=webp&amp;wxfrom=5\" alt=\"\" data-backsrc=\"http:\/\/mmbiz.qpic.cn\/mmbiz\/YE1dmj1Pw7l7GIvibGjWE2G1nia3UuTo7VbY5JoRyCtbYRaUUtjSVf2M1X8mwAgqwiau3zENibs6iaHehj0xUV8twOg\/0\" data-s=\"300,640\" \/><\/div>\n<div id=\"js_content\" class=\"rich_media_content \">\n<p>\u957f\u671f\u4ee5\u6765\uff0c\u591a\u7ebf\u7a0b\u95ee\u9898\u9887\u4e3a\u53d7\u5230\u9762\u8bd5\u5b98\u7684\u9752\u7750\u3002\u867d\u7136\u6211\u4e2a\u4eba\u8ba4\u4e3a\u6211\u4eec\u5f53\u4e2d\u5f88\u5c11\u6709\u4eba\u80fd\u771f\u6b63\u83b7\u5f97\u673a\u4f1a\u5f00\u53d1\u590d\u6742\u7684\u591a\u7ebf\u7a0b\u5e94\u7528(\u5728\u8fc7\u53bb\u7684\u4e03\u5e74\u4e2d\uff0c\u6211\u5f97\u5230\u4e86\u4e00\u4e2a\u673a\u4f1a)\uff0c\u4f46\u662f\u7406\u89e3\u591a\u7ebf\u7a0b\u5bf9\u589e\u52a0\u4f60\u7684\u4fe1\u5fc3\u5f88\u6709\u7528\u3002\u4e4b\u524d\uff0c\u6211\u8ba8\u8bba\u4e86\u4e00\u4e2await()\u548csleep()\u65b9\u6cd5\u533a\u522b\u7684\u95ee\u9898\uff0c\u8fd9\u4e00\u6b21\uff0c\u6211\u5c06\u4f1a\u8ba8\u8bbajoin()\u548cyield()\u65b9\u6cd5\u7684\u533a\u522b\u3002\u5766\u767d\u7684\u8bf4\uff0c\u5b9e\u9645\u4e0a\u6211\u5e76\u6ca1\u6709\u7528\u8fc7\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6709\u4e0d\u6070\u5f53\u7684\u5730\u65b9\uff0c\u8bf7\u63d0\u51fa\u8ba8\u8bba\u3002<\/p>\n<p><strong>Java\u7ebf\u7a0b\u8c03\u5ea6\u7684\u4e00\u70b9\u80cc\u666f<\/strong><\/p>\n<p>\u5728\u5404\u79cd\u5404\u6837\u7684\u7ebf\u7a0b\u4e2d\uff0cJava\u865a\u62df\u673a\u5fc5\u987b\u5b9e\u73b0\u4e00\u4e2a\u6709\u4f18\u5148\u6743\u7684\u3001\u57fa\u4e8e\u4f18\u5148\u7ea7\u7684\u8c03\u5ea6\u7a0b\u5e8f\u3002\u8fd9\u610f\u5473\u7740Java\u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u88ab\u5206\u914d\u5230\u4e00\u5b9a\u7684\u4f18\u5148\u6743\uff0c\u4f7f\u7528\u5b9a\u4e49\u597d\u7684\u8303\u56f4\u5185\u7684\u4e00\u4e2a\u6b63\u6574\u6570\u8868\u793a\u3002\u4f18\u5148\u7ea7\u53ef\u4ee5\u88ab\u5f00\u53d1\u8005\u6539\u53d8\u3002\u5373\u4f7f\u7ebf\u7a0b\u5df2\u7ecf\u8fd0\u884c\u4e86\u4e00\u5b9a\u65f6\u95f4\uff0cJava\u865a\u62df\u673a\u4e5f\u4e0d\u4f1a\u6539\u53d8\u5176\u4f18\u5148\u7ea7<\/p>\n<p>\u4f18\u5148\u7ea7\u7684\u503c\u5f88\u91cd\u8981\uff0c\u56e0\u4e3aJava\u865a\u62df\u673a\u548c\u4e0b\u5c42\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u7ea6\u5b9a\u662f\u64cd\u4f5c\u7cfb\u7edf\u5fc5\u987b\u9009\u62e9\u6709\u6700\u9ad8\u4f18\u5148\u6743\u7684Java\u7ebf\u7a0b\u8fd0\u884c\u3002\u6240\u4ee5\u6211\u4eec\u8bf4Java\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8e\u4f18\u5148\u6743\u7684\u8c03\u5ea6\u7a0b\u5e8f\u3002\u8be5\u8c03\u5ea6\u7a0b\u5e8f\u4f7f\u7528\u4e00\u79cd\u6709\u4f18\u5148\u6743\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u8fd9\u610f\u5473\u7740\u5f53\u4e00\u4e2a\u6709\u66f4\u9ad8\u4f18\u5148\u6743\u7684\u7ebf\u7a0b\u5230\u6765\u65f6\uff0c\u65e0\u8bba\u4f4e\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u662f\u5426\u5728\u8fd0\u884c\uff0c\u90fd\u4f1a\u4e2d\u65ad(\u62a2\u5360)\u5b83\u3002\u8fd9\u4e2a\u7ea6\u5b9a\u5bf9\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u6765\u8bf4\u5e76\u4e0d\u603b\u662f\u8fd9\u6837\uff0c\u8fd9\u610f\u5473\u7740\u64cd\u4f5c\u7cfb\u7edf\u6709\u65f6\u53ef\u80fd\u4f1a\u9009\u62e9\u8fd0\u884c\u4e00\u4e2a\u66f4\u4f4e\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u3002(\u6211\u618e\u6068\u591a\u7ebf\u7a0b\u7684\u8fd9\u4e00\u70b9\uff0c\u56e0\u4e3a\u8fd9\u4e0d\u80fd\u4fdd\u8bc1\u4efb\u4f55\u4e8b\u60c5)<\/p>\n<p>\u6ce8\u610fJava\u5e76\u4e0d\u9650\u5b9a\u7ebf\u7a0b\u662f\u4ee5\u65f6\u95f4\u7247\u8fd0\u884c\uff0c\u4f46\u662f\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u5374\u6709\u8fd9\u6837\u7684\u8981\u6c42\u3002\u5728\u672f\u8bed\u4e2d\u7ecf\u5e38\u5f15\u8d77\u6df7\u6dc6\uff1a\u62a2\u5360\u7ecf\u5e38\u4e0e\u65f6\u95f4\u7247\u6df7\u6dc6\u3002\u4e8b\u5b9e\u4e0a\uff0c\u62a2\u5360\u610f\u5473\u7740\u53ea\u6709\u62e5\u6709\u9ad8\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u53ef\u4ee5\u4f18\u5148\u4e8e\u4f4e\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u6267\u884c\uff0c\u4f46\u662f\u5f53\u7ebf\u7a0b\u62e5\u6709\u76f8\u540c\u4f18\u5148\u7ea7\u7684\u65f6\u5019\uff0c\u4ed6\u4eec\u4e0d\u80fd\u76f8\u4e92\u62a2\u5360\u3002\u5b83\u4eec\u901a\u5e38\u53d7\u65f6\u95f4\u7247\u7ba1\u5236\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662fJava\u7684\u8981\u6c42\u3002<\/p>\n<p><strong>\u7406\u89e3\u7ebf\u7a0b\u7684\u4f18\u5148\u6743<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u7406\u89e3\u7ebf\u7a0b\u4f18\u5148\u7ea7\u662f\u591a\u7ebf\u7a0b\u5b66\u4e60\u5f88\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u5c24\u5176\u662f\u4e86\u89e3yield()\u51fd\u6570\u7684\u5de5\u4f5c\u8fc7\u7a0b\u3002<\/p>\n<ol class=\" list-paddingleft-2\">\n<li>\u8bb0\u4f4f\u5f53\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u6ca1\u6709\u6307\u5b9a\u65f6\uff0c\u6240\u6709\u7ebf\u7a0b\u90fd\u643a\u5e26\u666e\u901a\u4f18\u5148\u7ea7\u3002<\/li>\n<li>\u4f18\u5148\u7ea7\u53ef\u4ee5\u7528\u4ece1\u523010\u7684\u8303\u56f4\u6307\u5b9a\u300210\u8868\u793a\u6700\u9ad8\u4f18\u5148\u7ea7\uff0c1\u8868\u793a\u6700\u4f4e\u4f18\u5148\u7ea7\uff0c5\u662f\u666e\u901a\u4f18\u5148\u7ea7\u3002<\/li>\n<li>\u8bb0\u4f4f\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u7ebf\u7a0b\u5728\u6267\u884c\u65f6\u88ab\u7ed9\u4e88\u4f18\u5148\u3002\u4f46\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5728\u542f\u52a8\u65f6\u5c31\u8fdb\u5165\u8fd0\u884c\u72b6\u6001\u3002<\/li>\n<li>\u4e0e\u5728\u7ebf\u7a0b\u6c60\u4e2d\u7b49\u5f85\u8fd0\u884c\u673a\u4f1a\u7684\u7ebf\u7a0b\u76f8\u6bd4\uff0c\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\u53ef\u80fd\u603b\u662f\u62e5\u6709\u66f4\u9ad8\u7684\u4f18\u5148\u7ea7\u3002<\/li>\n<li>\u7531\u8c03\u5ea6\u7a0b\u5e8f\u51b3\u5b9a\u54ea\u4e00\u4e2a\u7ebf\u7a0b\u88ab\u6267\u884c\u3002<\/li>\n<li>t.setPriority()\u7528\u6765\u8bbe\u5b9a\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u3002<\/li>\n<li>\u8bb0\u4f4f\u5728\u7ebf\u7a0b\u5f00\u59cb\u65b9\u6cd5\u88ab\u8c03\u7528\u4e4b\u524d\uff0c\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u5e94\u8be5\u88ab\u8bbe\u5b9a\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5e38\u91cf\uff0c\u5982MIN_PRIORITY,MAX_PRIORITY\uff0cNORM_PRIORITY\u6765\u8bbe\u5b9a\u4f18\u5148\u7ea7<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u5bf9\u7ebf\u7a0b\u8c03\u5ea6\u548c\u7ebf\u7a0b\u4f18\u5148\u7ea7\u6709\u4e00\u5b9a\u7406\u89e3\u540e\uff0c\u8ba9\u6211\u4eec\u8fdb\u5165\u4e3b\u9898\u3002<\/p>\n<p><strong>yield()\u65b9\u6cd5<\/strong><\/p>\n<p>\u7406\u8bba\u4e0a\uff0cyield\u610f\u5473\u7740\u653e\u624b\uff0c\u653e\u5f03\uff0c\u6295\u964d\u3002\u4e00\u4e2a\u8c03\u7528yield()\u65b9\u6cd5\u7684\u7ebf\u7a0b\u544a\u8bc9\u865a\u62df\u673a\u5b83\u4e50\u610f\u8ba9\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528\u81ea\u5df1\u7684\u4f4d\u7f6e\u3002\u8fd9\u8868\u660e\u8be5\u7ebf\u7a0b\u6ca1\u6709\u5728\u505a\u4e00\u4e9b\u7d27\u6025\u7684\u4e8b\u60c5\u3002\u6ce8\u610f\uff0c\u8fd9\u4ec5\u662f\u4e00\u4e2a\u6697\u793a\uff0c\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u5f71\u54cd\u3002<\/p>\n<p>\u5728Thread.java\u4e2dyield()\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<p>\/**<\/p>\n<p>* A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore<\/p>\n<p>* this hint. Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilize a CPU.<\/p>\n<p>* Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.<\/p>\n<p>*\/<\/p>\n<p>public static native void yield();<\/p>\n<p>\u8ba9\u6211\u4eec\u5217\u4e3e\u4e00\u4e0b\u5173\u4e8e\u4ee5\u4e0a\u5b9a\u4e49\u91cd\u8981\u7684\u51e0\u70b9\uff1a<\/p>\n<ul class=\" list-paddingleft-2\">\n<li>Yield\u662f\u4e00\u4e2a\u9759\u6001\u7684\u539f\u751f(native)\u65b9\u6cd5<\/li>\n<li>Yield\u544a\u8bc9\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u7ebf\u7a0b\u628a\u8fd0\u884c\u673a\u4f1a\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u4e2d\u62e5\u6709\u76f8\u540c\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u3002<\/li>\n<li>Yield\u4e0d\u80fd\u4fdd\u8bc1\u4f7f\u5f97\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\u8fc5\u901f\u8f6c\u6362\u5230\u53ef\u8fd0\u884c\u7684\u72b6\u6001<\/li>\n<li>\u5b83\u4ec5\u80fd\u4f7f\u4e00\u4e2a\u7ebf\u7a0b\u4ece\u8fd0\u884c\u72b6\u6001\u8f6c\u5230\u53ef\u8fd0\u884c\u72b6\u6001\uff0c\u800c\u4e0d\u662f\u7b49\u5f85\u6216\u963b\u585e\u72b6\u6001<\/li>\n<\/ul>\n<p><strong>yield()\u65b9\u6cd5\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<p>\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u6211\u968f\u610f\u7684\u521b\u5efa\u4e86\u540d\u4e3a\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7684\u4e24\u4e2a\u7ebf\u7a0b\u3002\u751f\u4ea7\u8005\u8bbe\u5b9a\u4e3a\u6700\u5c0f\u4f18\u5148\u7ea7\uff0c\u6d88\u8d39\u8005\u8bbe\u5b9a\u4e3a\u6700\u9ad8\u4f18\u5148\u7ea7\u3002\u5728Thread.yield()\u6ce8\u91ca\u548c\u975e\u6ce8\u91ca\u7684\u60c5\u51b5\u4e0b\u6211\u5c06\u5206\u522b\u8fd0\u884c\u8be5\u7a0b\u5e8f\u3002\u6ca1\u6709\u8c03\u7528yield()\u65b9\u6cd5\u65f6\uff0c\u867d\u7136\u8f93\u51fa\u6709\u65f6\u6539\u53d8\uff0c\u4f46\u662f\u901a\u5e38\u6d88\u8d39\u8005\u884c\u5148\u6253\u5370\u51fa\u6765\uff0c\u7136\u540e\u4e8b\u751f\u4ea7\u8005\u3002<\/p>\n<p>\u8c03\u7528yield()\u65b9\u6cd5\u65f6\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u4f9d\u6b21\u6253\u5370\uff0c\u7136\u540e\u5c06\u6267\u884c\u673a\u4f1a\u4ea4\u7ed9\u5bf9\u65b9\uff0c\u4e00\u76f4\u8fd9\u6837\u8fdb\u884c\u4e0b\u53bb\u3002<\/p>\n<p>[java]<br \/>\npackage test.core.threads;<\/p>\n<p>public class YieldExample<\/p>\n<p>{<\/p>\n<p>public static void main(String[] args)<\/p>\n<p>{<\/p>\n<p>Thread producer = new Producer();<\/p>\n<p>Thread consumer = new Consumer();<\/p>\n<p>producer.setPriority(Thread.MIN_PRIORITY); \/\/Min Priority<\/p>\n<p>consumer.setPriority(Thread.MAX_PRIORITY); \/\/Max Priority<\/p>\n<p>producer.start();<\/p>\n<p>consumer.start();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Producer extends Thread<\/p>\n<p>{<\/p>\n<p>public void run()<\/p>\n<p>{<\/p>\n<p>for (int i = 0; i &amp;lt; 5; i++)<\/p>\n<p>{<\/p>\n<p>System.out.println(&quot;I am Producer : Produced Item &quot; + i);<\/p>\n<p>Thread.yield();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class Consumer extends Thread<\/p>\n<p>{<\/p>\n<p>public void run()<\/p>\n<p>{<\/p>\n<p>for (int i = 0; i &amp;lt; 5; i++)<\/p>\n<p>{<\/p>\n<p>System.out.println(&quot;I am Consumer : Consumed Item &quot; + i);<\/p>\n<p>Thread.yield();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&lt;strong&gt;\u4e0a\u8ff0\u7a0b\u5e8f\u5728\u6ca1\u6709\u8c03\u7528yield()\u65b9\u6cd5\u60c5\u51b5\u4e0b\u7684\u8f93\u51fa\uff1a&lt;\/strong&gt;<\/p>\n<p>I am Consumer : Consumed Item 0<\/p>\n<p>I am Consumer : Consumed Item 1<\/p>\n<p>I am Consumer : Consumed Item 2<\/p>\n<p>I am Consumer : Consumed Item 3<\/p>\n<p>I am Consumer : Consumed Item 4<\/p>\n<p>I am Producer : Produced Item 0<\/p>\n<p>I am Producer : Produced Item 1<\/p>\n<p>I am Producer : Produced Item 2<\/p>\n<p>I am Producer : Produced Item 3<\/p>\n<p>I am Producer : Produced Item 4<\/p>\n<p>&lt;strong&gt;\u4e0a\u8ff0\u7a0b\u5e8f\u5728\u8c03\u7528yield()\u65b9\u6cd5\u60c5\u51b5\u4e0b\u7684\u8f93\u51fa\uff1a&lt;\/strong&gt;<\/p>\n<p>I am Producer : Produced Item 0<\/p>\n<p>I am Consumer : Consumed Item 0<\/p>\n<p>I am Producer : Produced Item 1<\/p>\n<p>I am Consumer : Consumed Item 1<\/p>\n<p>I am Producer : Produced Item 2<\/p>\n<p>I am Consumer : Consumed Item 2<\/p>\n<p>I am Producer : Produced Item 3<\/p>\n<p>I am Consumer : Consumed Item 3<\/p>\n<p>I am Producer : Produced Item 4<\/p>\n<p>I am Consumer : Consumed Item 4[\/java]<\/p>\n<p><strong>join()\u65b9\u6cd5<\/strong><\/p>\n<p>\u7ebf\u7a0b\u5b9e\u4f8b\u7684\u65b9\u6cd5join()\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u5f97\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7684\u6267\u884c\u7ed3\u675f\u540e\u518d\u5f00\u59cb\u6267\u884c\u8fd9\u4e2a\u7ebf\u7a0b\u3002\u5982\u679cjoin()\u65b9\u6cd5\u88ab\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5b9e\u4f8b\u4e0a\u8c03\u7528\uff0c\u5f53\u524d\u8fd0\u884c\u7740\u7684\u7ebf\u7a0b\u5c06\u963b\u585e\u76f4\u5230\u7ebf\u7a0b\u5b9e\u4f8b\u5b8c\u6210\u4e86\u6267\u884c\u3002<\/p>\n<p>\/\/Waits for this thread to die.<\/p>\n<p>public final void join() throws InterruptedException<\/p>\n<p>\u5728join()\u65b9\u6cd5\u5185\u8bbe\u5b9a\u8d85\u65f6\uff0c\u4f7f\u5f97join()\u65b9\u6cd5\u7684\u5f71\u54cd\u5728\u7279\u5b9a\u8d85\u65f6\u540e\u65e0\u6548\u3002\u5f53\u8d85\u65f6\u65f6\uff0c\u4e3b\u65b9\u6cd5\u548c\u4efb\u52a1\u7ebf\u7a0b\u7533\u8bf7\u8fd0\u884c\u7684\u65f6\u5019\u662f\u5e73\u7b49\u7684\u3002\u7136\u800c\uff0c\u5f53\u6d89\u53casleep\u65f6\uff0cjoin()\u65b9\u6cd5\u4f9d\u9760\u64cd\u4f5c\u7cfb\u7edf\u8ba1\u65f6\uff0c\u6240\u4ee5\u4f60\u4e0d\u5e94\u8be5\u5047\u5b9ajoin()\u65b9\u6cd5\u5c06\u4f1a\u7b49\u5f85\u4f60\u6307\u5b9a\u7684\u65f6\u95f4\u3002<\/p>\n<p>\u50cfsleep,join\u901a\u8fc7\u629b\u51faInterruptedException\u5bf9\u4e2d\u65ad\u505a\u51fa\u56de\u5e94\u3002<\/p>\n<p><strong>join()\u65b9\u6cd5\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<p>[java]package test.core.threads;<\/p>\n<p>public class JoinExample<\/p>\n<p>{<\/p>\n<p>public static void main(String[] args) throws InterruptedException<\/p>\n<p>{<\/p>\n<p>Thread t = new Thread(new Runnable()<\/p>\n<p>{<\/p>\n<p>public void run()<\/p>\n<p>{<\/p>\n<p>System.out.println(&quot;First task started&quot;);<\/p>\n<p>System.out.println(&quot;Sleeping for 2 seconds&quot;);<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>Thread.sleep(2000);<\/p>\n<p>} catch (InterruptedException e)<\/p>\n<p>{<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>System.out.println(&quot;First task completed&quot;);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>Thread t1 = new Thread(new Runnable()<\/p>\n<p>{<\/p>\n<p>public void run()<\/p>\n<p>{<\/p>\n<p>System.out.println(&quot;Second task completed&quot;);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>t.start(); \/\/ Line 15<\/p>\n<p>t.join(); \/\/ Line 16<\/p>\n<p>t1.start();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Output:<\/p>\n<p>First task started<\/p>\n<p>Sleeping for 2 seconds<\/p>\n<p>First task completed<\/p>\n<p>Second task completed[\/java]<\/p>\n<p>\u8fd9\u662f\u4e00\u4e9b\u5f88\u5c0f\u5374\u5f88\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5728\u8bc4\u8bba\u90e8\u5206\u8ba9\u6211\u77e5\u9053\u4f60\u7684\u60f3\u6cd5\u3002<\/p>\n<p>\uff08\u8f6c\u81eaImportNew\uff09<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u957f\u671f\u4ee5\u6765\uff0c\u591a\u7ebf\u7a0b\u95ee\u9898\u9887\u4e3a\u53d7\u5230\u9762\u8bd5\u5b98\u7684\u9752\u7750\u3002\u867d\u7136\u6211\u4e2a\u4eba\u8ba4\u4e3a\u6211\u4eec\u5f53\u4e2d\u5f88\u5c11\u6709\u4eba\u80fd\u771f\u6b63\u83b7\u5f97\u673a\u4f1a\u5f00\u53d1\u590d\u6742\u7684\u591a\u7ebf\u7a0b\u5e94\u7528(\u5728 [&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":[],"_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":1,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":1566,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1565\/revisions\/1566"}],"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}]}}