{"id":10006054,"date":"2022-07-29T12:44:33","date_gmt":"2022-07-29T04:44:33","guid":{"rendered":"https:\/\/ityet.com\/?p=10006054"},"modified":"2022-07-29T12:44:33","modified_gmt":"2022-07-29T04:44:33","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e9%a1%ba%e5%ba%8f%e8%bf%90%e8%a1%8c%e7%9a%84-4-%e7%a7%8d%e6%96%b9%e6%b3%95%ef%bc%8c%e9%9d%a2%e8%af%95%e9%9a%8f%e4%be%bf%e9%97%ae%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006054","title":{"rendered":"\u591a\u7ebf\u7a0b\u987a\u5e8f\u8fd0\u884c\u7684 4 \u79cd\u65b9\u6cd5\uff0c\u9762\u8bd5\u968f\u4fbf\u95ee\uff01"},"content":{"rendered":"<p>\u6587\u7ae0\u4ecb\u7ecd4\u79cd\u65b9\u6cd5\uff0c\u7b80\u5355\u6613\u61c2\uff0c\u901a\u8fc74\u4e2ademo\u629b\u7816\u5f15\u7389\u3002<\/p>\n<h3>1\u3001\u5728\u5b50\u7ebf\u7a0b\u4e2d\u901a\u8fc7join()\u65b9\u6cd5\u6307\u5b9a\u987a\u5e8f<\/h3>\n<p>\u901a\u8fc7<code>join()<\/code>\u65b9\u6cd5\u4f7f\u5f53\u524d\u7ebf\u7a0b\u201c\u963b\u585e\u201d\uff0c\u7b49\u5f85\u6307\u5b9a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<blockquote><p>\u4e3e\u4f8b\uff1a\u5728\u7ebf\u7a0bthread2\u4e2d\uff0c\u52a0\u4e0a\u4e00\u53e5thread1.join()\uff0c\u5176\u610f\u4e49\u5728\u4e8e\uff0c\u5f53\u524d\u7ebf\u7a0b2\u8fd0\u884c\u5230\u6b64\u884c\u4ee3\u7801\u65f6\u4f1a\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u76f4\u5230\u7ebf\u7a0bthread1\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7ebf\u7a0bthread2\u624d\u4f1a\u7ee7\u7eed\u8fd0\u884c\uff0c\u8fd9\u5c31\u4fdd\u8bc1\u4e86\u7ebf\u7a0bthread1\u4e0e\u7ebf\u7a0bthread2\u7684\u8fd0\u884c\u987a\u5e8f\u3002<\/p><\/blockquote>\n<pre><code>public class ThreadJoinDemo {\n    public static void main(String[] args) throws InterruptedException {\n        final Thread thread1 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u6253\u5f00\u51b0\u7bb1\uff01&quot;);\n            }\n        });\n \n        final Thread thread2 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                try {\n                    thread1.join();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n                System.out.println(&quot;\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01&quot;);\n            }\n        });\n \n        final Thread thread3 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                try {\n                    thread2.join();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n                System.out.println(&quot;\u5173\u4e0a\u51b0\u7bb1\uff01&quot;);\n            }\n        });\n \n        \/\/\u4e0b\u9762\u4e09\u884c\u4ee3\u7801\u987a\u5e8f\u53ef\u968f\u610f\u8c03\u6574\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e0d\u53d7\u5f71\u54cd\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u5b50\u7ebf\u7a0b\u4e2d\u901a\u8fc7\u201cjoin()\u65b9\u6cd5\u201d\u5df2\u7ecf\u6307\u5b9a\u4e86\u8fd0\u884c\u987a\u5e8f\u3002\n        thread3.start();\n        thread2.start();\n        thread1.start();\n \n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u6253\u5f00\u51b0\u7bb1\uff01\n\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01\n\u5173\u4e0a\u51b0\u7bb1\uff01<\/code><\/pre>\n<h3>2\u3001\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u901a\u8fc7join()\u65b9\u6cd5\u6307\u5b9a\u987a\u5e8f<\/h3>\n<p>\u7b80\u5355\u8bf4\u4e00\u4e0b\u5b50\u7ebf\u7a0b\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u533a\u522b\uff0c\u5b50\u7ebf\u7a0b\u6307\u7684\u662f\u53d1\u751f\u5728Thread\u5185\u90e8\u7684\u4ee3\u7801\uff0c\u4e3b\u7ebf\u7a0b\u6307\u7684\u662f\u53d1\u751f\u5728main\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5728main\u51fd\u6570\u4e2d\u901a\u8fc7<code>join()<\/code>\u65b9\u6cd5\u8ba9\u4e3b\u7ebf\u7a0b\u963b\u585e\u7b49\u5f85\u4ee5\u8fbe\u5230\u6307\u5b9a\u987a\u5e8f\u6267\u884c\u7684\u76ee\u7684\u3002<\/p>\n<pre><code>public class ThreadMainJoinDemo {\n    public static void main(String[] args) throws InterruptedException {\n        final Thread thread1 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u6253\u5f00\u51b0\u7bb1\uff01&quot;);\n            }\n        });\n \n        final Thread thread2 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01&quot;);\n            }\n        });\n \n        final Thread thread3 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u5173\u4e0a\u51b0\u7bb1\uff01&quot;);\n            }\n        });\n \n        thread1.start();\n        thread1.join();\n        thread2.start();\n        thread2.join();\n        thread3.start();\n    }\n}<\/code><\/pre>\n<p>\u57fa\u7840\u5c31\u4e0d\u4ecb\u7ecd\u4e86\uff0c\u591a\u7ebf\u7a0b\u7cfb\u5217\u6211\u535a\u5ba2\u6559\u7a0b\u5199\u4e86\u5f88\u591a\u4e86\uff0c\u8fd9\u91cc\u63a8\u8350\u5927\u5bb6\u770b\u770b\u5427\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=voyPnB%2FypJ8TDyRyTQ3xEQ%3D%3D.werRlVthQyXxX7BkbazKUkFOtfA3aUAiUYP4LRIw79iooQqIgphWcVJ1jody4Sj8\" rel=\"nofollow\">https:\/\/www.javastack.cn\/cate&#8230;<\/a><\/p>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u6253\u5f00\u51b0\u7bb1\uff01\n\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01\n\u5173\u4e0a\u51b0\u7bb1\uff01<\/code><\/pre>\n<h3>3\u3001\u901a\u8fc7\u5012\u6570\u8ba1\u65f6\u5668CountDownLatch\u5b9e\u73b0<\/h3>\n<p>CountDownLatch\u901a\u8fc7\u8ba1\u6570\u5668\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u63a7\u5236\uff0c\u53ea\u8981\u68c0\u6d4b\u5230\u8ba1\u6570\u5668\u4e3a0\u5f53\u524d\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u5f80\u4e0b\u6267\u884c\u800c\u4e0d\u7528\u7ba1\u76f8\u5e94\u7684thread\u662f\u5426\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<pre><code>public class ThreadCountDownLatchDemo {\n \n    private static CountDownLatch countDownLatch1 = new CountDownLatch(1);\n \n    private static CountDownLatch countDownLatch2 = new CountDownLatch(1);\n \n    public static void main(String[] args) {\n        final Thread thread1 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u6253\u5f00\u51b0\u7bb1\uff01&quot;);\n                countDownLatch1.countDown();\n            }\n        });\n \n        final Thread thread2 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                try {\n                    countDownLatch1.await();\n                    System.out.println(&quot;\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01&quot;);\n                    countDownLatch2.countDown();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n        });\n \n        final Thread thread3 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                try {\n                    countDownLatch2.await();\n                    System.out.println(&quot;\u5173\u4e0a\u51b0\u7bb1\uff01&quot;);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n        });\n \n        \/\/\u4e0b\u9762\u4e09\u884c\u4ee3\u7801\u987a\u5e8f\u53ef\u968f\u610f\u8c03\u6574\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u4e0d\u53d7\u5f71\u54cd\n        thread3.start();\n        thread1.start();\n        thread2.start();\n    }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u6253\u5f00\u51b0\u7bb1\uff01\n\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01\n\u5173\u4e0a\u51b0\u7bb1\uff01<\/code><\/pre>\n<h3>4\u3001\u901a\u8fc7\u521b\u5efa\u5355\u4e00\u5316\u7ebf\u7a0b\u6c60<code>newSingleThreadExecutor()<\/code>\u5b9e\u73b0<\/h3>\n<p>\u5355\u7ebf\u7a0b\u5316\u7ebf\u7a0b\u6c60(newSingleThreadExecutor)\u7684\u4f18\u70b9\uff0c\u4e32\u884c\u6267\u884c\u6240\u6709\u4efb\u52a1\u3002<\/p>\n<pre><code>public class ThreadPoolDemo {\n \n   static ExecutorService executorService = Executors.newSingleThreadExecutor();\n \n    public static void main(String[] args) {\n        final Thread thread1 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u6253\u5f00\u51b0\u7bb1\uff01&quot;);\n            }\n        });\n \n        final Thread thread2 =new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01&quot;);\n            }\n        });\n \n        final Thread thread3 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(&quot;\u5173\u4e0a\u51b0\u7bb1\uff01&quot;);\n            }\n        });\n        executorService.submit(thread1);\n        executorService.submit(thread2);\n        executorService.submit(thread3);\n        executorService.shutdown();        \/\/\u4f7f\u7528\u5b8c\u6bd5\u8bb0\u5f97\u5173\u95ed\u7ebf\u7a0b\u6c60\n    }\n \n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u6253\u5f00\u51b0\u7bb1\uff01\n\u62ff\u51fa\u4e00\u74f6\u725b\u5976\uff01\n\u5173\u4e0a\u51b0\u7bb1\uff01<\/code><\/pre>\n<p>\u6765\u6e90\uff1ablog.csdn.net\/jqc874789596\/article\/details\/100557300<\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=dNoxSdUSJncVokGZFiJJsw%3D%3D.fjfXaTynI%2BxulNLvgdMPnSaKTrbC6eegFiZocEw6rCc%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=APkLCYAYNWi2OzIiSlfaUg%3D%3D.KJMRZ2WhscnToOYw%2BpGC2gCWGggnKOQKrT1atHb8U0GjPhdjgkK6%2FVA4bwQfus4RiPHDnogo1ikIz%2FF7h1LjNgJ45cSfHYU4ROG3ubbihs0%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=MGb3t5WHlMmLW8jphjLl4Q%3D%3D.4vYyAzsgxiSzuW9zqHZOUtYflGwlwJZew0Qdy%2BZ4aEGX1YmK8IsNbFc%2BwniZkyJN\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=v%2BHL4SBlKJgy9j4gicy5mQ%3D%3D.VVy59XAJ8IZOjQEXmVNKBnrcAamH313QEN2xnZ1Y1FrE2V487nCcpNS24uEjYV4mbLaSctRl6idsvdB9sSFKmILjvHDnnJo%2BUJrPOTvoeOE%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=YKZ7xbcBjYCumRcPABlPow%3D%3D.1K0bDbxeqzGCA3ttkWzRMzAct5CR%2Bjs7xR4kWCQBXMw%2B7eUjRxEVS0aSzfqKkz3OuHeibEZ57YGUPxgC3gT1vn42f4Q3g%2B8eNnjEheEjQbChSuBReD7ck9hU0ClMa%2Bzi\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042238590\">https:\/\/segmentfault.com\/a\/1190000042238590<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u4ecb\u7ecd4\u79cd\u65b9\u6cd5\uff0c\u7b80\u5355\u6613\u61c2\uff0c\u901a\u8fc74\u4e2ademo\u629b\u7816\u5f15\u7389\u3002 1\u3001\u5728\u5b50\u7ebf\u7a0b\u4e2d\u901a\u8fc7join()\u65b9\u6cd5\u6307\u5b9a\u987a\u5e8f \u901a\u8fc7joi [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006054","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006054"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006054\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}