{"id":9471,"date":"2023-11-28T21:03:42","date_gmt":"2023-11-28T13:03:42","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9471"},"modified":"2023-11-28T21:04:18","modified_gmt":"2023-11-28T13:04:18","slug":"java-duo-xian-chengcallable-hefuture-jie-kou-qu-bi","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-duo-xian-chengcallable-hefuture-jie-kou-qu-bi\/","title":{"rendered":"Java\u591a\u7ebf\u7a0bCallable\u548cFuture\u63a5\u53e3\u533a\u522b\uff1f"},"content":{"rendered":"<p>Runnable\u662f\u6267\u884c\u5de5\u4f5c\u7684\u72ec\u7acb\u4efb\u52a1\uff0c\u4f46\u662f\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4efb\u52a1\u5b8c\u6210\u4e4b\u540e\u6709\u8fd4\u56de\u503c, \u53ef\u4ee5\u5b9e\u73b0Callable\u63a5\u53e3\u3002\u5728JavaSE5\u4e2d\u5f15\u5165\u7684Callable\u662f\u4e00\u4e2a\u5177\u6709\u7c7b\u578b\u53c2\u6570\u7684\u8303\u578b\uff0c\u4ed6\u7684\u7c7b\u578b\u53c2\u6570\u65b9\u6cd5\u8868\u793a\u4e3a\u65b9\u6cd5call()\u800c\u4e0d\u662frun()\u4e2d\u8fd4\u56de\u7684\u503c\uff0c\u5e76\u4e14\u5fc5\u987b\u4f7f\u7528<br \/>\nExecutorService.submint()\u65b9\u6cd5\u8fdb\u884c\u8c03\u7528\u3002<br \/>\n\u4ee3\u7801\u5982\u4e0b<br \/>\n\u5b9e\u73b0Callable\u63a5\u53e3\u3002\u76f8\u8f83\u4e8e\u5b9e\u73b0Runnable\u63a5\u53e3\u7684\u65b9\u5f0f\uff0c\u65b9\u6cd5\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\uff0c\u5e76\u4e14\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\u3002<br \/>\n\u6267\u884cCallable\u65b9\u5f0f\uff0c\u9700\u8981FutureTask\u5b9e\u73b0\u7c7b\u7684\u652f\u6301\uff0c\u7528\u4e8e\u63a5\u6536\u8fd0\u7b97\u7ed3\u679c\u3002FutureTask\u662f Future\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b<\/p>\n<pre><code class=\"language-public\" data-meta=\"class TestCallable {\">public static void main(String[] args) {\nThreadDemo td =new ThreadDemo();\n\/\/1.\u6267\u884cCallable\u65b9\u5f0f\uff0c\u9700\u8981FutureTask\u5b9e\u73b0\u7c7b\u7684\u652f\u6301\uff0c\u7528\u4e8e\u63a5\u6536\u8fd0\u7b97\u7ed3\u679c\u3002\nFutureTask&lt;Integer&gt; result =new FutureTask&lt;&gt;(td);\nnew Thread(result).start();\n\/\/2.\u63a5\u6536\u7ebf\u7a0b\u8fd0\u7b97\u540e\u7684\u7ed3\u679c\ntry {\nInteger sum = result.get(); \/\/FutureTask \u53ef\u7528\u4e8e \u95ed\u9501 \u7c7b\u4f3c\u4e8e CountDownLatch \u7684\u4f5c\u7528\uff0c\u5728\u6240\u6709\u7684\u7ebf \u7a0b\u6ca1\u6709\u6267\u884c\u5b8c\u6210\u4e4b\u540e\u8fd9\u91cc\u662f\u4e0d\u4f1a\u6267\u884c\u7684\nSystem.out.println(sum);\nSystem.out.println(&quot;\t\u201d);\n}catch (InterruptedException | ExecutionException e) {\ne.printStackTrace();\n}\n}\n}\nclass ThreadDemo implements Callable&lt;Integer&gt; {\n@Override\npublic Integer call() throws Exception {\nint sum = 0;\nfor (int i = 0; i &lt;= 100000; i++) {\nsum += i;\n}\nreturn sum;\n}\n}\n\n```\u7efc\u4e0a\u4f8b\u5b50\u53ef\u4ee5\u770b\u5230\uff1aCallable\u548cFuture\u63a5\u53e3\u7684\u533a\u522b\n1\u3001\tCallable\u89c4\u5b9a\u7684\u65b9\u6cd5\u662fcall(),\u800cRunnable\u89c4\u5b9a\u7684\u65b9\u6cd5\u662frun().\n2\u3001\tCallable\u7684\u4efb\u52a1\u6267\u884c\u540e\u53ef\u8fd4\u56de\u503c\uff0c\u800cRunnable\u7684\u4efb\u52a1\u662f\u4e0d\u80fd\u8fd4\u56de\u503c\u7684\u3002\n3\u3001\tcall()\u65b9\u6cd5\u53ef\u629b\u51fa\u5f02\u5e38\uff0c\u800crun()\u65b9\u6cd5\u662f\u4e0d\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u3002\n4\u3001\t\u8fd0\u884cCallable\u4efb\u52a1\u53ef\u62ff\u5230\u4e00\u4e2aFuture\u5bf9\u8c61\uff0cFuture\u8868\u793a\u5f02\u6b65\u8ba1\u7b97\u7684\u7ed3\u679c\u3002\n5\u3001\t\u5b83\u63d0\u4f9b\u4e86\u68c0\u67e5\u8ba1\u7b97\u662f\u5426\u5b8c\u6210\u7684\u65b9\u6cd5\uff0c\u4ee5\u7b49\u5f85\u8ba1\u7b97\u7684\u5b8c\u6210\uff0c\u5e76\u68c0\u7d22\u8ba1\u7b97\u7684\u7ed3\u679c\u3002\n6\u3001\t\u901a\u8fc7Future\u5bf9\u8c61\u53ef\u4e86\u89e3\u4efb\u52a1\u6267\u884c\u60c5\u51b5\uff0c\u53ef\u53d6\u6d88\u4efb\u52a1\u7684\u6267\u884c\uff0c\u8fd8\u53ef\u83b7\u53d6\u4efb\u52a1\u6267\u884c\u7684\u7ed3\u679c\u3002\n7\u3001\tCallable\u662f\u7c7b\u4f3c\u4e8eRunnable\u7684\u63a5\u53e3\uff0c\u5b9e\u73b0Callable\u63a5\u53e3\u7684\u7c7b\u548c\u5b9e\u73b0Runnable\u7684\u7c7b\u90fd\u662f \u53ef\u88ab\u5176\u5b83\u7ebf\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u3002\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Runnable\u662f\u6267\u884c\u5de5\u4f5c\u7684\u72ec\u7acb\u4efb\u52a1\uff0c\u4f46\u662f\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4efb\u52a1\u5b8c\u6210\u4e4b\u540e\u6709\u8fd4\u56de\u503c, \u53ef\u4ee5\u5b9e\u73b0Calla [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-9471","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=9471"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9471\/revisions"}],"predecessor-version":[{"id":9472,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9471\/revisions\/9472"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9471"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}