{"id":485,"date":"2021-03-22T02:30:40","date_gmt":"2021-03-21T18:30:40","guid":{"rendered":"https:\/\/blog.frost-s.tk\/?p=485"},"modified":"2021-12-16T23:03:13","modified_gmt":"2021-12-16T15:03:13","slug":"countdownlatch","status":"publish","type":"post","link":"https:\/\/blog.frost-s.com\/index.php\/2021\/03\/22\/countdownlatch\/","title":{"rendered":"countDownLatch"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">1.\u80cc\u666f\uff1a<\/h4>\n\n\n\n<ul><li>countDownLatch\u662f\u5728java1.5\u88ab\u5f15\u5165\uff0c\u8ddf\u5b83\u4e00\u8d77\u88ab\u5f15\u5165\u7684\u5de5\u5177\u7c7b\u8fd8\u6709CyclicBarrier\u3001Semaphore\u3001concurrentHashMap\u548cBlockingQueue\u3002<\/li><li>\u5b58\u5728\u4e8ejava.util.cucurrent\u5305\u4e0b\u3002<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2.\u6982\u5ff5<\/h4>\n\n\n\n<ul><li>countDownLatch\u8fd9\u4e2a\u7c7b\u4f7f\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u5404\u81ea\u6267\u884c\u5b8c\u6bd5\u540e\u518d\u6267\u884c\u3002<\/li><li>\u662f\u901a\u8fc7\u4e00\u4e2a\u8ba1\u6570\u5668\u6765\u5b9e\u73b0\u7684\uff0c\u8ba1\u6570\u5668\u7684\u521d\u59cb\u503c\u662f\u7ebf\u7a0b\u7684\u6570\u91cf\u3002\u6bcf\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u8ba1\u6570\u5668\u7684\u503c\u5c31-1\uff0c\u5f53\u8ba1\u6570\u5668\u7684\u503c\u4e3a0\u65f6\uff0c\u8868\u793a\u6240\u6709\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u6bd5\uff0c\u7136\u540e\u5728\u95ed\u9501\u4e0a\u7b49\u5f85\u7684\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u6062\u590d\u5de5\u4f5c\u4e86\u3002<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.\u6e90\u7801<\/h4>\n\n\n\n<ul><li>countDownLatch\u7c7b\u4e2d\u53ea\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6784\u9020\u5668\uff1a<\/li><\/ul>\n\n\n\n<p><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u53c2\u6570count\u4e3a\u8ba1\u6570\u503c\npublic CountDownLatch(int count) {  };  \n<\/code><\/pre>\n\n\n\n<ul><li>\u7c7b\u4e2d\u6709\u4e09\u4e2a\u65b9\u6cd5\u662f\u6700\u91cd\u8981\u7684\uff1a<\/li><\/ul>\n\n\n\n<p><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u8c03\u7528await()\u65b9\u6cd5\u7684\u7ebf\u7a0b\u4f1a\u88ab\u6302\u8d77\uff0c\u5b83\u4f1a\u7b49\u5f85\u76f4\u5230count\u503c\u4e3a0\u624d\u7ee7\u7eed\u6267\u884c\npublic void await() throws InterruptedException { };   \n\/\/\u548cawait()\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u7b49\u5f85\u4e00\u5b9a\u7684\u65f6\u95f4\u540ecount\u503c\u8fd8\u6ca1\u53d8\u4e3a0\u7684\u8bdd\u5c31\u4f1a\u7ee7\u7eed\u6267\u884c\npublic boolean await(long timeout, TimeUnit unit) throws InterruptedException { };  \n\/\/\u5c06count\u503c\u51cf1\npublic void countDown() { };  \n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.\u793a\u4f8b<\/h4>\n\n\n\n<p><em>\u666e\u901a\u793a\u4f8b\uff1a<\/em><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CountDownLatchTest {\n\n    public static void main(String&#91;] args) {\n        final CountDownLatch latch = new CountDownLatch(2);\n        System.out.println(\"\u4e3b\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u2026\u2026 \u2026\u2026\");\n        \/\/\u7b2c\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u6267\u884c\n        ExecutorService es1 = Executors.newSingleThreadExecutor();\n        es1.execute(new Runnable() {\n            @Override\n            public void run() {\n                try {\n                    Thread.sleep(3000);\n                    System.out.println(\"\u5b50\u7ebf\u7a0b\uff1a\"+Thread.currentThread().getName()+\"\u6267\u884c\");\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n                latch.countDown();\n            }\n        });\n        es1.shutdown();\n\n        \/\/\u7b2c\u4e8c\u4e2a\u5b50\u7ebf\u7a0b\u6267\u884c\n        ExecutorService es2 = Executors.newSingleThreadExecutor();\n        es2.execute(new Runnable() {\n            @Override\n            public void run() {\n                try {\n                    Thread.sleep(3000);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n                System.out.println(\"\u5b50\u7ebf\u7a0b\uff1a\"+Thread.currentThread().getName()+\"\u6267\u884c\");\n                latch.countDown();\n            }\n        });\n        es2.shutdown();\n        System.out.println(\"\u7b49\u5f85\u4e24\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u2026\u2026 \u2026\u2026\");\n        try {\n            latch.await();\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        System.out.println(\"\u4e24\u4e2a\u5b50\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u6bd5\uff0c\u7ee7\u7eed\u6267\u884c\u4e3b\u7ebf\u7a0b\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\u96c6\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4e3b\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u2026\u2026 \u2026\u2026\n\u7b49\u5f85\u4e24\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u2026\u2026 \u2026\u2026\n\u5b50\u7ebf\u7a0b\uff1apool-1-thread-1\u6267\u884c\n\u5b50\u7ebf\u7a0b\uff1apool-2-thread-1\u6267\u884c\n\u4e24\u4e2a\u5b50\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u6bd5\uff0c\u7ee7\u7eed\u6267\u884c\u4e3b\u7ebf\u7a0b\n<\/code><\/pre>\n\n\n\n<p><em>\u6a21\u62df\u5e76\u53d1\u793a\u4f8b\uff1a<\/em><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Parallellimit {\n    public static void main(String&#91;] args) {\n        ExecutorService pool = Executors.newCachedThreadPool();\n        CountDownLatch cdl = new CountDownLatch(100);\n        for (int i = 0; i < 100; i++) {\n            CountRunnable runnable = new CountRunnable(cdl);\n            pool.execute(runnable);\n        }\n    }\n}\n\n class CountRunnable implements Runnable {\n    private CountDownLatch countDownLatch;\n    public CountRunnable(CountDownLatch countDownLatch) {\n        this.countDownLatch = countDownLatch;\n    }\n    @Override\n    public void run() {\n        try {\n            synchronized (countDownLatch) {\n                \/*** \u6bcf\u6b21\u51cf\u5c11\u4e00\u4e2a\u5bb9\u91cf*\/\n                countDownLatch.countDown();\n                System.out.println(\"thread counts = \" + (countDownLatch.getCount()));\n            }\n            countDownLatch.await();\n            System.out.println(\"concurrency counts = \" + (100 - countDownLatch.getCount()));\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>*<strong>CountDownLatch\u548cCyclicBarrier\u533a\u522b\uff1a<\/strong><br>1.countDownLatch\u662f\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u7ebf\u7a0b\u5b8c\u6210\u4e00\u4e2a\u8bb0\u5f55\u4e00\u4e2a\uff0c\u8ba1\u6570\u5668\u9012\u51cf\uff0c\u53ea\u80fd\u53ea\u7528\u4e00\u6b21<br>2.CyclicBarrier\u7684\u8ba1\u6570\u5668\u66f4\u50cf\u4e00\u4e2a\u9600\u95e8\uff0c\u9700\u8981\u6240\u6709\u7ebf\u7a0b\u90fd\u5230\u8fbe\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u8ba1\u6570\u5668\u9012\u589e\uff0c\u63d0\u4f9breset\u529f\u80fd\uff0c\u53ef\u4ee5\u591a\u6b21\u4f7f\u7528<\/p><p><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>1.\u80cc\u666f\uff1a countDownLatch\u662f\u5728java1.5\u88ab\u5f15\u5165\uff0c\u8ddf\u5b83\u4e00\u8d77\u88ab\u5f15\u5165\u7684\u5de5\u5177\u7c7b\u8fd8\u6709CyclicBar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":252,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/485"}],"collection":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/comments?post=485"}],"version-history":[{"count":7,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/485\/revisions"}],"predecessor-version":[{"id":838,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/485\/revisions\/838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media\/252"}],"wp:attachment":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media?parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/categories?post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/tags?post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}