{"id":14564,"date":"2024-05-08T10:26:36","date_gmt":"2024-05-08T02:26:36","guid":{"rendered":"https:\/\/92it.top\/?p=14564"},"modified":"2024-05-08T10:26:36","modified_gmt":"2024-05-08T02:26:36","slug":"%e4%b8%a4%e4%b8%aa%e7%bb%8f%e5%85%b8%e4%be%8b%e5%ad%90%e8%ae%a9%e4%bd%a0%e5%bd%bb%e5%ba%95%e7%90%86%e8%a7%a3java%e5%9b%9e%e8%b0%83%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=14564","title":{"rendered":"\u4e24\u4e2a\u7ecf\u5178\u4f8b\u5b50\u8ba9\u4f60\u5f7b\u5e95\u7406\u89e3java\u56de\u8c03\u673a\u5236"},"content":{"rendered":"\n<p><strong>\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5148\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u751f\u6d3b\u4e2d\u7684\u573a\u666f\u6765\u8fd8\u539f\u4e00\u4e0b\u56de\u8c03\u7684\u573a\u666f\uff1a\u4f60\u9047\u5230\u4e86\u4e00\u4e2a\u6280\u672f\u96be\u9898\uff08\u6bd4\u5982\uff0c1+1\u7b49\u4e8e\u51e0\uff1f\u592a\u96be\u4e86\uff01\uff09\uff0c\u4e8e\u662f\u4f60\u53bb\u54a8\u8be2\u5927\u725b\uff0c\u5927\u725b\u8bf4\u73b0\u5728\u6b63\u5728\u5fd9\uff0c\u5f85\u4f1a\u513f\u544a\u8bc9\u4f60\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u6b64\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u53bb\u5237\u670b\u53cb\u5708\u4e86\uff0c\u7b49\u5927\u725b\u5fd9\u5b8c\u4e4b\u540e\uff0c\u544a\u8bc9\u4f60\u7b54\u6848\u662f2\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u8be2\u95ee\u95ee\u9898\uff08\u8c03\u7528\u5bf9\u65b9\u63a5\u53e3\uff09\uff0c\u7136\u540e\u95ee\u9898\u89e3\u51b3\u4e4b\u540e\u518d\u544a\u8bc9\u4f60\uff08\u5bf9\u65b9\u5904\u7406\u5b8c\u518d\u8c03\u7528\u4f60\uff0c\u901a\u77e5\u7ed3\u679c\uff09\uff0c\u8fd9\u4e00\u8fc7\u7a0b\u4fbf\u662f\u56de\u8c03\u3002<\/p>\n\n\n\n<p><strong>\u7cfb\u7edf\u8c03\u7528\u7684\u5206\u7c7b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5e94\u7528\u7cfb\u7edf\u6a21\u5757\u4e4b\u95f4\u7684\u8c03\u7528\uff0c\u901a\u5e38\u5206\u4e3a\uff1a\u540c\u6b65\u8c03\u7528\uff0c\u5f02\u6b65\u8c03\u7528\uff0c\u56de\u8c03\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"822\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-1024x822.png\" alt=\"\" class=\"wp-image-14565\" style=\"width:472px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-1024x822.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-300x241.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-768x616.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-830x666.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-230x185.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-350x281.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21-480x385.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-21.png 1296w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u540c\u6b65\u8c03\u7528\u662f\u6700\u57fa\u672c\u7684\u8c03\u7528\u65b9\u5f0f\u3002\u7c7bA\u7684a()\u65b9\u6cd5\u8c03\u7528\u7c7bB\u7684b()\u65b9\u6cd5\uff0c\u7c7bA\u7684\u65b9\u6cd5\u9700\u8981\u7b49\u5230B\u7c7b\u7684\u65b9\u6cd5\u6267\u884c\u5b8c\u6210\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\u5982\u679cB\u7684\u65b9\u6cd5\u957f\u65f6\u95f4\u963b\u585e\uff0c\u5c31\u4f1a\u5bfc\u81f4A\u7c7b\u65b9\u6cd5\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u4e0b\u53bb\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"849\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-1024x849.png\" alt=\"\" class=\"wp-image-14566\" style=\"width:466px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-1024x849.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-300x249.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-768x637.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-830x688.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-230x191.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-350x290.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22-480x398.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-22.png 1324w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5982\u679cA\u8c03\u7528B\uff0cB\u7684\u6267\u884c\u65f6\u95f4\u6bd4\u8f83\u957f\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u8003\u8651\u8fdb\u884c\u5f02\u6b65\u5904\u7406\uff0c\u4f7f\u5f97B\u7684\u6267\u884c\u4e0d\u5f71\u54cdA\u3002\u901a\u5e38\u5728A\u4e2d\u65b0\u8d77\u4e00\u4e2a\u7ebf\u7a0b\u7528\u6765\u8c03\u7528B\uff0c\u7136\u540eA\u4e2d\u7684\u4ee3\u7801\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u5f02\u6b65\u901a\u5e38\u5206\u4e24\u79cd\u60c5\u51b5\uff1a\u7b2c\u4e00\uff0c\u4e0d\u9700\u8981\u8c03\u7528\u7ed3\u679c\uff0c\u76f4\u63a5\u8c03\u7528\u5373\u53ef\uff0c\u6bd4\u5982\u53d1\u9001\u6d88\u606f\u901a\u77e5\uff1b\u7b2c\u4e8c\uff0c\u9700\u8981\u5f02\u6b65\u8c03\u7528\u7ed3\u679c\uff0c\u5728Java\u4e2d\u53ef\u4f7f\u7528Future+Callable\u5b9e\u73b0\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"756\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-1024x756.png\" alt=\"\" class=\"wp-image-14568\" style=\"width:472px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-1024x756.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-300x221.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-768x567.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-830x613.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-230x170.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-350x258.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24-480x354.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/05\/image-24.png 1352w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u901a\u8fc7\u4e0a\u56fe\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u56de\u5230\u5c5e\u4e8e\u4e00\u79cd\u53cc\u5411\u7684\u8c03\u7528\u65b9\u5f0f\u3002\u56de\u8c03\u7684\u57fa\u672c\u4e0a\u601d\u8def\u662f\uff1aA\u8c03\u7528B\uff0cB\u5904\u7406\u5b8c\u4e4b\u540e\u518d\u8c03\u7528A\u63d0\u4f9b\u7684\u56de\u8c03\u65b9\u6cd5\uff08\u901a\u5e38\u4e3acallbakc()\uff09\u901a\u77e5\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\u56de\u8c03\u5206\u4e3a\uff1a\u540c\u6b65\u56de\u8c03\u548c\u5f02\u6b65\u56de\u8c03\u3002\u7f51\u7edc\u4e0a\u5927\u591a\u6570\u7684\u56de\u8c03\u6848\u4f8b\u90fd\u662f\u540c\u6b65\u56de\u8c03\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u540c\u6b65\u56de\u8c03\u4e0e\u540c\u6b65\u8c03\u7528\u7c7b\u4f3c\uff0c\u4ee3\u7801\u8fd0\u884c\u5230\u67d0\u4e00\u4e2a\u4f4d\u7f6e\u7684\u65f6\u5019\uff0c\u5982\u679c\u9047\u5230\u4e86\u9700\u8981\u56de\u8c03\u7684\u4ee3\u7801\uff0c\u4f1a\u5728\u8fd9\u91cc\u7b49\u5f85\uff0c\u7b49\u5f85\u56de\u8c03\u7ed3\u679c\u8fd4\u56de\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u800c\u5f02\u6b65\u56de\u8c03\u4e0e\u5f02\u6b65\u8c03\u7528\u7c7b\u4f3c\uff0c\u4ee3\u7801\u6267\u884c\u5230\u9700\u8981\u56de\u8c03\u7684\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u4f1a\u505c\u4e0b\u6765\uff0c\u800c\u662f\u7ee7\u7eed\u6267\u884c\uff0c\u5f53\u7136\u53ef\u80fd\u8fc7\u4e00\u4f1a\u56de\u8c03\u7684\u7ed3\u679c\u4f1a\u8fd4\u56de\u56de\u6765\u3002<\/p>\n\n\n\n<p><strong>\u540c\u6b65\u56de\u8c03\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u4ee5\u540c\u6b65\u56de\u8c03\u4e3a\u4f8b\u6765\u8bb2\u89e3\u56de\u8c03\u7684Java\u4ee3\u7801\u5b9e\u73b0\u3002\u6574\u4e2a\u8fc7\u7a0b\u5c31\u6a21\u62df\u4e0a\u9762\u95ee\u7b54\u95ee\u9898\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u5b9a\u4e49\u7ed9\u4e00\u4e2aCallBack\u7684\u63a5\u53e3\uff0c\u5c06\u56de\u8c03\u7684\u529f\u80fd\u8fdb\u884c\u5355\u72ec\u62bd\u79bb\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public interface CallBack {\n    void callback(String string);\n}\n\n<\/pre>\n\n\n\n<p>CallBack\u63a5\u53e3\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2acallback\u65b9\u6cd5\uff0c\u7528\u4e8e\u56de\u8c03\u65f6\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u5b9a\u4e49\u95ee\u95ee\u9898\u7684\u4ebaPerson\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Person implements CallBack {\n\n    private Genius genius;\n\n    public Person(Genius genius) {\n        this.genius = genius;\n    }\n\n    @Override\n    public void callback(String string) {\n        System.out.println(\"\u6536\u5230\u7b54\u6848\uff1a\" + string);\n    }\n\n    public void ask() {\n        genius.answer(this);\n    }\n\n}<\/pre>\n\n\n\n<p>\u7531\u4e8ePerson\u8981\u63d0\u4f9b\u56de\u8c03\u65b9\u6cd5\uff0c\u56e0\u6b64\u5b9e\u73b0CallBack\u63a5\u53e3\u53ca\u5176\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4e2d\u4e3b\u8981\u9488\u5bf9\u56de\u8c03\u7ed3\u679c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\uff0c\u7531\u4e8ePerson\u8981\u8c03\u7528Genius\u5bf9\u5e94\u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u8981\u6301\u6709Genius\u7684\u5f15\u7528\uff0c\u8fd9\u91cc\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u4f20\u5165\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\u56de\u7b54\u95ee\u9898\u7684\u5927\u795eGenius\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Genius {\n\n    public void answer(CallBack callBack) {\n        System.out.println(\"\u5728\u5fd9\u5176\u4ed6\u4e8b...\");\n        try {\n            Thread.sleep(2000);\n            System.out.println(\"\u5fd9\u5b8c\u5176\u4ed6\u4e8b\uff0c\u5f00\u59cb\u8ba1\u7b97...\");\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n\n        System.out.println(\"\u5929\u624d\u8ba1\u7b97\u51fa\u7b54\u6848\u4e3a\uff1a2\");\n        \/\/ \u56de\u8c03\u544a\u8bc9\u4f60\n        callBack.callback(\"2\");\n    }\n}<\/pre>\n\n\n\n<p>\u8fd9\u6a21\u62df\u5927\u795e\u6b63\u5728\u5fd9\u788c\uff0c\u7ebf\u7a0b\u7761\u77202\u79d2\uff0c\u5fd9\u788c\u5b8c\u4e4b\u540e\uff0c\u5f00\u59cb\u5e2e\u5fd9\u8ba1\u7b97\u7b54\u6848\uff0c\u83b7\u5f97\u7b54\u6848\u4e4b\u540e\uff0c\u8c03\u7528CallBack\u63a5\u53e3\u7684callback\u65b9\u6cd5\u8fdb\u884c\u56de\u8c03\uff0c\u901a\u77e5\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7Main\u65b9\u6cd5\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static void main(String[] args) {\n    Genius genius = new Genius();\n    Person you = new Person(genius);\n    you.ask();\n}<\/pre>\n\n\n\n<p>\u6267\u884c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u5728\u5fd9\u5176\u4ed6\u4e8b...\n\u5fd9\u5b8c\u5176\u4ed6\u4e8b\uff0c\u5f00\u59cb\u8ba1\u7b97...\n\u5929\u624d\u8ba1\u7b97\u51fa\u7b54\u6848\u4e3a\uff1a2\n\u6536\u5230\u7b54\u6848\uff1a2<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u8fc7\u7a0b\uff0c\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u540c\u6b65\u56de\u8c03\u7684\u529f\u80fd\u3002\u5f53\u7136\uff0c\u4ece\u7a0b\u5e8f\u8bbe\u8ba1\u4e0a\u6765\u8bf4\uff0c\u53ef\u4ee5\u5bf9Person\u548cGenius\u8fdb\u4e00\u6b65\u62bd\u8c61\u5316\u5904\u7406\uff0c\u901a\u8fc7\u63a5\u53e3\u7684\u5f62\u5f0f\u5448\u73b0\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u56de\u8c03\u673a\u5236\u7684\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6700\u6838\u5fc3\u7684\u662f\u5728\u8c03\u7528answer\u65b9\u6cd5\u65f6\u4f20\u9012\u4e86this\u53c2\u6570\uff0c\u5373\u8c03\u7528\u8005\u81ea\u8eab\u3002<\/p>\n\n\n\n<p>\u4ece\u672c\u8d28\u4e0a\u6765\u8bf4\uff0c\u56de\u8c03\u662f\u4e00\u79cd\u601d\u60f3\uff0c\u662f\u4e00\u79cd\u673a\u5236\uff0c\u81f3\u4e8e\u5177\u4f53\u5982\u4f55\u5b9e\u73b0\uff0c\u5982\u4f55\u901a\u8fc7\u4ee3\u7801\u5c06\u56de\u8c03\u5b9e\u73b0\u5f97\u4f18\u96c5\u3001\u5b9e\u73b0\u5f97\u53ef\u6269\u5c55\u6027\u6bd4\u8f83\u9ad8\uff0c\u5c31\u9700\u8981\u516b\u4ed9\u8fc7\u6d77\u5404\u663e\u795e\u901a\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u5f02\u6b65\u56de\u8c03\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0a\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u540c\u6b65\u56de\u8c03\uff0c\u5f88\u660e\u663e\u5728\u8c03\u7528\u7684\u8fc7\u53d7\u5230Genius\u6267\u884c\u65f6\u957f\u7684\u5f71\u54cd\uff0c\u9700\u8981\u7b49\u5230Genius\u5904\u7406\u5b8c\u624d\u80fd\u7ee7\u7eed\u6267\u884cPerson\u65b9\u6cd5\u4e2d\u7684\u540e\u7eed\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e0a\u8fdb\u884c\u6539\u8fdb\uff0cPerson\u63d0\u4f9b\u4e00\u4e2a\u652f\u6301\u5f02\u6b65\u56de\u8c03\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> public void askASyn() {\n    System.out.println(\"\u521b\u5efa\u65b0\u7ebf\u7a0b\u8bf7\u6559\u95ee\u9898\");\n    new Thread(() -> genius.answer(this)).start();\n    System.out.println(\"\u65b0\u7ebf\u7a0b\u5df2\u542f\u52a8...\");\n}<\/pre>\n\n\n\n<p>\u5728\u8be5\u65b9\u6cd5\u5185\uff0c\u65b0\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u7528\u6765\u5904\u7406Genius#answer\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u8fd9\u6837\u5c31\u80fd\u591f\u8df3\u8fc7Genius#answer\u65b9\u6cd5\u7684\u963b\u585e\uff0c\u76f4\u63a5\u6267\u884c\u4e0b\u9762\u7684\u64cd\u4f5c\uff08\u65e5\u5fd7\u6253\u5370\uff09\u3002<\/p>\n\n\n\n<p>\u5728main\u65b9\u6cd5\u4e2d\u5c06\u8c03\u7528\u7684\u65b9\u6cd5\u6539\u4e3aaskASyn\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u521b\u5efa\u65b0\u7ebf\u7a0b\u8bf7\u6559\u95ee\u9898\n\u65b0\u7ebf\u7a0b\u5df2\u542f\u52a8...\n\u5728\u5fd9\u5176\u4ed6\u4e8b...\n\u5fd9\u5b8c\u5176\u4ed6\u4e8b\uff0c\u5f00\u59cb\u8ba1\u7b97...\n\u5929\u624d\u8ba1\u7b97\u51fa\u7b54\u6848\u4e3a\uff1a2\n\u6536\u5230\u7b54\u6848\uff1a2<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u76f4\u63a5\u6253\u5370\u4e86\u201c\u65b0\u7ebf\u7a0b\u5df2\u542f\u52a8\u2026\u201d\uff0c\u540e\u7eed\u624d\u6253\u5370\u51faGenius#answer\u65b9\u6cd5\u65b9\u6cd5\u4e2d\u5904\u7406\u65e5\u5fd7\u548c\u56de\u8c03\u65f6callback\u65b9\u6cd5\u63a5\u6536\u5230\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<p><strong>\u57fa\u4e8eFuture\u7684\u534a\u5f02\u6b65<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u9664\u4e86\u4e0a\u8ff0\u7684\u540c\u6b65\uff0c\u5f02\u6b65\u5904\u7406\uff0c\u8fd8\u6709\u4e00\u79cd\u4ecb\u4e8e\u540c\u6b65\u548c\u5f02\u6b65\u4e4b\u95f4\u7684\u57fa\u4e8eFuture\u7684\u534a\u5f02\u6b65\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u5728Java\u4f7f\u7528nio\u540e\u65e0\u6cd5\u7acb\u5373\u62ff\u5230\u771f\u5b9e\u7684\u6570\u636e\uff0c\u800c\u662f\u5148\u5f97\u5230\u4e00\u4e2a&#8221;future&#8221;\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u90ae\u6233\u6216\u5feb\u9012\u5355\uff0c\u4e3a\u4e86\u83b7\u6089\u771f\u6b63\u7684\u6570\u636e\u6211\u4eec\u9700\u8981\u4e0d\u505c\u7684\u901a\u8fc7\u5feb\u9012\u5355\u53f7&#8221;future&#8221;\u67e5\u8be2\u5feb\u9012\u662f\u5426\u771f\u6b63\u5bc4\u5230\u3002<\/p>\n\n\n\n<p>Futures\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u503c\uff0c\u5728\u67d0\u4e00\u70b9\u4f1a\u53d8\u5f97\u53ef\u7528\u3002\u4e00\u4e2aFuture\u8981\u4e48\u83b7\u5f97\u8ba1\u7b97\u5b8c\u7684\u7ed3\u679c\uff0c\u8981\u4e48\u83b7\u5f97\u8ba1\u7b97\u5931\u8d25\u540e\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\u4ec0\u4e48\u65f6\u5019\u4f1a\u7528\u5230Future\u5462\uff1f\u4e00\u822c\u6765\u8bf4\uff0c\u5f53\u6267\u884c\u4e00\u4e2a\u8017\u65f6\u7684\u4efb\u52a1\u65f6\uff0c\u4f7f\u7528Future\u5c31\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u6682\u65f6\u53bb\u5904\u7406\u5176\u4ed6\u7684\u4efb\u52a1\uff0c\u7b49\u957f\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u518d\u8fd4\u56de\u5176\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u7ecf\u5e38\u4f1a\u4f7f\u7528\u5230Future\u7684\u573a\u666f\u6709\uff1a1. \u8ba1\u7b97\u5bc6\u96c6\u573a\u666f\u30022. \u5904\u7406\u5927\u6570\u636e\u91cf\u30023. \u8fdc\u7a0b\u65b9\u6cd5\u8c03\u7528\u7b49\u3002<\/p>\n\n\n\n<p>Java\u5728java.util.concurrent\u5305\u4e2d\u9644\u5e26\u4e86Future\u63a5\u53e3\uff0c\u5b83\u4f7f\u7528Executor\u5f02\u6b65\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u6bcf\u4f20\u9012\u4e00\u4e2aRunnable\u5bf9\u8c61\u5230ExecutorService.submit()\u65b9\u6cd5\u5c31\u4f1a\u5f97\u5230\u4e00\u4e2a\u56de\u8c03\u7684Future\uff0c\u4f7f\u7528\u5b83\u68c0\u6d4b\u662f\u5426\u6267\u884c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u662f\u540c\u6b65\u7b49\u5f85\u7ebf\u5904\u7406\u7ed3\u679c\u5b8c\u6210\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class TestFuture {\n\n    public static void main(String[] args) {\n\n        \/\/\u5b9e\u73b0\u4e00\u4e2aCallable\u63a5\u53e3\n        Callable&lt;User> c = () -> {\n            \/\/\u8fd9\u91cc\u662f\u4e1a\u52a1\u903b\u8f91\u5904\u7406\n\n            \/\/\u8ba9\u5f53\u524d\u7ebf\u7a0b\u963b\u585e1\u79d2\u770b\u4e0b\u6548\u679c\n            Thread.sleep(1000);\n            return new User(\"\u5f20\u4e09\");\n        };\n\n        ExecutorService es = Executors.newFixedThreadPool(2);\n\n        \/\/ \u8bb0\u5f97\u8981\u7528submit\uff0c\u6267\u884cCallable\u5bf9\u8c61\n        Future&lt;User> fn = es.submit(c);\n        \/\/ \u4e00\u5b9a\u8981\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u4e0d\u7136executorService.isTerminated()\u6c38\u8fdc\u4e0d\u4e3atrue\n        es.shutdown();\n        \/\/ \u65e0\u9650\u5faa\u73af\u7b49\u5f85\u4efb\u52a1\u5904\u7406\u5b8c\u6bd5  \u5982\u679c\u5df2\u7ecf\u5904\u7406\u5b8c\u6bd5 isDone\u8fd4\u56detrue\n        while (!fn.isDone()) {\n            try {\n                \/\/\u5904\u7406\u5b8c\u6bd5\u540e\u8fd4\u56de\u7684\u7ed3\u679c\n                User nt = fn.get();\n                System.out.println(nt.name);\n            } catch (InterruptedException | ExecutionException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n\n    static class User {\n        private String name;\n\n        private User(String name) {\n            this.name = name;\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u6b64\u79cd\u60c5\u51b5\u4e0b\u867d\u7136\u662f\u521b\u5efa\u4e86\u65b0\u7ebf\u7a0b\u6765\u8fdb\u884c\u5904\u7406\uff0c\u4f46\u8fd8\u662f\u9700\u8981\u7b49\u5f85\u5904\u7406\u7684\u7ed3\u679c\u3002\u597d\u5904\u662f\u53ef\u4ee5\u5c06\u6279\u91cf\u7684\u5904\u7406\uff0c\u5206\u4e3a\u51e0\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u884c\u5904\u7406\uff0c\u6700\u540e\u5bf9\u7ed3\u679c\u8fdb\u884c\u5408\u5e76\uff0c\u8fbe\u5230\u63d0\u5347\u5904\u7406\u6548\u7387\u7684\u76ee\u7684\u3002<\/p>\n\n\n\n<p><strong>\u5c0f\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u7ecf\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u60f3\u5fc5\u5927\u5bb6\u5bf9Java\u7684\u56de\u8c03\u673a\u5236\u5df2\u7ecf\u6709\u6240\u4e86\u89e3\uff0c\u5728\u5404\u7c7b\u5f00\u6e90\u6846\u67b6\u4e2d\uff0c\u5176\u5b9e\u4e5f\u4f1a\u7ecf\u5e38\u770b\u5230\u56de\u8c03\u7684\u4f7f\u7528\uff0c\u6d3b\u5b66\u6d3b\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5148\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u751f\u6d3b\u4e2d\u7684\u573a\u666f\u6765\u8fd8\u539f\u4e00\u4e0b\u56de\u8c03\u7684\u573a\u666f\uff1a\u4f60\u9047\u5230\u4e86\u4e00\u4e2a\u6280\u672f\u96be\u9898\uff08\u6bd4\u5982\uff0c1+1\u7b49\u4e8e\u51e0\uff1f\u592a\u96be\u4e86\uff01\uff09\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/14564"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14564"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/14564\/revisions"}],"predecessor-version":[{"id":14569,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/14564\/revisions\/14569"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}