{"id":7437,"date":"2022-09-28T22:37:36","date_gmt":"2022-09-28T14:37:36","guid":{"rendered":"http:\/\/123.57.164.21\/?p=7437"},"modified":"2022-09-28T22:37:36","modified_gmt":"2022-09-28T14:37:36","slug":"java-%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b-5-%e7%a7%8d%e5%bc%82%e6%ad%a5%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=7437","title":{"rendered":"Java \u5f02\u6b65\u7f16\u7a0b (5 \u79cd\u5f02\u6b65\u5b9e\u73b0\u65b9\u5f0f\u8be6\u89e3)"},"content":{"rendered":"\n<p>\u540c\u6b65\u64cd\u4f5c\u5982\u679c\u9047\u5230\u4e00\u4e2a\u8017\u65f6\u7684\u65b9\u6cd5\uff0c\u9700\u8981\u963b\u585e\u7b49\u5f85\uff0c\u90a3\u4e48\u6211\u4eec\u6709\u6ca1\u6709\u529e\u6cd5\u89e3\u51b3\u5462\uff1f\u8ba9\u5b83\u5f02\u6b65\u6267\u884c\uff0c\u4e0b\u9762\u6211\u4f1a\u8be6\u89e3\u5f02\u6b65\u53ca\u5b9e\u73b0<\/p>\n\n\n\n<p><strong>\u4ec0\u4e48\u662f\u5f02\u6b65\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u5148\u6765\u770b\u770b\u4e00\u4e2a\u540c\u6b65\u7684\u7528\u6237\u6ce8\u518c\u4f8b\u5b50\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/image-1-402x1024.png\" alt=\"\" class=\"wp-image-7438\" width=\"230\" height=\"586\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-1-402x1024.png 402w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-1-118x300.png 118w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-1-230x586.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-1-350x891.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-1.png 476w\" sizes=\"(max-width: 230px) 100vw, 230px\" \/><\/figure><\/div>\n\n\n\n<p>\u5728\u540c\u6b65\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u6267\u884c\u5230\u63d2\u5165\u6570\u636e\u5e93\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5fc5\u987b\u7b49\u5f85\u8fd9\u4e2a\u65b9\u6cd5\u5f7b\u5e95\u6267\u884c\u5b8c\u624d\u80fd\u6267\u884c\u201c\u53d1\u9001\u77ed\u4fe1\u201d\u8fd9\u4e2a\u64cd\u4f5c\uff0c\u5982\u679c\u63d2\u5165\u6570\u636e\u5e93\u8fd9\u4e2a\u52a8\u4f5c\u6267\u884c\u65f6\u95f4\u8f83\u957f\uff0c\u53d1\u9001\u77ed\u4fe1\u9700\u8981\u7b49\u5f85\uff0c\u8fd9\u5c31\u662f\u5178\u578b\u7684\u540c\u6b65\u573a\u666f\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\u806a\u660e\u7684\u4eba\u4eec\u5f00\u59cb\u601d\u8003\uff0c\u5982\u679c\u4e24\u8005\u5173\u8054\u6027\u4e0d\u5f3a\uff0c\u80fd\u4e0d\u80fd\u5c06\u4e00\u4e9b\u975e\u6838\u5fc3\u4e1a\u52a1\u4ece\u4e3b\u6d41\u7a0b\u4e2d\u5265\u79bb\u51fa\u6765\uff0c\u4e8e\u662f\u6709\u4e86\u5f02\u6b65\u7f16\u7a0b\u96cf\u5f62\uff0c\u6539\u8fdb\u540e\u7684\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/image-2-814x1024.png\" alt=\"\" class=\"wp-image-7439\" width=\"464\" height=\"584\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-2-814x1024.png 814w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-2-238x300.png 238w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-2-768x967.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-2-230x289.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-2-350x440.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-2-480x604.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/image-2.png 820w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u5c31\u662f\u5f02\u6b65\u7f16\u7a0b\uff0c\u5b83\u662f\u7a0b\u5e8f\u5e76\u53d1\u8fd0\u884c\u7684\u4e00\u79cd\u624b\u6bb5\uff0c\u5b83\u5141\u8bb8\u591a\u4e2a\u4e8b\u4ef6\u540c\u65f6\u53d1\u751f\uff0c\u5f53\u7a0b\u5e8f\u8c03\u7528\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u65b9\u6cd5\u65f6\uff0c\u5b83\u4e0d\u4f1a\u963b\u585e\u5f53\u524d\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>\u5728\u804a\u5b8c\u5f02\u6b65\u7f16\u7a0b\u540e\uff0c\u90a3\u4e48\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770bJava\u91cc\u9762\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u7a76\u7adf\u6709\u54ea\u4e9b\u65b9\u5f0f\u5462\uff1f<\/p>\n\n\n\n<p><strong>\u4e00\u3001\u7ebf\u7a0b\u5f02\u6b65<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728 Java \u8bed\u8a00\u4e2d\u6700\u7b80\u5355\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\u7684\u65b9\u5f0f\u5c31\u662f\u521b\u5efa\u4e00\u4e2a \u7ebf\u7a0b\u6765\u5b9e\u73b0\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684 JDK \u7248\u672c\u662f 8 \u4ee5\u4e0a\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f \u4f1a\u66f4\u52a0\u7b80\u6d01\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 AsyncThread extends Thread{\n    @Override\n    public void run() {\n        System.out.println(\"\u5f53\u524d\u7ebf\u7a0b\u540d\u79f0:\" + this.getName() + \", \u6267\u884c\u7ebf\u7a0b\u540d\u79f0:\" + Thread.currentThread().getName() + \"-hello\");\n    }\n}<\/pre>\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 \n  \/\/ \u6a21\u62df\u4e1a\u52a1\u6d41\u7a0b\n  \/\/ .......\n  \n    \/\/ \u521b\u5efa\u5f02\u6b65\u7ebf\u7a0b \n    AsyncThread asyncThread = new AsyncThread();\n \n    \/\/ \u542f\u52a8\u5f02\u6b65\u7ebf\u7a0b\n    asyncThread.start();\n}<\/pre>\n\n\n\n<p>\u5f53\u7136\u5982\u679c\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a Thread\u7ebf\u7a0b\uff0c\u9891\u7e41\u7684\u521b\u5efa\u3001\u9500\u6bc1\uff0c\u6d6a\u8d39\u7cfb\u7edf\u8d44\u6e90\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528<a href=\"https:\/\/so.csdn.net\/so\/search?q=%E7%BA%BF%E7%A8%8B%E6%B1%A0&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">\u7ebf\u7a0b\u6c60<\/a>\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=\"\">private ExecutorService executor = Executors.newCachedThreadPool() ;\n \n    public void fun() throws Exception {\n \n        executor.submit(new Runnable(){\n \n            @override\n \n                public void run() {\n \n                    try {\n                     \/\/\u8981\u6267\u884c\u7684\u4e1a\u52a1\u4ee3\u7801\uff0c\u6211\u4eec\u8fd9\u91cc\u6ca1\u6709\u5199\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u4f11\u606f\u51e0\u79d2\u8fdb\u884c\u6d4b\u8bd5\n \n                        Thread.sleep(10000);\n \n                        System.out.print(\"\u7761\u591f\u5566~\");\n \n                    }catch(Exception e) {\n \n                        throw new RuntimeException(\"\u62a5\u9519\u5566\uff01\uff01\");\n \n                    }\n \n                }\n \n        });\n \n    }<\/pre>\n\n\n\n<p>\u5c06\u4e1a\u52a1\u903b\u8f91\u5c01\u88c5\u5230 Runnable \u6216 Callable \u4e2d\uff0c\u4ea4\u7531 \u7ebf\u7a0b\u6c60 \u6765\u6267\u884c\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001Future\u5f02\u6b65<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0a\u8ff0\u65b9\u5f0f\u867d\u7136\u8fbe\u5230\u4e86\u591a\u7ebf\u7a0b\u5e76\u884c\u5904\u7406\uff0c\u4f46\u6709\u4e9b\u4e1a\u52a1\u4e0d\u4ec5\u4ec5\u8981\u6267\u884c\u8fc7\u7a0b\uff0c\u8fd8\u8981\u83b7\u53d6\u6267\u884c\u7ed3\u679c\uff0c\u540e\u7eed\u63d0\u4f9b\u5728JUC\u5305\u589e\u52a0\u4e86Future\u3002<\/p>\n\n\n\n<p>\u4ece\u5b57\u9762\u610f\u601d\u7406\u89e3\u5c31\u662f\u672a\u6765\u7684\u610f\u601d\uff0c\u4f46\u4f7f\u7528\u8d77\u6765\u5374\u7740\u5b9e\u6709\u70b9\u9e21\u808b\uff0c\u5e76\u4e0d\u80fd\u5b9e\u73b0\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u5f02\u6b65\uff0c\u83b7\u53d6\u7ed3\u679c\u65f6\u9700\u8981\u963b\u585e\u7ebf\u7a0b\uff0c\u6216\u8005\u4e0d\u65ad\u8f6e\u8be2\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=\"\">@Test\npublic void futureTest() throws Exception {\n \n    System.out.println(\"main\u51fd\u6570\u5f00\u59cb\u6267\u884c\");\n \n    ExecutorService executor = Executors.newFixedThreadPool(1);\n    Future&lt;Integer> future = executor.submit(new Callable&lt;Integer>() {\n        @Override\n        public Integer call() throws Exception {\n \n            System.out.println(\"===task start===\");\n            Thread.sleep(5000);\n            System.out.println(\"===task finish===\");\n            return 3;\n        }\n    });\n    \/\/\u8fd9\u91cc\u9700\u8981\u8fd4\u56de\u503c\u65f6\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u5982\u679c\u4e0d\u9700\u8981\u8fd4\u56de\u503c\u4f7f\u7528\u662fOK\u7684\u3002\u5012\u4e5f\u8fd8\u80fd\u63a5\u6536\n    \/\/Integer result=future.get();\n    System.out.println(\"main\u51fd\u6570\u6267\u884c\u7ed3\u675f\");\n \n    System.in.read();\n \n}<\/pre>\n\n\n\n<p><strong>\u4e09\u3001CompletableFuture\u5f02\u6b65<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Future \u7c7b\u901a\u8fc7 get() \u65b9\u6cd5\u963b\u585e\u7b49\u5f85\u83b7\u53d6\u5f02\u6b65\u6267\u884c\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u6027\u80fd\u6bd4\u8f83\u5dee\u3002<\/p>\n\n\n\n<p>JDK1.8 \u4e2d\uff0cJava \u63d0\u4f9b\u4e86 CompletableFuture \u7c7b\uff0c\u5b83\u662f\u57fa\u4e8e\u5f02\u6b65\u51fd\u6570\u5f0f\u7f16\u7a0b\u3002\u76f8\u5bf9\u963b\u585e\u5f0f\u7b49\u5f85\u8fd4\u56de\u7ed3\u679c\uff0cCompletableFuture \u53ef\u4ee5\u901a\u8fc7\u56de\u8c03\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8ba1\u7b97\u7ed3\u679c\uff0c\u5b9e\u73b0\u4e86\u5f02\u6b65\u975e\u963b\u585e\uff0c\u6027\u80fd\u66f4\u4f18\u3002<\/p>\n\n\n\n<p>CompletableFuture \u5b9e\u73b0\u4e86 Future \u548c CompletionStage \u63a5\u53e3\uff0c \u5e76\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u7684\u65b9\u6cd5\uff0c\u5982supplyAsync, runAsync\u4ee5\u53cathenApplyAsync\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u4f7f\u7528CompletableFuture\u6765\u5b9e\u73b0\u4e0a\u9762\u7684\u4f8b\u5b50\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=\"\">CompletableFuture&lt;Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));\nwhile (!completableFuture.isDone()) {\n    System.out.println(\"CompletableFuture is not finished yet...\");\n}\nlong result = completableFuture.get();\n<\/pre>\n\n\n\n<p>\u6211\u4eec\u4e0d\u9700\u8981\u663e\u5f0f\u4f7f\u7528 ExecutorService\uff0cCompletableFuture \u5185\u90e8\u4f7f\u7528\u4e86 ForkJoinPool \u6765\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u7684\u4ee3\u7801\u53d8\u7684\u66f4\u7b80\u6d01\u3002<\/p>\n\n\n\n<p><strong>\u56db\u3001SpringBoot @Async\u5f02\u6b65<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u8003\u8651\u5f02\u6b65\u5904\u7406\uff0cSpring\u4e3a\u6211\u4eec\u5b9e\u73b0\u4e86\u5341\u5206\u4fbf\u5229\u7684\u652f\u6301\uff0c\u4f7f\u7528@EnableAsync\u5373\u53ef\u4f7f\u7528\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u529f\u80fd\uff0c\u4f7f\u7528@Async\u5373\u53ef\u5f00\u542f\u4e00\u4e2a\u7ebf\u7a0b\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>Spring\u4f1a\u641c\u7d22\u4e0a\u4e0b\u6587\u4e2d\u552f\u4e00\u7684TaskExecutor\u5b9e\u4f8b\uff0c\u6216\u4e00\u4e2a\u540d\u4e3ataskExecutor\u7684java.util.concurrent.Executor \u5b9e\u4f8b\uff1b\u5982\u679c\u6ca1\u6709\u627e\u5230\u4ee5\u4e0a\u5b9e\u4f8b\uff0c\u5219\u4f1a\u4f7f\u7528 SimpleAsyncTaskExecutor \u5904\u7406\u5f02\u6b65\u65b9\u6cd5\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u5728@Async\u6ce8\u89e3\u4e4b\u524d\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u9700\u8981\u4f7f\u7528JDK\u7684\u539f\u751f\u65b9\u6cd5\uff0c\u975e\u5e38\u9ebb\u70e6\uff0c\u5f53\u6709\u4e86@Async\u4e4b\u540e\u5c31\u6bd4\u8f83\u7b80\u5355\u4e86\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u4f7f\u7528 @EnableAsync \u542f\u7528\u5f02\u6b65\u6ce8\u89e3:<\/p>\n\n\n\n<p>\u5176\u4e2d@EnableAsync(proxyTargetClass=true)\u7528\u4e8e\u5f00\u542f\u5bf9\u5f02\u6b65\u4efb\u52a1\u7684\u652f\u6301\uff0cproxyTargetClass\u5c5e\u6027\u503c\u51b3\u5b9a\u662f\u57fa\u4e8e\u63a5\u53e3\u7684\u8fd8\u662f\u57fa\u4e8e\u7c7b\u7684\u4ee3\u7406\u88ab\u521b\u5efa\uff0c\u5982\u679c\u662ftrue\uff0c\u90a3\u4e48\u57fa\u4e8e\u7c7b\u7684\u4ee3\u7406\u5c06\u8d77\u4f5c\u7528\uff08\u8fd9\u65f6\u9700\u8981cglib\u5e93\uff09\uff0c\u5982\u679c\u662ffasle\uff0c\u90a3\u4e48\u6807\u51c6\u7684JDK \u57fa\u4e8e\u63a5\u53e3\u7684\u4ee3\u7406\uff0c\u8fd9\u4e2a\u503c\u9ed8\u8ba4\u4e3afalse\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=\"\">@SpringBootApplication\n@EnableAsync\npublic class StartApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(StartApplication.class, args);\n    }\n}\n<\/pre>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\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=\"\">import java.util.concurrent.Executor;\nimport java.util.concurrent.ThreadPoolExecutor;\n\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;\n\n@Configuration\npublic class ThreadPoolConfig {\n\n    @Bean(name = \"defaultThreadPoolExecutor\", destroyMethod = \"shutdown\")\n    public Executor asyncTaskExecutor() {\n        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\n        \/\/1: \u6838\u5fc3\u7ebf\u7a0b\u6570\u76ee\n        executor.setCorePoolSize(4);\n        \/\/2: \u6307\u5b9a\u6700\u5927\u7ebf\u7a0b\u6570,\u53ea\u6709\u5728\u7f13\u51b2\u961f\u5217\u6ee1\u4e86\u4e4b\u540e\u624d\u4f1a\u7533\u8bf7\u8d85\u8fc7\u6838\u5fc3\u7ebf\u7a0b\u6570\u7684\u7ebf\u7a0b\n        executor.setMaxPoolSize(10);\n        \/\/3: \u961f\u5217\u4e2d\u6700\u5927\u7684\u6570\u76ee\n        executor.setQueueCapacity(200);\n        \/\/4: \u7ebf\u7a0b\u540d\u79f0\u524d\u7f00\n        executor.setThreadNamePrefix(\"LocustTask-\");\n        \/\/5:\u5f53pool\u5df2\u7ecf\u8fbe\u5230max size\u7684\u65f6\u5019\uff0c\u5982\u4f55\u5904\u7406\u65b0\u4efb\u52a1\n        \/\/ CallerRunsPolicy: \u4f1a\u5728execute \u65b9\u6cd5\u7684\u8c03\u7528\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u88ab\u62d2\u7edd\u7684\u4efb\u52a1,\u5982\u679c\u6267\u884c\u7a0b\u5e8f\u5df2\u5173\u95ed\uff0c\u5219\u4f1a\u4e22\u5f03\u8be5\u4efb\u52a1\n        \/\/ AbortPolicy: \u629b\u51fajava.util.concurrent.RejectedExecutionException\u5f02\u5e38\n        \/\/ DiscardOldestPolicy: \u629b\u5f03\u65e7\u7684\u4efb\u52a1\n        \/\/ DiscardPolicy: \u629b\u5f03\u5f53\u524d\u7684\u4efb\u52a1\n        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());\n        \/\/6: \u7ebf\u7a0b\u7a7a\u95f2\u540e\u7684\u6700\u5927\u5b58\u6d3b\u65f6\u95f4(\u9ed8\u8ba4\u503c 60),\u5f53\u8d85\u8fc7\u4e86\u6838\u5fc3\u7ebf\u7a0b\u51fa\u4e4b\u5916\u7684\u7ebf\u7a0b\u5728\u7a7a\u95f2\u65f6\u95f4\u5230\u8fbe\u4e4b\u540e\u4f1a\u88ab\u9500\u6bc1\n        executor.setKeepAliveSeconds(60);\n        \/\/7:\u7ebf\u7a0b\u7a7a\u95f2\u65f6\u95f4,\u5f53\u7ebf\u7a0b\u7a7a\u95f2\u65f6\u95f4\u8fbe\u5230keepAliveSeconds(\u79d2)\u65f6,\u7ebf\u7a0b\u4f1a\u9000\u51fa,\u76f4\u5230\u7ebf\u7a0b\u6570\u91cf\u7b49\u4e8ecorePoolSize,\u5982\u679callowCoreThreadTimeout=true,\u5219\u4f1a\u76f4\u5230\u7ebf\u7a0b\u6570\u91cf\u7b49\u4e8e0\n        executor.setAllowCoreThreadTimeOut(false);\n        executor.initialize();\n        return executor;\n    }\n\n}<\/pre>\n\n\n\n<p>\u5728\u5f02\u6b65\u5904\u7406\u7684\u65b9\u6cd5\u4e0a\u6dfb\u52a0\u6ce8\u89e3\u00a0<code>@Async<\/code>\u00a0\uff0c\u5f53\u5bf9\u00a0<code>execute \u65b9\u6cd5<\/code>\u00a0\u8c03\u7528\u65f6\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u7684\u7ebf\u7a0b\u6c60\u00a0<code>defaultThreadPoolExecutor<\/code>\u00a0\u5f02\u6b65\u5316\u6267\u884c \u00a0<code>execute \u65b9\u6cd5<\/code><\/p>\n\n\n\n<p><code>@Async<\/code>\u8868\u793a\u58f0\u660e\u4e00\u4e2a\u6216\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u53ef\u4ee5\u5728\u7c7b\u6216\u65b9\u6cd5\u4e0a\u4f7f\u7528\uff0c\u5728\u7c7b\u4e0a\u4f7f\u7528\u65f6\u8868\u793a\u6574\u4e2a\u7c7b\u7684\u65b9\u6cd5\u90fd\u4f1a\u4f7f\u7528\u8be5<a href=\"https:\/\/so.csdn.net\/so\/search?q=%E7%BA%BF%E7%A8%8B%E6%B1%A0&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">\u7ebf\u7a0b\u6c60<\/a>\u64cd\u4f5c\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=\"\">@Service\npublic class AsyncServiceImpl implements AsyncService {\n \n    @Async(\"defaultThreadPoolExecutor\")\n    public Boolean execute(Integer num) {\n        System.out.println(\"\u7ebf\u7a0b\uff1a\" + Thread.currentThread().getName() + \" , \u4efb\u52a1\uff1a\" + num);\n        return true;\n    }\n \n}<\/pre>\n\n\n\n<p>\u7528 @Async \u6ce8\u89e3\u6807\u8bb0\u7684\u65b9\u6cd5\uff0c\u79f0\u4e3a\u5f02\u6b65\u65b9\u6cd5\u3002\u5728spring boot\u5e94\u7528\u4e2d\u4f7f\u7528 @Async \u5f88\u7b80\u5355\uff1a<\/p>\n\n\n\n<ul><li>\u8c03\u7528\u5f02\u6b65\u65b9\u6cd5\u7c7b\u4e0a\u6216\u8005\u542f\u52a8\u7c7b\u52a0\u4e0a\u6ce8\u89e3 @EnableAsync<\/li><\/ul>\n\n\n\n<ul><li>\u5728\u9700\u8981\u88ab\u5f02\u6b65\u8c03\u7528\u7684\u65b9\u6cd5\u5916\u52a0\u4e0a @Async<\/li><\/ul>\n\n\n\n<ul><li>\u6240\u4f7f\u7528\u7684 @Async \u6ce8\u89e3\u65b9\u6cd5\u7684\u7c7b\u5bf9\u8c61\u5e94\u8be5\u662fSpring\u5bb9\u5668\u7ba1\u7406\u7684bean\u5bf9\u8c61\uff1b<\/li><\/ul>\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=\"\">\u5982\u4e0b\u65b9\u5f0f\u4f1a\u4f7f@Async\u5931\u6548\n\u4e00\u3001\u5f02\u6b65\u65b9\u6cd5\u4f7f\u7528static\u4fee\u9970\n\u4e8c\u3001\u5f02\u6b65\u7c7b\u6ca1\u6709\u4f7f\u7528@Component\u6ce8\u89e3\uff08\u6216\u5176\u4ed6\u6ce8\u89e3\uff09\u5bfc\u81f4spring\u65e0\u6cd5\u626b\u63cf\u5230\u5f02\u6b65\u7c7b\n\u4e09\u3001\u5f02\u6b65\u65b9\u6cd5\u4e0d\u80fd\u4e0e\u5f02\u6b65\u65b9\u6cd5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\n\u56db\u3001\u7c7b\u4e2d\u9700\u8981\u4f7f\u7528@Autowired\u6216@Resource\u7b49\u6ce8\u89e3\u81ea\u52a8\u6ce8\u5165\uff0c\u4e0d\u80fd\u81ea\u5df1\u624b\u52a8new\u5bf9\u8c61\n\u4e94\u3001\u5982\u679c\u4f7f\u7528SpringBoot\u6846\u67b6\u5fc5\u987b\u5728\u542f\u52a8\u7c7b\u4e2d\u589e\u52a0@EnableAsync\u6ce8\u89e3\n\u516d\u3001\u5728Async \u65b9\u6cd5\u4e0a\u6807\u6ce8@Transactional\u662f\u6ca1\u7528\u7684\u3002 \u5728Async \u65b9\u6cd5\u8c03\u7528\u7684\u65b9\u6cd5\u4e0a\u6807\u6ce8@Transactional \u6709\u6548\u3002\n\u4e03\u3001\u8c03\u7528\u88ab@Async\u6807\u8bb0\u7684\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u4e0d\u80fd\u548c\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5728\u540c\u4e00\u7c7b\u4e2d\u4e0d\u7136\u4e0d\u4f1a\u8d77\u4f5c\u7528\uff01\uff01\uff01\uff01\uff01\uff01\uff01\n\u516b\u3001\u4f7f\u7528@Async\u65f6\u8981\u6c42\u662f\u4e0d\u80fd\u6709\u8fd4\u56de\u503c\u7684\u4e0d\u7136\u4f1a\u62a5\u9519\u7684 \u56e0\u4e3a\u5f02\u6b65\u8981\u6c42\u662f\u4e0d\u5173\u5fc3\u7ed3\u679c\u7684<\/pre>\n\n\n\n<p><strong>\u4e94\u3001Guava\u5f02\u6b65<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Guava \u63d0\u4f9b\u4e86 ListenableFuture \u7c7b\u6765\u6267\u884c\u5f02\u6b65\u64cd\u4f5c<\/p>\n\n\n\n<p>1.\u9996\u5148\u6211\u4eec\u9700\u8981\u6dfb\u52a0 guava \u7684maven\u4f9d\u8d56\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=\"\">&lt;dependency>\n    &lt;groupId>com.google.guava&lt;\/groupId>\n    &lt;artifactId>guava&lt;\/artifactId>\n    &lt;version>28.2-jre&lt;\/version>\n&lt;\/dependency>\n<\/pre>\n\n\n\n<p>2.\u73b0\u5728\u6211\u4eec\u4f7f\u7528ListenableFuture\u6765\u5b9e\u73b0\u6211\u4eec\u4e4b\u524d\u7684\u4f8b\u5b50:<\/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=\"\">ExecutorService threadpool = Executors.newCachedThreadPool();\nListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);\nListenableFuture&lt;Long> guavaFuture = (ListenableFuture&lt;Long>) service.submit(()-> factorial(number));\nlong result = guavaFuture.get();\n<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u4f7f\u7528MoreExecutors\u83b7\u53d6ListeningExecutorService\u7c7b\u7684\u5b9e\u4f8b\uff0c\u7136\u540eListeningExecutorService.submit\u6267\u884c\u5f02\u6b65\u4efb\u52a1\uff0c\u5e76\u8fd4\u56de ListenableFuture\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p><strong>Java\u5f02\u6b65\u7f16\u7a0b\u5c0f\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5f02\u6b65\u7f16\u7a0b\u53d7\u5230\u4e86\u8d8a\u6765\u8d8a\u591a\u7684\u5173\u6ce8\uff0c\u5c24\u5176\u662f\u5728 IO \u5bc6\u96c6\u578b\u7684\u4e1a\u52a1\u573a\u666f\u4e2d\uff0c\u76f8\u6bd4\u4f20\u7edf\u7684\u540c\u6b65\u5f00\u53d1\u6a21\u5f0f\uff0c\u5f02\u6b65\u7f16\u7a0b\u7684\u4f18\u52bf\u8d8a\u6765\u8d8a\u660e\u663e\uff0c\u5e0c\u671b\u4ee5\u4e0a\u4ecb\u7ecd\u76845\u79cdJava\u5f02\u6b65\u7f16\u7a0b\u65b9\u5f0f\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u6b65\u64cd\u4f5c\u5982\u679c\u9047\u5230\u4e00\u4e2a\u8017\u65f6\u7684\u65b9\u6cd5\uff0c\u9700\u8981\u963b\u585e\u7b49\u5f85\uff0c\u90a3\u4e48\u6211\u4eec\u6709\u6ca1\u6709\u529e\u6cd5\u89e3\u51b3\u5462\uff1f\u8ba9\u5b83\u5f02\u6b65\u6267\u884c\uff0c\u4e0b\u9762\u6211\u4f1a\u8be6\u89e3\u5f02\u6b65\u53ca\u5b9e\u73b0  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7437"}],"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=7437"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7437\/revisions"}],"predecessor-version":[{"id":7440,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7437\/revisions\/7440"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}