{"id":683,"date":"2015-05-21T16:59:10","date_gmt":"2015-05-21T08:59:10","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=683"},"modified":"2015-05-25T10:33:28","modified_gmt":"2015-05-25T02:33:28","slug":"%e7%ba%bf%e7%a8%8b%e7%bb%93%e6%9d%9f%e7%ad%89%e5%be%85%e4%b8%8e%e7%ba%bf%e7%a8%8b%e4%bc%98%e5%8c%96%e7%ba%a7","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=683","title":{"rendered":"\u7ebf\u7a0b\u7ed3\u675f\u7b49\u5f85(join)\u4e0e\u7ebf\u7a0b\u4f18\u5148\u7ea7(Priority)"},"content":{"rendered":"<p>\u901a\u5e38\u4f60\u5e0c\u671b\u4e3b\u7ebf\u7a0b\u6700\u540e\u7ed3\u675f\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u70b9\u662f\u901a\u8fc7\u5728main()\u4e2d\u8c03\u7528sleep()\u6765\u5b9e\u73b0\u7684\uff0c\u7ecf\u8fc7\u8db3\u591f\u957f\u65f6\u95f4\u7684\u5ef6\u8fdf\u4ee5\u786e\u4fdd\u6240\u6709\u5b50\u7ebf\u7a0b\u90fd\u5148\u4e8e\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u3002\u7136\u800c\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u4ee4\u4eba\u6ee1\u610f\u7684\u89e3\u51b3\u65b9\u6cd5\uff0c\u5b83\u4e5f\u5e26\u6765\u4e00\u4e2a\u5927\u95ee\u9898\uff1a\u4e00\u4e2a\u7ebf\u7a0b\u5982\u4f55\u77e5\u9053\u53e6\u4e00\u7ebf\u7a0b\u5df2\u7ecf\u7ed3\u675f\uff1f\u5e78\u8fd0\u7684\u662f\uff0cThread\u7c7b\u63d0\u4f9b\u4e86\u56de\u7b54\u6b64\u95ee\u9898\u7684\u65b9\u6cd5\u3002<br \/>\n<!--more--><\/p>\n<p>\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5224\u5b9a\u4e00\u4e2a\u7ebf\u7a0b\u662f\u5426\u7ed3\u675f\u3002<br \/>\n\u7b2c\u4e00\uff0c\u53ef\u4ee5\u5728\u7ebf\u7a0b\u4e2d\u8c03\u7528isAlive()\u3002\u8fd9\u79cd\u65b9\u6cd5\u7531Thread\u5b9a\u4e49\uff0c\u5b83\u7684\u901a\u5e38\u5f62\u5f0f\u5982\u4e0b\uff1a<br \/>\nfinal boolean isAlive( )<br \/>\n\u5982\u679c\u6240\u8c03\u7528\u7ebf\u7a0b\u4ecd\u5728\u8fd0\u884c\uff0cisAlive()\u65b9\u6cd5\u8fd4\u56detrue\uff0c\u5982\u679c\u4e0d\u662f\u5219\u8fd4\u56defalse\u3002<br \/>\n\u4f46isAlive\uff08\uff09\u5f88\u5c11\u7528\u5230\uff0c\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\u7684\u66f4\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u8c03\u7528join()\uff0c\u63cf\u8ff0\u5982\u4e0b\uff1a<br \/>\nfinal void join( ) throws InterruptedException<br \/>\n\u8be5\u65b9\u6cd5\u7b49\u5f85\u6240\u8c03\u7528\u7ebf\u7a0b\u7ed3\u675f\u3002\u8be5\u540d\u5b57\u6765\u81ea\u4e8e\u8981\u6c42\u7ebf\u7a0b\u7b49\u5f85\u76f4\u5230\u6307\u5b9a\u7ebf\u7a0b\u53c2\u4e0e\u7684\u6982\u5ff5\u3002join()\u7684\u9644\u52a0\u5f62\u5f0f\u5141\u8bb8\u7ed9\u7b49\u5f85\u6307\u5b9a\u7ebf\u7a0b\u7ed3\u675f\u5b9a\u4e49\u4e00\u4e2a\u6700\u5927\u65f6\u95f4\u3002\u4e0b\u9762\u662f\u524d\u9762\u4f8b\u5b50\u7684\u6539\u8fdb\u7248\u672c\u3002\u8fd0\u7528join()\u4ee5\u786e\u4fdd\u4e3b\u7ebf\u7a0b\u6700\u540e\u7ed3\u675f\u3002\u540c\u6837\uff0c\u5b83\u4e5f\u6f14\u793a\u4e86isAlive()\u65b9\u6cd5\u3002<\/p>\n<p>[java]\/\/ Using join() to wait for threads to finish.<br \/>\nclass NewThread implements Runnable {<br \/>\n    String name; \/\/ name of thread<br \/>\n    Thread t;<br \/>\n   NewThread(String threadname) {<br \/>\n        name = threadname;<br \/>\n        t = new Thread(this, name);<br \/>\n       System.out.println(&quot;New thread: &quot; + t);<br \/>\n       t.start(); \/\/ Start the thread<br \/>\n    }<br \/>\n    \/\/ This is the entry point for thread.<br \/>\n    public void run() {<br \/>\n        try {<br \/>\n           for(int i = 5; i &gt; 0; i&#8211;) {<br \/>\n              System.out.println(name + &quot;: &quot; + i);<br \/>\n              Thread.sleep(1000);<br \/>\n           }<br \/>\n        } catch (InterruptedException e) {<br \/>\n           System.out.println(name + &quot; interrupted.&quot;);<br \/>\n       }<br \/>\n       System.out.println(name + &quot; exiting.&quot;);<br \/>\n    }<br \/>\n}<br \/>\nclass DemoJoin {<br \/>\n    public static void main(String args[]) {<br \/>\n       NewThread ob1 = new NewThread(&quot;One&quot;);<br \/>\n       NewThread ob2 = new NewThread(&quot;Two&quot;);<br \/>\n       NewThread ob3 = new NewThread(&quot;Three&quot;);<br \/>\n       System.out.println(&quot;Thread One is alive: &quot;+ ob1.t.isAlive());<br \/>\n       System.out.println(&quot;Thread Two is alive: &quot;+ ob2.t.isAlive());<br \/>\n       System.out.println(&quot;Thread Three is alive: &quot;+ ob3.t.isAlive());<br \/>\n        \/\/ wait for threads to finish<br \/>\n        try {<br \/>\n           System.out.println(&quot;Waiting for threads to finish.&quot;);<br \/>\n           ob1.t.join();<br \/>\n           ob2.t.join();<br \/>\n           ob3.t.join();<br \/>\n        } catch (InterruptedException e) {<br \/>\n           System.out.println(&quot;Main thread Interrupted&quot;);<br \/>\n       }<br \/>\n       System.out.println(&quot;Thread One is alive: &quot;+ ob1.t.isAlive());<br \/>\n       System.out.println(&quot;Thread Two is alive: &quot;+ ob2.t.isAlive());<br \/>\n       System.out.println(&quot;Thread Three is alive: &quot;+ ob3.t.isAlive());<br \/>\n       System.out.println(&quot;Main thread exiting.&quot;);<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>\u7a0b\u5e8f\u8f93\u51fa\u5982\u4e0b\u6240\u793a\uff1a<br \/>\nNew thread: Thread[One,5,main]<br \/>\nNew thread: Thread[Two,5,main]<br \/>\nNew thread: Thread[Three,5,main]<br \/>\nThread One is alive: true<br \/>\nThread Two is alive: true<br \/>\nThread Three is alive: true<br \/>\nWaiting for threads to finish.<br \/>\nOne: 5<br \/>\nTwo: 5<br \/>\nThree: 5<br \/>\nOne: 4<br \/>\nTwo: 4<br \/>\nThree: 4<br \/>\nOne: 3<br \/>\nTwo: 3<br \/>\nThree: 3<br \/>\nOne: 2<br \/>\nTwo: 2<br \/>\nThree: 2<br \/>\nOne: 1<br \/>\nTwo: 1<br \/>\nThree: 1<br \/>\nTwo exiting.<br \/>\nThree exiting.<br \/>\nOne exiting.<br \/>\nThread One is alive: false<br \/>\nThread Two is alive: false<br \/>\nThread Three is alive: false<br \/>\nMain thread exiting.<br \/>\n\u5982\u4f60\u6240\u89c1\uff0c\u8c03\u7528join()\u540e\u8fd4\u56de\uff0c\u7ebf\u7a0b\u7ec8\u6b62\u6267\u884c\u3002<\/p>\n<p>\u7ebf\u7a0b\u4f18\u5148\u7ea7<\/p>\n<p>\u7ebf\u7a0b\u4f18\u5148\u7ea7\u88ab\u7ebf\u7a0b\u8c03\u5ea6\u7528\u6765\u5224\u5b9a\u4f55\u65f6\u6bcf\u4e2a\u7ebf\u7a0b\u5141\u8bb8\u8fd0\u884c\u3002\u7406\u8bba\u4e0a\uff0c\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u6bd4\u4f18\u5148\u7ea7\u4f4e\u7684\u7ebf\u7a0b\u83b7\u5f97\u66f4\u591a\u7684CPU\u65f6\u95f4\u3002\u5b9e\u9645\u4e0a\uff0c\u7ebf\u7a0b\u83b7\u5f97\u7684CPU\u65f6\u95f4\u901a\u5e38\u7531\u5305\u62ec\u4f18\u5148\u7ea7\u5728\u5185\u7684\u591a\u4e2a\u56e0\u7d20\u51b3\u5b9a\u3002<\/p>\n<p>\u4e00\u4e2a\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u81ea\u7136\u6bd4\u4f18\u5148\u7ea7\u4f4e\u7684\u7ebf\u7a0b\u4f18\u5148\u3002\u8bbe\u7f6e\u7ebf\u7a0b\u4f18\u5316\u7ea7\u53ea\u662f\u8ba9\u7ebf\u7a0b\u5148\u6267\u884c\u7684\u6982\u7387\u589e\u5927\u5e76\u4e0d\u4fdd\u8bc1\u8be5\u7ebf\u7a0b\u4e00\u5b9a\u5148\u6267\u884c.<\/p>\n<p>\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\uff0c\u7528setPriority()\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4e5f\u662fTread \u7684\u6210\u5458\u3002\u5b83\u7684\u901a\u5e38\u5f62\u5f0f\u4e3a\uff1a<br \/>\nfinal void setPriority(int level)<\/p>\n<p>\u8fd9 \u91cc \uff0c level \u6307\u5b9a\u4e86\u5bf9\u6240\u8c03\u7528\u7684\u7ebf\u7a0b\u7684\u65b0\u7684\u4f18\u5148\u6743\u7684\u8bbe\u7f6e\u3002Level\u7684\u503c\u5fc5\u987b\u5728MIN_PRIORITY\u5230MAX_PRIORITY\u8303\u56f4\u5185\u3002\u901a\u5e38\uff0c\u5b83\u4eec\u7684\u503c\u5206\u522b\u662f1\u548c10\u3002\u8981\u8fd4\u56de\u4e00\u4e2a\u7ebf\u7a0b\u4e3a\u9ed8\u8ba4\u7684\u4f18\u5148\u7ea7\uff0c\u6307\u5b9aNORM_PRIORITY\uff0c\u901a\u5e38\u503c\u4e3a5\u3002\u8fd9\u4e9b\u4f18\u5148\u7ea7\u5728Thread\u4e2d\u90fd\u88ab\u5b9a\u4e49\u4e3afinal\u578b\u53d8\u91cf\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528Thread\u7684getPriority()\u65b9\u6cd5\u6765\u83b7\u5f97\u5f53\u524d\u7684\u4f18\u5148\u7ea7\u8bbe\u7f6e\u3002\u8be5\u65b9\u6cd5\u5982\u4e0b\uff1a<br \/>\nfinal int getPriority( )<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u5e38\u4f60\u5e0c\u671b\u4e3b\u7ebf\u7a0b\u6700\u540e\u7ed3\u675f\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u70b9\u662f\u901a\u8fc7\u5728main()\u4e2d\u8c03\u7528sleep()\u6765\u5b9e\u73b0\u7684\uff0c\u7ecf\u8fc7\u8db3\u591f\u957f\u65f6\u95f4 [&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\/683"}],"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=683"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/683\/revisions"}],"predecessor-version":[{"id":692,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/683\/revisions\/692"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}