{"id":10008132,"date":"2023-08-22T12:59:56","date_gmt":"2023-08-22T04:59:56","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008132"},"modified":"2023-08-22T12:59:56","modified_gmt":"2023-08-22T04:59:56","slug":"%e5%90%8c%e4%ba%8b%e5%86%99%e4%ba%86%e4%b8%aa%e6%83%8a%e5%a4%a9-bug%ef%bc%8c%e8%bf%98%e4%b8%8d%e5%ae%b9%e6%98%93%e8%a2%ab%e5%8f%91%e7%8e%b0%e3%80%82%e3%80%82","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008132","title":{"rendered":"\u540c\u4e8b\u5199\u4e86\u4e2a\u60ca\u5929 bug\uff0c\u8fd8\u4e0d\u5bb9\u6613\u88ab\u53d1\u73b0\u3002\u3002"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u6811\u6d1e\u541b \/<br \/>\u94fe\u63a5\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=EK%2Bc4kYJlwPdLQkcP4v4Qw%3D%3D.roP%2BqeyWKqHS4yUO9pi52Y3jw4Q6LsqnQGK1Qe9xK7PO%2Fnoe6nOTZEBWtsBzSyaR\" rel=\"noopener noreferrer\">https:\/\/juejin.cn\/post\/7064376361334358046<\/a><\/p>\n<h2>\u4e8b\u6545\u63cf\u8ff0<\/h2>\n<p>\u4ece6\u70b932\u5206\u5f00\u59cb\u5c11\u91cf\u7528\u6237\u8bbf\u95eeapp\u65f6\u4f1a\u51fa\u73b0\u9996\u9875\u8bbf\u95ee\u5f02\u5e38\uff0c\u52307\u70b920\u5206\u9996\u9875\u670d\u52a1\u5927\u89c4\u6a21\u4e0d\u53ef\u7528\uff0c7\u70b936\u5206\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n<h2>\u6574\u4f53\u7ecf\u8fc7<\/h2>\n<p>6:58 \u53d1\u73b0\u62a5\u8b66\uff0c\u540c\u65f6\u53d1\u73b0\u7fa4\u91cc\u53cd\u9988\u9996\u9875\u51fa\u73b0\u7f51\u7edc\u7e41\u5fd9\uff0c\u8003\u8651\u5230<strong>\u524d\u51e0\u65e5<\/strong>\u665a\u4e0a\u95e8\u5e97\u5217\u8868\u670d\u52a1\u4e0a\u7ebf\u53d1\u5e03\u8fc7\uff0c\u6240\u4ee5\u8003\u8651\u56de\u6eda\u4ee3\u7801\u7d27\u6025\u5904\u7406\u95ee\u9898\u3002<\/p>\n<p>7:07 \u5f00\u59cb\u5148\u540e\u8054\u7cfbXXX\u67e5\u770b\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p>7:36  \u4ee3\u7801\u56de\u6eda\u5b8c\uff0c\u670d\u52a1\u6062\u590d\u6b63\u5e38\u3002<\/p>\n<h2>\u4e8b\u6545\u6839\u672c\u539f\u56e0-\u4e8b\u6545\u4ee3\u7801\u6a21\u62df<\/h2>\n<pre><code class=\"java\">public static void test() throws InterruptedException, ExecutionException {\n    Executor executor = Executors.newFixedThreadPool(3);\n    CompletionService&lt;String&gt; service = new ExecutorCompletionService&lt;&gt;(executor);\n        service.submit(new Callable&lt;String&gt;() {\n            @Override\n            public String call() throws Exception {\n                return &quot;HelloWorld--&quot; + Thread.currentThread().getName();\n            }\n        });\n}<\/code><\/pre>\n<p><strong>\u6839\u6e90\u5c31\u5728\u4e8eExecutorCompletionService\u7ed3\u679c\u6ca1 \u8c03\u7528take\uff0cpoll\u65b9\u6cd5\u3002<\/strong><\/p>\n<p><strong>\u6b63\u786e\u7684\u5199\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/strong><\/p>\n<pre><code class=\"java\">public static void test() throws InterruptedException, ExecutionException {\n    Executor executor = Executors.newFixedThreadPool(3);\n    CompletionService&lt;String&gt; service = new ExecutorCompletionService&lt;&gt;(executor);\n    service.submit(new Callable&lt;String&gt;() {\n        @Override\n        public String call() throws Exception {\n            return &quot;HelloWorld--&quot; + Thread.currentThread().getName();\n        }\n    });\n    service.take().get();\n}<\/code><\/pre>\n<p>\u4e00\u884c\u4ee3\u7801\u5f15\u53d1\u7684\u8840\u6848\uff0c\u800c\u4e14\u4e0d\u5bb9\u6613\u88ab\u53d1\u73b0\uff0c\u56e0\u4e3aoom\u662f\u4e00\u4e2a\u5185\u5b58\u7f13\u6162\u589e\u957f\u7684\u8fc7\u7a0b\uff0c\u7a0d\u5fae\u7c97\u5fc3\u5927\u610f\u5c31\u4f1a\u5ffd\u7565\uff0c\u5982\u679c\u662f\u8fd9\u4e2a\u4ee3\u7801\u5757\u7684\u8c03\u7528\u91cf\u5c11\u7684\u8bdd\uff0c\u5f88\u53ef\u80fd\u51e0\u5929\u751a\u81f3\u51e0\u4e2a\u6708\u540e\u66b4\u96f7\u3002<\/p>\n<p>\u64cd\u4f5c\u4eba\u56de\u6edaor\u91cd\u542f\u670d\u52a1\u5668\u786e\u5b9e\u662f\u6700\u5feb\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u5982\u679c\u4e0d\u662f\u4e8b\u540e\u5feb\u901f\u5206\u6790\u51faoom\u7684\u4ee3\u7801\uff0c\u800c\u4e14\u4e0d\u5de7\u56de\u6eda\u7684\u7248\u672c\u4e5f\u662f\u5e26oom\u4ee3\u7801\u7684\uff0c\u5c31\u6bd4\u8f83\u60b2\u50ac\u4e86\uff0c\u5982\u521a\u624d\u6240\u8bf4\uff0c\u6d41\u91cf\u5c0f\u4e86\uff0c\u56de\u6eda\u6216\u8005\u91cd\u542f\u90fd\u53ef\u4ee5\u91ca\u653e\u5185\u5b58\uff1b\u4f46\u662f\u6d41\u91cf\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u9664\u975e\u56de\u6eda\u5230\u6b63\u5e38\u7684\u7248\u672c\uff0c\u5426\u5219GG\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=rMMorzCA2CsIEPeTs4vuRQ%3D%3D.Rw0xqs1QFa2CnjwnUAkF0InrgDCfFpEacGxNJQjjvHpVtT4BRd%2BbsXZOe3QGM0IhmosS06ko9HOXTujtdhrXOg%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p>\n<h2>\u63a2\u8be2\u95ee\u9898\u7684\u6839\u6e90<\/h2>\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u7406\u89e3ExecutorCompletionService\u7684 \u201c\u5957\u8def\u201d \u6211\u4eec\u7528 ExecutorService\u6765\u4f5c\u4e3a\u5bf9\u6bd4\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u597d\u7684\u6e05\u695a\uff0c\u4ec0\u4e48\u573a\u666f\u4e0b\u7528ExecutorCompletionService\u3002<\/p>\n<p><strong>\u5148\u770bExecutorService\u4ee3\u7801\uff08\u5efa\u8baedown\u4e0b\u6765\u8dd1\u4e00\u8dd1\uff09<\/strong><\/p>\n<pre><code class=\"ini\">public static void test1() throws Exception{\n    ExecutorService executorService = Executors.newCachedThreadPool();\n    ArrayList&lt;Future&lt;String&gt;&gt; futureArrayList = new ArrayList&lt;&gt;();\n    System.out.println(&quot;\u516c\u53f8\u8ba9\u4f60\u901a\u77e5\u5927\u5bb6\u805a\u9910 \u4f60\u5f00\u8f66\u53bb\u63a5\u4eba&quot;);\n    Future&lt;String&gt; future10 = executorService.submit(() -&gt; {\n        System.out.println(&quot;\u603b\u88c1\uff1a\u6211\u5728\u5bb6\u4e0a\u5927\u53f7 \u6211\u6700\u8fd1\u62c9\u809a\u5b50\u6bd4\u8f83\u6162 \u8981\u8e721\u4e2a\u5c0f\u65f6\u624d\u80fd\u51fa\u6765 \u4f60\u7b49\u4f1a\u6765\u63a5\u6211\u5427&quot;);\n        TimeUnit.SECONDS.sleep(10);\n        System.out.println(&quot;\u603b\u88c1\uff1a1\u5c0f\u65f6\u4e86 \u6211\u4e0a\u5b8c\u5927\u53f7\u4e86\u3002\u4f60\u6765\u63a5\u5427&quot;);\n        return &quot;\u603b\u88c1\u4e0a\u5b8c\u5927\u53f7\u4e86&quot;;\n\n    });\n    futureArrayList.add(future10);\n    Future&lt;String&gt; future3 = executorService.submit(() -&gt; {\n        System.out.println(&quot;\u7814\u53d1\uff1a\u6211\u5728\u5bb6\u4e0a\u5927\u53f7 \u6211\u6bd4\u8f83\u5feb \u8981\u8e723\u5206\u949f\u5c31\u53ef\u4ee5\u51fa\u6765 \u4f60\u7b49\u4f1a\u6765\u63a5\u6211\u5427&quot;);\n        TimeUnit.SECONDS.sleep(3);\n        System.out.println(&quot;\u7814\u53d1\uff1a3\u5206\u949f \u6211\u4e0a\u5b8c\u5927\u53f7\u4e86\u3002\u4f60\u6765\u63a5\u5427&quot;);\n        return &quot;\u7814\u53d1\u4e0a\u5b8c\u5927\u53f7\u4e86&quot;;\n    });\n    futureArrayList.add(future3);\n    Future&lt;String&gt; future6 = executorService.submit(() -&gt; {\n        System.out.println(&quot;\u4e2d\u5c42\u7ba1\u7406\uff1a\u6211\u5728\u5bb6\u4e0a\u5927\u53f7  \u8981\u8e7210\u5206\u949f\u5c31\u53ef\u4ee5\u51fa\u6765 \u4f60\u7b49\u4f1a\u6765\u63a5\u6211\u5427&quot;);\n        TimeUnit.SECONDS.sleep(6);\n        System.out.println(&quot;\u4e2d\u5c42\u7ba1\u7406\uff1a10\u5206\u949f \u6211\u4e0a\u5b8c\u5927\u53f7\u4e86\u3002\u4f60\u6765\u63a5\u5427&quot;);\n        return &quot;\u4e2d\u5c42\u7ba1\u7406\u4e0a\u5b8c\u5927\u53f7\u4e86&quot;;\n    });\n    futureArrayList.add(future6);\n    TimeUnit.SECONDS.sleep(1);\n    System.out.println(&quot;\u90fd\u901a\u77e5\u5b8c\u4e86,\u7b49\u7740\u63a5\u5427\u3002&quot;);\n    try {\n        for (Future&lt;String&gt; future : futureArrayList) {\n            String returnStr = future.get();\n            System.out.println(returnStr + &quot;\uff0c\u4f60\u53bb\u63a5\u4ed6&quot;);\n        }\n        Thread.currentThread().join();\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}<\/code><\/pre>\n<p>\u4e09\u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u5206\u522b\u662f 10s\u30013s\u30016s \u3002\u901a\u8fc7JDK\u7ebf\u7a0b\u6c60\u7684 submit \u63d0\u4ea4\u8fd9\u4e09\u4e2a Callable\u7c7b\u578b\u7684\u4efb\u52a1\u3002<\/p>\n<ul>\n<li>step1 \u4e3b\u7ebf\u7a0b\u628a\u4e09\u4e2a\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u91cc\u9762\u53bb\uff0c\u628a\u5bf9\u5e94\u8fd4\u56de\u7684 Future \u653e\u5230 List \u91cc\u9762\u5b58\u8d77\u6765\uff0c\u7136\u540e\u6267\u884c\u201c\u90fd\u901a\u77e5\u5b8c\u4e86,\u7b49\u7740\u63a5\u5427\u3002\u201d\u8fd9\u884c\u8f93\u51fa\u8bed\u53e5\u3002<\/li>\n<li>step2\u5728\u5faa\u73af\u91cc\u9762\u6267\u884c future.get() \u64cd\u4f5c\uff0c\u963b\u585e\u7b49\u5f85\u3002 \u6700\u540e\u7ed3\u679c\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"546\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l1W\" alt=\"\" title=\"\"><\/p>\n<p>\u5148\u901a\u77e5\u5230\u603b\u88c1\uff0c\u4e5f\u662f\u5148\u63a5\u603b\u88c1 \u8db3\u8db3\u7b49\u4e861\u4e2a\u5c0f\u65f6\uff0c\u63a5\u5230\u603b\u88c1\u540e\u518d\u53bb\u63a5\u7814\u53d1\u548c\u4e2d\u5c42\u7ba1\u7406\uff0c\u5c3d\u7ba1\u4ed6\u4eec\u65e9\u5c31\u5b8c\u4e8b\u513f\u4e86\uff0c\u4e5f\u5f97\u7b49\u603b\u88c1\u4e0a\u5b8c\u5395\u6240~~<\/p>\n<p>\u8017\u65f6\u6700\u4e45\u7684-10s\u5f02\u6b65\u4efb\u52a1\u6700\u5148\u8fdb\u5165list\u6267\u884c\uff0c\u6240\u4ee5\u5728\u5faa\u73af\u8fc7\u7a0b\u4e2d\u83b7\u53d6\u8fd9\u4e2a10s\u7684\u4efb\u52a1\u7ed3\u679c\u7684\u65f6\u5019\uff0cget\u64cd\u4f5c\u4f1a\u4e00\u76f4\u963b\u585e\uff0c\u76f4\u523010s\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u3002\u5373\u4f7f 3s\u30015s\u7684\u4efb\u52a1\u65e9\u5c31\u6267\u884c\u5b8c\u4e86\uff0c\u4e5f\u5f97\u963b\u585e\u7b49\u5f8510s\u4efb\u52a1\u6267\u884c\u5b8c\u3002<\/p>\n<p>\u770b\u5230\u8fd9\u91cc \u5c24\u5176\u662f\u505a\u7f51\u5173\u4e1a\u52a1\u7684\u540c\u5b66\u53ef\u80fd\u4f1a\u4ea7\u751f\u5171\u9e23\uff0c\u4e00\u822c\u6765\u8bf4\u7f51\u5173RPC\u4f1a\u8c03\u7528\u4e0b\u6e38N\u591a\u4e2a\u63a5\u53e3\uff0c\u5982\u4e0b\u56fe<\/p>\n<p>\u5982\u679c\u90fd\u6309\u7167ExecutorService\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5e76\u4e14\u6070\u5de7\u524d\u51e0\u4e2a\u4efb\u52a1\u8c03\u7528\u7684\u63a5\u53e3\u8017\u65f6\u6bd4\u8f83\u4e45\uff0c\u540c\u65f6\u963b\u585e\u7b49\u5f85\uff0c\u90a3\u5c31\u6bd4\u8f83\u60b2\u50ac\u4e86\u3002\u6240\u4ee5ExecutorCompletionService\u5e94\u666f\u800c\u51fa\u3002\u5b83\u4f5c\u4e3a\u4efb\u52a1\u7ebf\u7a0b\u7684\u5408\u7406\u7ba1\u63a7\u8005\uff0c\u201c\u4efb\u52a1\u89c4\u5212\u5e08\u201d\u7684\u79f0\u53f7\u540d\u526f\u5176\u5b9e\u3002<\/p>\n<p><strong>\u76f8\u540c\u573a\u666f ExecutorCompletionService\u4ee3\u7801<\/strong><\/p>\n<pre><code class=\"csharp\">public static void test2() throws Exception {\n    ExecutorService executorService = Executors.newCachedThreadPool();\n    ExecutorCompletionService&lt;String&gt; completionService = new ExecutorCompletionService&lt;&gt;(executorService);\n    System.out.println(&quot;\u516c\u53f8\u8ba9\u4f60\u901a\u77e5\u5927\u5bb6\u805a\u9910 \u4f60\u5f00\u8f66\u53bb\u63a5\u4eba&quot;);\n    completionService.submit(() -&gt; {\n        System.out.println(&quot;\u603b\u88c1\uff1a\u6211\u5728\u5bb6\u4e0a\u5927\u53f7 \u6211\u6700\u8fd1\u62c9\u809a\u5b50\u6bd4\u8f83\u6162 \u8981\u8e721\u4e2a\u5c0f\u65f6\u624d\u80fd\u51fa\u6765 \u4f60\u7b49\u4f1a\u6765\u63a5\u6211\u5427&quot;);\n        TimeUnit.SECONDS.sleep(10);\n        System.out.println(&quot;\u603b\u88c1\uff1a1\u5c0f\u65f6\u4e86 \u6211\u4e0a\u5b8c\u5927\u53f7\u4e86\u3002\u4f60\u6765\u63a5\u5427&quot;);\n        return &quot;\u603b\u88c1\u4e0a\u5b8c\u5927\u53f7\u4e86&quot;;\n    });\n    completionService.submit(() -&gt; {\n        System.out.println(&quot;\u7814\u53d1\uff1a\u6211\u5728\u5bb6\u4e0a\u5927\u53f7 \u6211\u6bd4\u8f83\u5feb \u8981\u8e723\u5206\u949f\u5c31\u53ef\u4ee5\u51fa\u6765 \u4f60\u7b49\u4f1a\u6765\u63a5\u6211\u5427&quot;);\n        TimeUnit.SECONDS.sleep(3);\n        System.out.println(&quot;\u7814\u53d1\uff1a3\u5206\u949f \u6211\u4e0a\u5b8c\u5927\u53f7\u4e86\u3002\u4f60\u6765\u63a5\u5427&quot;);\n        return &quot;\u7814\u53d1\u4e0a\u5b8c\u5927\u53f7\u4e86&quot;;\n    });\n    completionService.submit(() -&gt; {\n        System.out.println(&quot;\u4e2d\u5c42\u7ba1\u7406\uff1a\u6211\u5728\u5bb6\u4e0a\u5927\u53f7  \u8981\u8e7210\u5206\u949f\u5c31\u53ef\u4ee5\u51fa\u6765 \u4f60\u7b49\u4f1a\u6765\u63a5\u6211\u5427&quot;);\n        TimeUnit.SECONDS.sleep(6);\n        System.out.println(&quot;\u4e2d\u5c42\u7ba1\u7406\uff1a10\u5206\u949f \u6211\u4e0a\u5b8c\u5927\u53f7\u4e86\u3002\u4f60\u6765\u63a5\u5427&quot;);\n        return &quot;\u4e2d\u5c42\u7ba1\u7406\u4e0a\u5b8c\u5927\u53f7\u4e86&quot;;\n    });\n    TimeUnit.SECONDS.sleep(1);\n    System.out.println(&quot;\u90fd\u901a\u77e5\u5b8c\u4e86,\u7b49\u7740\u63a5\u5427\u3002&quot;);\n    \/\/\u63d0\u4ea4\u4e863\u4e2a\u5f02\u6b65\u4efb\u52a1\uff09\n    for (int i = 0; i &lt; 3; i++) {\n        String returnStr = completionService.take().get();\n        System.out.println(returnStr + &quot;\uff0c\u4f60\u53bb\u63a5\u4ed6&quot;);\n    }\n    Thread.currentThread().join();\n}<\/code><\/pre>\n<p>\u8dd1\u5b8c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"513\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l1X\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u6b21\u5c31\u76f8\u5bf9\u9ad8\u6548\u4e86\u4e00\u4e9b\uff0c\u867d\u7136\u5148\u901a\u77e5\u7684\u603b\u88c1\uff0c\u4f46\u662f\u6839\u636e\u5927\u5bb6\u4e0a\u5927\u53f7\u7684\u901f\u5ea6\uff0c\u8c01\u5148\u62c9\u5b8c\u5148\u53bb\u63a5\u8c01\uff0c\u4e0d\u7528\u7b49\u5f85\u4e0a\u5927\u53f7\u6700\u4e45\u7684\u603b\u88c1\u4e86\uff08\u73b0\u5b9e\u751f\u6d3b\u91cc \u5efa\u8bae\u91c7\u7528\u7b2c\u4e00\u79cd \u4e0d\u7b49\u603b\u88c1\u7684\u540e\u679c emmm \u54c8\u54c8\u54c8\uff09\u3002<\/p>\n<p>\u653e\u5728\u4e00\u8d77\u5bf9\u6bd4\u4e0b\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"263\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l1Y\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e24\u6bb5\u4ee3\u7801\u7684\u5dee\u5f02\u975e\u5e38\u5c0f \u83b7\u53d6\u7ed3\u679c\u7684\u65f6\u5019ExecutorCompletionService \u4f7f\u7528\u4e86<\/p>\n<pre><code class=\"scss\">completionService.take().get();<\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u8981\u7528take() \u7136\u540e\u518dget()\u5462\uff1f\uff1f\uff1f\uff1f\u6211\u4eec\u770b\u770b\u6e90\u7801<\/p>\n<p>CompletionService\u63a5\u53e3 \u4ee5\u53ca\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b<\/p>\n<p>1\u3001ExecutorCompletionService\u662fCompletionService\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b <img decoding=\"async\" width=\"723\" height=\"408\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l1Z\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>2\u3001\u63a5\u7740\u8ddf\u4e00\u4e0bExecutorCompletionService\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u53ef\u4ee5\u770b\u5230\u5165\u53c2\u9700\u8981\u4f20\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u4f7f\u7528\u7684\u961f\u5217\u662f LinkedBlockingQueue\uff0c\u4e0d\u8fc7\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u6784\u9020\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u961f\u5217\u7c7b\u578b\uff0c\u5982\u4e0b\u4e24\u5f20\u56fe\uff0c\u4e24\u4e2a\u6784\u9020\u65b9\u6cd5\u3002<\/p>\n<p>\u9ed8\u8ba4LinkedBlockingQueue\u7684\u6784\u9020\u65b9\u6cd5 <img decoding=\"async\" width=\"723\" height=\"357\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l10\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u53ef\u9009\u961f\u5217\u7c7b\u578b\u7684\u6784\u9020\u65b9\u6cd5 <img decoding=\"async\" width=\"723\" height=\"275\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l11\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>3\u3001submit\u4efb\u52a1\u63d0\u4ea4\u7684\u4e24\u79cd\u65b9\u5f0f\uff0c\u90fd\u662f\u6709\u8fd4\u56de\u503c\u7684\uff0c\u6211\u4eec\u4f8b\u5b50\u4e2d\u7528\u5230\u7684\u5c31\u662f\u7b2c\u4e00\u79cdCallable\u7c7b\u578b\u7684\u65b9\u6cd5\u3002 <img decoding=\"async\" width=\"723\" height=\"401\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l12\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>4\u3001\u5bf9\u6bd4ExecutorService \u548c ExecutorCompletionService submit\u65b9\u6cd5 \u53ef\u4ee5\u770b\u51fa\u533a\u522b \uff081\uff09ExecutorService<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"388\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l13\" alt=\"wecom-temp-7a3620b4ca55c25badcbc5f96bfeb75f.png\" title=\"wecom-temp-7a3620b4ca55c25badcbc5f96bfeb75f.png\" loading=\"lazy\"><\/p>\n<p>\uff082\uff09ExecutorCompletionService<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"261\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l14\" alt=\"wecom-temp-8c8a582217d0ae65f7e3aff43ce71de2.png\" title=\"wecom-temp-8c8a582217d0ae65f7e3aff43ce71de2.png\" loading=\"lazy\"><\/p>\n<p>5\u3001\u5dee\u5f02\u5c31\u5728 QueueingFuture\uff0c\u8fd9\u4e2a\u5230\u5e95\u4f5c\u7528\u662f\u5565\uff0c\u6211\u4eec\u7ee7\u7eed\u8ddf\u8fdb\u53bb\u770b<\/p>\n<ul>\n<li>QueueingFuture \u7ee7\u627f\u81ea FutureTask\uff0c\u800c\u4e14\u7ea2\u7ebf\u90e8\u5206\u6807\u6ce8\u7684\u4f4d\u7f6e\uff0c\u91cd\u5199\u4e86done()\u65b9\u6cd5\u3002<\/li>\n<li>\u628a task \u653e\u5230 completionQueue \u961f\u5217\u91cc\u9762\uff0c\u5f53\u4efb\u52a1\u6267\u884c\u5b8c\u6210\u540e\uff0ctask\u5c31\u4f1a\u88ab\u653e\u5230\u961f\u5217\u91cc\u9762\u53bb\u4e86\u3002<\/li>\n<li>\u6b64\u65f6\u6b64\u523bcompletionQueue\u961f\u5217\u91cc\u9762\u7684 task \u90fd\u662f\u5df2\u7ecf done()\u5b8c\u6210\u4e86\u7684 task\uff0c\u800c\u8fd9\u4e2a task \u5c31\u662f\u6211\u4eec\u62ff\u5230\u7684\u4e00\u4e2a\u4e2a\u7684future\u7ed3\u679c\u3002<\/li>\n<li>\u5982\u679c\u8c03\u7528 completionQueue \u7684 task \u65b9\u6cd5\uff0c\u4f1a\u963b\u585e\u7b49\u5f85\u4efb\u52a1\u3002\u7b49\u5230\u7684\u4e00\u5b9a\u662f\u5b8c\u6210\u4e86\u7684 future\uff0c\u6211\u4eec\u8c03\u7528 .get()\u65b9\u6cd5 \u5c31\u80fd\u7acb\u9a6c\u83b7\u5f97\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"497\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l15\" alt=\"wecom-temp-aaf01e40f5f3fb8023e9d23243cef40f.png\" title=\"wecom-temp-aaf01e40f5f3fb8023e9d23243cef40f.png\" loading=\"lazy\"><\/p>\n<p>\u770b\u5230\u8fd9\u91cc \u76f8\u4fe1\u5927\u5bb6\u4f19\u90fd\u5e94\u8be5\u591a\u5c11\u660e\u767d\u70b9\u4e86<\/p>\n<ul>\n<li>\u6211\u4eec\u5728\u4f7f\u7528ExecutorService submit\u63d0\u4ea4\u4efb\u52a1\u540e\u9700\u8981\u5173\u6ce8\u6bcf\u4e2a\u4efb\u52a1\u8fd4\u56de\u7684future\uff0c\u7136\u800cCompletionService \u5bf9\u8fd9\u4e9b future \u8fdb\u884c\u4e86\u8ffd\u8e2a\uff0c\u5e76\u4e14\u91cd\u5199\u4e86done\u65b9\u6cd5\uff0c\u8ba9\u4f60\u7b49\u7684completionQueue \u961f\u5217\u91cc\u9762 \u4e00\u5b9a\u662f\u5b8c\u6210\u4e86\u7684task\u3002<\/li>\n<li>\u4f5c\u4e3a\u7f51\u5173RPC\u5c42\uff0c\u6211\u4eec\u4e0d\u7528\u56e0\u4e3a\u67d0\u4e00\u4e2a\u63a5\u53e3\u7684\u54cd\u5e94\u6162\u62d6\u7d2f\u6240\u6709\u7684\u8bf7\u6c42\uff0c\u53ef\u4ee5\u5728\u5904\u7406\u6700\u5feb\u54cd\u5e94\u7684\u4e1a\u52a1\u573a\u666f\u91cc\u4f7f\u7528CompletionService\u3002<\/li>\n<\/ul>\n<p><strong>but \u6ce8\u610f\u3001\u6ce8\u610f\u3001\u6ce8\u610f \u4e5f\u662f\u672c\u6b21\u4e8b\u6545\u7684\u6838\u5fc3<\/strong><\/p>\n<p>\u5f53\u53ea\u6709\u8c03\u7528\u4e86ExecutorCompletionService\u4e0b\u9762\u76843\u4e2a\u65b9\u6cd5\u7684\u4efb\u610f\u4e00\u4e2a\u65f6\uff0c\u963b\u585e\u961f\u5217\u4e2d\u7684task\u6267\u884c\u7ed3\u679c\u624d\u4f1a\u4ece\u961f\u5217\u4e2d\u79fb\u9664\u6389,\u91ca\u653e\u5806\u5185\u5b58\uff0c\u7531\u4e8e\u8be5\u4e1a\u52a1\u4e0d\u9700\u8981\u4f7f\u7528\u4efb\u52a1\u7684\u8fd4\u56de\u503c\uff0c\u5219\u6ca1\u8fdb\u884c\u8c03\u7528take\uff0cpoll\u65b9\u6cd5\u3002\u4ece\u800c\u5bfc\u81f4\u6ca1\u6709\u91ca\u653e\u5806\u5185\u5b58\uff0c\u5806\u5185\u5b58\u4f1a\u968f\u7740\u8c03\u7528\u91cf\u7684\u589e\u52a0\u4e00\u76f4\u589e\u957f\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"343\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9l16\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u6240\u4ee5\uff0c\u4e1a\u52a1\u573a\u666f\u4e2d\u4e0d\u9700\u8981\u4f7f\u7528\u4efb\u52a1\u8fd4\u56de\u503c\u7684 \u522b\u6ca1\u4e8b\u513f\u4f7f\u7528CompletionService\uff0c\u5047\u5982\u4f7f\u7528\u4e86\uff0c\u8bb0\u5f97\u4e00\u5b9a\u8981\u4ece\u963b\u585e\u961f\u5217\u4e2d \u79fb\u9664\u6389task\u6267\u884c\u7ed3\u679c\uff0c\u907f\u514dOOM\uff01<\/strong><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p><strong>\u77e5\u9053\u4e8b\u6545\u7684\u539f\u56e0\uff0c\u6211\u4eec\u6765\u603b\u7ed3\u4e0b\u65b9\u6cd5\u8bba\uff0c\u6bd5\u7adf\u5b54\u5b50\u4ed6\u8001\u4eba\u5bb6\u8bf4\u8fc7\uff1a\u81ea\u7701\u543e\u8eab\uff0c\u5e38\u601d\u5df1\u8fc7\uff0c\u5584\u4fee\u5176\u8eab\uff01<\/strong><\/p>\n<p><strong>\u4e0a\u7ebf\u524d\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e25\u683c\u7684\u4ee3\u7801review\u4e60\u60ef\uff0c\u4e00\u5b9a\u8981\u4ea4\u7ed9back\u4eba\u53bb\u770b\uff0c\u6bd5\u7adf\u81ea\u5df1\u5199\u7684\u4ee3\u7801\u81ea\u5df1\u662f\u770b\u4e0d\u51fa\u95ee\u9898\u7684\uff0c\u76f8\u4fe1\u6bcf\u4e2a\u7a0b\u5e8f\u733f\u90fd\u6709\u8fd9\u4e2a\u81ea\u4fe1\uff08\u8fd9\u4e2a\u540e\u7eed\u4e8b\u6545\u91cc\u53ef\u80fd\u4f1a\u53cd\u590d\u63d0\u5230\uff01\u5f88\u91cd\u8981\uff09<\/li>\n<li>\u4e0a\u7ebf\u8bb0\u5f55-\u5907\u6ce8\u597d\u4e0a\u4e00\u4e2a\u53ef\u56de\u6eda\u7684\u5305\u7248\u672c\uff08\u7ed9\u81ea\u5df1\u7559\u4e00\u4e2a\u540e\u8def\uff09<\/li>\n<li>\u4e0a\u7ebf\u524d\u786e\u8ba4\u56de\u6eda\u540e\uff0c\u4e1a\u52a1\u662f\u5426\u53ef\u964d\u7ea7\uff0c\u5982\u679c\u4e0d\u53ef\u964d\u7ea7\uff0c\u4e00\u5b9a\u8981\u4e25\u683c\u62c9\u957f\u8fd9\u6b21\u4e0a\u7ebf\u7684\u76d1\u63a7\u5468\u671f <strong>\u4e0a\u7ebf\u540e\uff1a<\/strong><\/li>\n<li>\u6301\u7eed\u5173\u6ce8\u5185\u5b58\u589e\u957f\u60c5\u51b5\uff08\u8fd9\u90e8\u5206\u6781\u5bb9\u6613\u88ab\u5ffd\u7565\uff0c\u5927\u5bb6\u5bf9\u5185\u5b58\u7684\u91cd\u89c6\u5ea6\u4e0d\u5982cpu\u4f7f\u7528\u7387\uff09<\/li>\n<li>\u6301\u7eed\u5173\u6ce8cpu\u4f7f\u7528\u7387\u589e\u957f\u60c5\u51b5<\/li>\n<li>gc\u60c5\u51b5\u3001\u7ebf\u7a0b\u6570\u662f\u5426\u589e\u957f\u3001\u662f\u5426\u6709\u9891\u7e41\u7684fullgc\u7b49<\/li>\n<li>\u5173\u6ce8\u670d\u52a1\u6027\u80fd\u62a5\u8b66\uff0ctp99\u3001999 \u3001max\u662f\u5426\u51fa\u73b0\u660e\u663e\u7684\u589e\u9ad8<\/li>\n<\/ul>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=KwWIduaIu9yw71BpwGu2GA%3D%3D.pL7Re%2Fx376wjyqAzYeneXQAHhnNDEVi80ilgOw5%2BoUY%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=xMIlrDZggGIwowrXJq25Yg%3D%3D.krf7UiTJ1lMCXOpJ7ZcGxyJWSLmIH0DlwKDaqNVMvQ0rq1DlAE9S4PRp8fH9le5enzvVS2L977TA43aLfFOBMQ%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=JP0kWfHA4b%2Fob5W%2Fmbzt4g%3D%3D.aW4%2FNi1plkOnwZrEu5Fu0mMjk7Q7Qf94ZtgSy66gqjMDzEk9dXCBuTvshwfh9AiQ\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=wN%2BMQcN6O%2B6XTxzT47x9VQ%3D%3D.p33Y%2Ft2y4u7P4zPCqWXfnQtT612LCFUYEGrU21j4dOZosEG6S7POHUJvW5kuNc8Ik35lh5K%2FG5t7I1TTKH0SPA%3D%3D\" rel=\"noopener noreferrer\">\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 target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=Nafuoc98b9k0AOo9UakeHg%3D%3D.efKB79bgxhWkvs1vVa4nDBONGsm2r8kc%2BsHYvN%2FIg0OwSUSiPgk55wycz%2F%2FupKwljPUAlYUpqjB9SEiQW3pQiGly7ki4e%2BKs4GJN%2F4gzgb8%3D\" rel=\"noopener noreferrer\">\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\/1190000044136309\">https:\/\/segmentfault.com\/a\/1190000044136309<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u6811\u6d1e\u541b \/\u94fe\u63a5\uff1ahttps:\/\/juejin.cn\/post\/7064376361334358046 \u4e8b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008132","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008132","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"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008132"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008132\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}