{"id":203666,"date":"2025-05-22T11:27:20","date_gmt":"2025-05-22T03:27:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203666\/"},"modified":"2025-05-22T11:27:20","modified_gmt":"2025-05-22T03:27:20","slug":"countdownlatch-%e4%bb%a3%e7%a0%81%e6%ae%b5%e4%b8%ba%e4%bd%95%e6%97%a0%e6%b3%95%e6%8c%89%e9%a1%ba%e5%ba%8f%e6%89%a7%e8%a1%8c-test-%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203666\/","title":{"rendered":"CountDownLatch \u4ee3\u7801\u6bb5\u4e3a\u4f55\u65e0\u6cd5\u6309\u987a\u5e8f\u6267\u884c test() \u65b9\u6cd5\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>CountDownLatch \u4ee3\u7801\u6bb5\u4e3a\u4f55\u65e0\u6cd5\u6309\u987a\u5e8f\u6267\u884c test() \u65b9\u6cd5\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aCountDownLatch \u4ee3\u7801\u6bb5\u4e3a\u4f55\u65e0\u6cd5\u6309\u987a\u5e8f\u6267\u884c test() \u65b9\u6cd5\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/173180576967394249877f3.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5173\u4e8e countdownlatch \u4ee3\u7801\u6bb5\u7684\u7591\u60d1<\/strong><\/p>\n<p>\u4f60\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4ee3\u7801\u6bb5\uff1a<\/p>\n<pre>public class CountDownLatchExample1 {\n\n    private static int threadCount = 10;\n\n    public static void main(String[] args) throws InterruptedException {\n        ExecutorService es = Executors.newCachedThreadPool();\n        CountDownLatch countDownLatch = new CountDownLatch(threadCount);\n        for (int i = 0; i &lt; threadCount; i++) {\n            final int count = i;\n            es.execute(() -&gt; {\n                try {\n                    test(count);\n                } catch (Exception e) {\n                    log.error(\"Exception,{}\", e);\n                } finally {\n                    countDownLatch.countDown();\n                }\n            });\n        }\n        countDownLatch.await(1, TimeUnit.MILLISECONDS);\n        log.info(\"======\");\n        es.shutdown();\n    }\n\n    public static void test(int i) throws InterruptedException {\n        Thread.sleep(1000); \/\/ \u6b64\u65b9\u6cd5\u5ef6\u8fdf1\u79d2\u518d\u6267\u884c\n        log.info(\"test {}\", i);\n    }\n\n}<\/pre>\n<p>\u5176\u4e2d\uff0c\u95ee\u9898\u662f\u4f60\u8ba4\u4e3a\u6b64\u4ee3\u7801\u6bb5\u5c06\u6309\u987a\u5e8f\u6267\u884c test() \u65b9\u6cd5\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u4f1a\u5e76\u884c\u6267\u884c\u3002<\/p>\n<p><strong>\u7b54\u6848:<\/strong><\/p>\n<p>\u4f60\u7684\u4ee3\u7801\u6bb5\u4f7f\u7528\u4e86\u4e00\u4e2a countdownlatch \u6765\u534f\u8c03\u591a\u4e2a\u7ebf\u7a0b\uff0c\u5b83\u5b9e\u9645\u4e0a\u8fd0\u884c\u5728\u5e76\u53d1\u6a21\u5f0f\u4e0b\u3002\u5f53\u6240\u6709\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4e3b\u7ebf\u7a0b\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u4e2d\uff0c\u4f60\u4ece executorservice \u751f\u6210\u7ebf\u7a0b\u5e76\u7acb\u5373\u6267\u884c\u5b83\u4eec\u3002\u7531\u4e8e countdownlatch \u7684\u8ba1\u6570\u6700\u521d\u8bbe\u7f6e\u4e3a 10\uff0c\u56e0\u6b64\u6240\u6709\u7ebf\u7a0b\u90fd\u5141\u8bb8\u8fd0\u884c\u800c\u65e0\u9700\u7b49\u5f85\u3002\u8fd9\u5bfc\u81f4 test() \u65b9\u6cd5\u4ee5\u975e\u786e\u5b9a\u6027\u7684\u987a\u5e8f\u5e76\u884c\u6267\u884c\uff0c\u800c\u4e0d\u662f\u6309\u987a\u5e8f\u6267\u884c\u3002<\/p>\n<p>\u8981\u6309\u987a\u5e8f\u6267\u884c test() \u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 await() \u65b9\u6cd5\u4e0a\u7684\u963b\u585e\u7248\u672c\uff0c\u800c\u4e0d\u662f\u5e26\u8d85\u65f6\u7684 await() \u7248\u672c\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e3b\u7ebf\u7a0b\u963b\u585e\uff0c\u76f4\u5230 countdownlatch \u8ba1\u6570\u964d\u4e3a 0\uff0c\u5373\u6240\u6709\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aCountDownLatch \u4ee3\u7801\u6bb5\u4e3a\u4f55\u65e0\u6cd5\u6309\u987a\u5e8f\u6267\u884c test() \u65b9\u6cd5\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CountDownLatch \u4ee3&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203666","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203666"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203666\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}