{"id":4292,"date":"2023-07-19T14:20:22","date_gmt":"2023-07-19T06:20:22","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4292"},"modified":"2023-07-19T14:25:43","modified_gmt":"2023-07-19T06:25:43","slug":"completionservice%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4292","title":{"rendered":"CompletionService\u8be6\u89e3"},"content":{"rendered":"\n<p>CompletionService\u7684\u4f5c\u7528\u548c\u5177\u4f53\u7528\u6cd5\uff1f<\/p>\n\n\n\n<p>Java\u4e2d\u7684<code>CompletionService<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u63a5\u53e3\uff0c\u5b83\u5c06Executor\u548cBlockingQueue\u7684\u529f\u80fd\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u4f60\u53ef\u4ee5\u5c06Callable\u4efb\u52a1\u63d0\u4ea4\u7ed9\u5b83\u53bb\u6267\u884c\uff0c\u7136\u540e\u4f7f\u7528\u7c7b\u4f3c\u4e8e\u961f\u5217\u7684\u64cd\u4f5c\uff0c\u5982take\u6216poll\uff0c\u6765\u83b7\u53d6\u5df2\u5b8c\u6210\u7684\u7ed3\u679c\u3002\u8fd9\u662f\u4e00\u4e2a\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u7684\u8bbe\u8ba1\uff0c\u5176\u4e2dCallable\u4efb\u52a1\u662f\u751f\u4ea7\u8005\uff0c\u4ea7\u751f\u7684\u7ed3\u679c\u88ab\u653e\u5165\u961f\u5217\uff0c\u800c\u7528\u6237\u901a\u8fc7take\u6216poll\u64cd\u4f5c\u4f5c\u4e3a\u6d88\u8d39\u8005\u53d6\u51fa\u5e76\u6d88\u8d39\u8fd9\u4e9b\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528<code>CompletionService<\/code>\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy code<code>\/\/ \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\nExecutorService executor = Executors.newFixedThreadPool(3);\n\/\/ \u4f7f\u7528ExecutorCompletionService\uff0c\u5b83\u662fCompletionService\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\nCompletionService&lt;String&gt; service = new ExecutorCompletionService&lt;&gt;(executor);\n\n\/\/ \u63d0\u4ea4\u4efb\u52a1\nfor (int i = 0; i &lt; 5; i++) {\n    int finalI = i;\n    service.submit(() -&gt; {\n        Thread.sleep(1000 * finalI);\n        return \"Result of Task \" + finalI;\n    });\n}\n\n\/\/ \u68c0\u7d22\u5e76\u8f93\u51fa\u7ed3\u679c\nfor (int i = 0; i &lt; 5; i++) {\n    try {\n        System.out.println(service.take().get());\n    } catch (InterruptedException | ExecutionException e) {\n        e.printStackTrace();\n    }\n}\n\n\/\/ \u5173\u95ed\u7ebf\u7a0b\u6c60\nexecutor.shutdown();\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u63d0\u4ea4\u4e865\u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u4f1a\u4f11\u7720\u4e00\u6bb5\u65f6\u95f4\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u7531\u4e8e<code>CompletionService<\/code>\u5185\u90e8\u4f7f\u7528\u4e86\u963b\u585e\u961f\u5217\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528<code>take<\/code>\u65b9\u6cd5\u6309\u7167\u4efb\u52a1\u5b8c\u6210\u7684\u987a\u5e8f\uff08\u800c\u975e\u63d0\u4ea4\u7684\u987a\u5e8f\uff09\u83b7\u53d6\u7ed3\u679c\u3002\u6ce8\u610f\uff0c<code>take<\/code>\u65b9\u6cd5\u4f1a\u963b\u585e\uff0c\u5982\u679c\u4f60\u60f3\u8981\u975e\u963b\u585e\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>poll<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u522b\u5fd8\u4e86\u5728\u5b8c\u6210\u4efb\u52a1\u540e\u5173\u95ed\u7ebf\u7a0b\u6c60\u3002<\/p>\n\n\n\n<p><code>CompletionService<\/code>\u7684\u4e3b\u8981\u7528\u9014\u662f\u5904\u7406\u5927\u91cf\u72ec\u7acb\u7684\u4efb\u52a1\uff0c\u8fd9\u4e9b\u4efb\u52a1\u53ef\u80fd\u4f1a\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u5b8c\u6210\uff0c\u5e76\u4e14\u4f60\u5e0c\u671b\u7acb\u5373\u5904\u7406\u8fd9\u4e9b\u5df2\u5b8c\u6210\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528poll\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u957f\u65f6\u95f4\u6ca1\u6709\u8fd4\u56de\u600e\u4e48\u5904\u7406\uff1f<\/p>\n\n\n\n<p>\u4f7f\u7528<code>poll<\/code>\u65b9\u6cd5\u53ef\u4ee5\u975e\u963b\u585e\u5730\u83b7\u53d6<code>CompletionService<\/code>\u7684\u7ed3\u679c\u3002\u5b83\u4f1a\u7acb\u5373\u8fd4\u56de\u4e00\u4e2a<code>Future<\/code>\uff0c\u5982\u679c\u6ca1\u6709\u4efb\u52a1\u5b8c\u6210\uff0c\u5219\u8fd4\u56de<code>null<\/code>\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u8981\u957f\u65f6\u95f4\u6ca1\u6709\u8fd4\u56de\u7ed3\u679c\u65f6\u8fdb\u884c\u7279\u5b9a\u5904\u7406\uff0c\u53ef\u4ee5\u8003\u8651\u8bbe\u7f6e\u4e00\u4e2a\u8d85\u65f6\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5faa\u73af\u5730\u8c03\u7528<code>poll<\/code>\uff0c\u6bcf\u6b21\u8c03\u7528\u540e\u68c0\u67e5\u662f\u5426\u8fd4\u56de\u4e86\u7ed3\u679c\uff0c\u5982\u679c\u6ca1\u6709\u7ed3\u679c\uff0c\u5219\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u518d\u7ee7\u7eed\u3002\u5982\u679c\u8d85\u8fc7\u4e86\u4f60\u8bbe\u5b9a\u7684\u603b\u8d85\u65f6\u65f6\u95f4\uff0c\u4f60\u53ef\u4ee5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy code<code>ExecutorService executor = Executors.newFixedThreadPool(3);\nCompletionService&lt;String&gt; service = new ExecutorCompletionService&lt;&gt;(executor);\n\nfor (int i = 0; i &lt; 5; i++) {\n    int finalI = i;\n    service.submit(() -&gt; {\n        Thread.sleep(1000 * finalI);\n        return \"Result of Task \" + finalI;\n    });\n}\n\nlong timeout = 10000; \/\/ 10 seconds\nlong start = System.currentTimeMillis();\n\nfor (int i = 0; i &lt; 5; i++) {\n    while (true) {\n        Future&lt;String&gt; future = service.poll();\n        if (future != null) {\n            try {\n                System.out.println(future.get());\n                break;\n            } catch (InterruptedException | ExecutionException e) {\n                e.printStackTrace();\n            }\n        } else {\n            \/\/ \u68c0\u67e5\u662f\u5426\u8d85\u65f6\n            if (System.currentTimeMillis() - start &gt; timeout) {\n                throw new RuntimeException(\"Timeout after \" + timeout + \" milliseconds\");\n            }\n\n            \/\/ \u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u518d\u7ee7\u7eed\n            try {\n                Thread.sleep(1000);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n\nexecutor.shutdown();\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u6bcf\u6b21\u8c03\u7528<code>poll<\/code>\u5e76\u672a\u83b7\u5f97\u7ed3\u679c\u540e\uff0c\u7b49\u5f851\u79d2\u518d\u7ee7\u7eed\u3002\u5982\u679c\u603b\u65f6\u95f4\u8d85\u8fc7\u4e8610\u79d2\uff0c\u6211\u4eec\u5c06\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u4f7f\u7528\u4e86\u4e00\u4e2a\u56fa\u5b9a\u7684\u7b49\u5f85\u65f6\u95f4\uff081\u79d2\uff09\uff0c\u5e76\u4e14\u4f1a\u5728\u6bcf\u6b21<code>poll<\/code>\u8fd4\u56de<code>null<\/code>\u65f6\u7b49\u5f85\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u4f7f\u7528\u66f4\u590d\u6742\u7684\u903b\u8f91\uff0c\u4f8b\u5982\u9010\u6b65\u589e\u52a0\u7b49\u5f85\u65f6\u95f4\uff0c\u6216\u8005\u53ea\u5728\u8fde\u7eed\u591a\u6b21<code>poll<\/code>\u8fd4\u56de<code>null<\/code>\u540e\u7b49\u5f85\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e00\u4e0b\u662f\u53e6\u4e00\u7bc7\u6587\u7ae0\uff0c\u4ecb\u7ecd\u57fa\u672c\u7528\u6cd5\u7684\uff1a<\/p>\n\n\n\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/blog.csdn.net\/qq_40276626\/article\/details\/119961854\">(48\u6761\u6d88\u606f) CompletionService\u8be6\u89e3_\u5f20\u5b5f\u6d69_jay\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/a><\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u6709\u6279\u91cf\u6267\u884c\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u7684\u4e1a\u52a1\uff0c\u5e76\u4e14\u9700\u8981\u8c03\u7528get()\u83b7\u53d6\u6267\u884c\u7ed3\u679c\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5199\u7684\u4ee3\u7801\u4e00\u822c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>Future f1 = excutor.submit(c1);<br>f1.get();<br>Future f2 = excutor.submit(c2);<br>f2.get();<\/p>\n\n\n\n<p>f1.get()\u5728\u83b7\u53d6\u6210\u529f\u4e4b\u524d\u4f1a\u88ab\u963b\u585e\uff0c\u4f1a\u963b\u585ec2\u7684\u6267\u884c\uff0c\u4e25\u91cd\u964d\u4f4e\u4e86\u6548\u7387\u3002<\/p>\n\n\n\n<p>\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7CompletionService\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CompletionService\u7ed3\u6784<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"794\" height=\"429\" src=\"https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-17.png\" alt=\"\" class=\"wp-image-4296\" srcset=\"https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-17.png 794w, https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-17-300x162.png 300w, https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-17-768x415.png 768w\" sizes=\"(max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n\n<p>CompletionSerive\u63a5\u53e3\u6709\u4e00\u4e2a\u5b9e\u73b0\u7c7bExecutorCompletionService\uff0c\u7ed3\u6784\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"883\" src=\"https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-18.png\" alt=\"\" class=\"wp-image-4299\" srcset=\"https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-18.png 813w, https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-18-276x300.png 276w, https:\/\/www.zeroheart.xyz\/wordpress\/wp-content\/uploads\/2023\/07\/image-18-768x834.png 768w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/figure>\n\n\n\n<p>BlockingQueue q = new LinkedBlockingQueue();<br>ThreadPoolExecutor executor = new ThreadPoolExecutor(4,6,3, TimeUnit.SECONDS,q);<br>CompletionService cs = new ExecutorCompletionService&lt;&gt;(executor);<\/p>\n\n\n\n<p>cs.submit(new Callable() {<br>@Override<br>public Integer call() throws Exception {<br>Thread.sleep(2000);<br>return 1;<br>}<br>});<br>cs.submit(new Callable() {<br>@Override<br>public Integer call() throws Exception {<br>Thread.sleep(2000);<br>return 1;<br>}<br>});<\/p>\n\n\n\n<p>for (int i = 0;i &lt; 3;i++){<br>Integer f = cs.take().get();<br>System.out.println(f);<br>}<\/p>\n\n\n\n<p>\u6e90\u7801\u89e3\u6790\uff1a<\/p>\n\n\n\n<p>public ExecutorCompletionService(Executor executor,<br>BlockingQueue&gt; completionQueue) {<br>if (executor == null || completionQueue == null)<br>throw new NullPointerException();<br>\/\/\u7ebf\u7a0b\u6c60<br>this.executor = executor;<br>\/\/AbstractExecutorService<br>this.aes = (executor instanceof AbstractExecutorService) ?<br>(AbstractExecutorService) executor : null;<br>\/\/\u4fdd\u5b58Future\u7684\u961f\u5217<br>this.completionQueue = completionQueue;<br>}<\/p>\n\n\n\n<p>public Future submit(Callable task) {<br>if (task == null) throw new NullPointerException();<br>\/\/\u5305\u88c5\u6210FutureTask<br>RunnableFuture f = newTaskFor(task);<br>\/\/\u4f1a\u8c03\u7528FutureTask\u7684run()<br>executor.execute(new QueueingFuture(f));<br>return f;<br>}<\/p>\n\n\n\n<p>private class QueueingFuture extends FutureTask {<br>QueueingFuture(RunnableFuture task) {<br>super(task, null);<br>this.task = task;<br>}<br>\/\/\u5c06task\u52a0\u5165\u5230completionQueue<br>protected void done() { completionQueue.add(task); }<br>private final Future task;<br>}<\/p>\n\n\n\n<p>public void run() {<br>if (state != NEW ||<br>!UNSAFE.compareAndSwapObject(this, runnerOffset,<br>null, Thread.currentThread()))<br>return;<br>try {<br>Callable c = callable;<br>if (c != null &amp;&amp; state == NEW) {<br>V result;<br>boolean ran;<br>try {<br>\/\/\u8c03\u7528call()\u65b9\u6cd5\uff0cresult\u4e3a\u6267\u884c\u7684\u7ed3\u679c<br>result = c.call();<br>ran = true;<br>} catch (Throwable ex) {<br>result = null;<br>ran = false;<br>setException(ex);<br>}<br>if (ran)<br>\/\/\u8fd9\u91cc\u8fd9\u91cc\u8fd9\u91cc<br>set(result);<br>}<br>} finally {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        runner = null;\n\n        int s = state;\n        if (s &gt;= INTERRUPTING)\n            handlePossibleCancellationInterrupt(s);\n    }<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>protected void set(V v) {<br>if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {<br>\/\/\u5c06FutureTask\u7684outcome\u8bbe\u7f6eresult<br>outcome = v;<br>UNSAFE.putOrderedInt(this, stateOffset, NORMAL); \/\/ final state<br>\/\/\u8fd9\u91cc\u8fd9\u6765\u8fd9\u91cc<br>finishCompletion();<br>}<br>}<\/p>\n\n\n\n<p>private void finishCompletion() {<br>\/\/ assert state &gt; COMPLETING;<br>for (WaitNode q; (q = waiters) != null;) {<br>if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {<br>for (;;) {<br>Thread t = q.thread;<br>if (t != null) {<br>q.thread = null;<br>LockSupport.unpark(t);<br>}<br>WaitNode next = q.next;<br>if (next == null)<br>break;<br>q.next = null; \/\/ unlink to help gc<br>q = next;<br>}<br>break;<br>}<br>}<br>\/\/\u5c06task\u52a0\u5165\u5230completionQueue<br>done();<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>callable = null;        \/\/ to reduce footprint<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\u603b\u7ed3<br>\u4ece\u6e90\u7801\u89e3\u6790\u770b\uff0cExecutorCompletionService\u4e2d\u5185\u7f6e\u4e86\u4e00\u4e2aFuture\u7684completionQueue\uff0c\u5728\u4efb\u52a1\u8c03\u7528\u5b8c\u6210\u540e\uff0c\u4f1a\u5c06submit\u8fd4\u56de\u7684future\u653e\u5165\u5230completionQueue\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7completionQueue.take()\u5f97\u5230future\u7136\u540e\u8c03\u7528future.get()\u6765\u83b7\u53d6\u4efb\u52a1\u6267\u884c\u7ed3\u679c\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CompletionService\u7684\u4f5c\u7528\u548c\u5177\u4f53\u7528\u6cd5\uff1f Java\u4e2d\u7684CompletionService\u662f\u4e00\u4e2a\u975e\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[219],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4292"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4292"}],"version-history":[{"count":13,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4292\/revisions"}],"predecessor-version":[{"id":4307,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4292\/revisions\/4307"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}