{"id":157264,"date":"2019-09-02T10:09:13","date_gmt":"2019-09-02T02:09:13","guid":{"rendered":"https:\/\/gulass.cn\/?p=157264"},"modified":"2019-08-27T10:10:14","modified_gmt":"2019-08-27T02:10:14","slug":"java-async","status":"publish","type":"post","link":"https:\/\/gulass.cn\/java-async.html","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u7528 Java \u5b9e\u73b0\u5f02\u6b65\u8c03\u7528"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6559\u7a0b\u6559\u4f60\u5982\u4f55\u4f7f\u7528Java\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u521b\u5efa\u7ebf\u7a0b<\/strong><\/div>\n
@Test\r\npublic void test0() throws Exception {\r\n\u00a0 System.out.println(\"main\u51fd\u6570\u5f00\u59cb\u6267\u884c\");\r\n\u00a0 Thread thread=new Thread(new Runnable() {\r\n\u00a0 \u00a0 @Override\r\n\u00a0 \u00a0 public void run() {\r\n\u00a0 \u00a0 \u00a0 System.out.println(\"===task start===\");\r\n\u00a0 \u00a0 \u00a0 try {\r\n\u00a0 \u00a0 \u00a0 \u00a0 Thread.sleep(5000);\r\n\u00a0 \u00a0 \u00a0 } catch (InterruptedException e) {\r\n\u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();\r\n\u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 System.out.println(\"===task finish===\");\r\n\u00a0 \u00a0 }\r\n\u00a0 });\r\n\r\n\u00a0 thread.start();\r\n\u00a0 System.out.println(\"main\u51fd\u6570\u6267\u884c\u7ed3\u675f\");\r\n\r\n}<\/pre>\n
\u4e8c\u3001Future<\/strong><\/div>\n

jdk8\u4e4b\u524d\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5728JUC\u4e0b\u589e\u52a0\u4e86Future\uff0c\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

@Test\r\npublic void test1() throws Exception {\r\n\r\n\u00a0 \u00a0 System.out.println(\"main\u51fd\u6570\u5f00\u59cb\u6267\u884c\");\r\n\r\n\u00a0 \u00a0 ExecutorService executor = Executors.newFixedThreadPool(1);\r\n\u00a0 \u00a0 Future<Integer> future = executor.submit(new Callable<Integer>() {\r\n\u00a0 \u00a0 \u00a0 \u00a0 @Override\r\n\u00a0 \u00a0 \u00a0 \u00a0 public Integer call() throws Exception {\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(\"===task start===\");\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Thread.sleep(5000);\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(\"===task finish===\");\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return 3;\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 });\r\n\u00a0 \u00a0 \/\/\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\r\n\u00a0 \u00a0 \/\/Integer result=future.get();\r\n\u00a0 \u00a0 System.out.println(\"main\u51fd\u6570\u6267\u884c\u7ed3\u675f\");\r\n\r\n\u00a0 \u00a0 System.in.read();\r\n\r\n}<\/pre>\n
\u4e09\u3001CompletableFuture<\/strong><\/div>\n

\u4f7f\u7528\u539f\u751f\u7684CompletableFuture\u5b9e\u73b0\u5f02\u6b65\u64cd\u4f5c\uff0c\u52a0\u4e0a\u5bf9lambda\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u8bf4\u5b9e\u73b0\u5f02\u6b65\u4efb\u52a1\u5df2\u7ecf\u53d1\u6325\u5230\u4e86\u6781\u81f4\u3002<\/p>\n

@Test\r\npublic void test2() throws Exception {\r\n\u00a0 \u00a0 System.out.println(\"main\u51fd\u6570\u5f00\u59cb\u6267\u884c\");\r\n\u00a0 \u00a0 ExecutorService executor = Executors.newFixedThreadPool(2);\r\n\u00a0 \u00a0 CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {\r\n\u00a0 \u00a0 \u00a0 \u00a0 @Override\r\n\u00a0 \u00a0 \u00a0 \u00a0 public Integer get() {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(\"===task start===\");\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 try {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Thread.sleep(5000);\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } catch (InterruptedException e) {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(\"===task finish===\");\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return 3;\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 }, executor);\r\n\u00a0 \u00a0 future.thenAccept(e -> System.out.println(e));\r\n\u00a0 \u00a0 System.out.println(\"main\u51fd\u6570\u6267\u884c\u7ed3\u675f\");\r\n}<\/pre>\n
\u56db\u3001Spring\u7684Async\u6ce8\u89e3<\/strong><\/div>\n

\u4f7f\u7528spring\u5b9e\u73b0\u5f02\u6b65\u9700\u8981\u5f00\u542f\u6ce8\u89e3\uff0c\u53ef\u4ee5\u4f7f\u7528xml\u65b9\u5f0f\u6216\u8005Java config\u7684\u65b9\u5f0f\u3002<\/p>\n

xml\u65b9\u5f0f\uff1a <\/strong><\/span><\/div>\n
<task:annotation-driven executor=\"executor\" \/>\r\n<task:executor id=\"executor\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 pool-size=\"2\" \u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\r\n\u00a0 \u00a0 \u00a0 \u00a0 queue-capacity=\"100\" \u6392\u961f\u961f\u5217\u957f\u5ea6 \r\n\u00a0 \u00a0 \u00a0 \u00a0 keep-alive=\"120\" \u7ebf\u7a0b\u4fdd\u6d3b\u65f6\u95f4\uff08\u5355\u4f4d\u79d2\uff09\r\n\u00a0 \u00a0 \u00a0 \u00a0 rejection-policy=\"CALLER_RUNS\" \u5bf9\u62d2\u7edd\u7684\u4efb\u52a1\u5904\u7406\u7b56\u7565 \/><\/pre>\n
java\u65b9\u5f0f\uff1a<\/strong><\/span><\/div>\n
@EnableAsync\r\npublic class MyConfig {\r\n\r\n    @Bean\r\n    public TaskExecutor executor(){\r\n        ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();\r\n        executor.setCorePoolSize(10); \/\/\u6838\u5fc3\u7ebf\u7a0b\u6570\r\n        executor.setMaxPoolSize(20);  \/\/\u6700\u5927\u7ebf\u7a0b\u6570\r\n        executor.setQueueCapacity(1000); \/\/\u961f\u5217\u5927\u5c0f\r\n        executor.setKeepAliveSeconds(300); \/\/\u7ebf\u7a0b\u6700\u5927\u7a7a\u95f2\u65f6\u95f4\r\n        executor.setThreadNamePrefix(\"fsx-Executor-\"); \/\/\u6307\u5b9a\u7528\u4e8e\u65b0\u521b\u5efa\u7684\u7ebf\u7a0b\u540d\u79f0\u7684\u524d\u7f00\u3002\r\n        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());\r\n        return executor;\r\n    }\r\n}<\/pre>\n
\uff081\uff09@Async<\/strong><\/span><\/div>\n
@Test\r\npublic void test3() throws Exception {\r\n    System.out.println(\"main\u51fd\u6570\u5f00\u59cb\u6267\u884c\");\r\n    myService.longtime();\r\n    System.out.println(\"main\u51fd\u6570\u6267\u884c\u7ed3\u675f\");\r\n}\r\n\r\n @Async\r\npublic void longtime() {\r\n    System.out.println(\"\u6211\u5728\u6267\u884c\u4e00\u9879\u8017\u65f6\u4efb\u52a1\");\r\n    try {\r\n        Thread.sleep(5000);\r\n    } catch (InterruptedException e) {\r\n        e.printStackTrace();\r\n    }\r\n    System.out.println(\"\u5b8c\u6210\");\r\n\r\n}<\/pre>\n
\uff082\uff09AsyncResult<\/strong><\/span><\/div>\n

\u5982\u679c\u9700\u8981\u8fd4\u56de\u503c\uff0c\u8017\u65f6\u65b9\u6cd5\u8fd4\u56de\u503c\u7528AsyncResult\u5305\u88c5\u3002<\/p>\n

@Test\r\npublic void test4() throws Exception {\r\n    System.out.println(\"main\u51fd\u6570\u5f00\u59cb\u6267\u884c\");\r\n    Future future=myService.longtime2();\r\n    System.out.println(\"main\u51fd\u6570\u6267\u884c\u7ed3\u675f\");\r\n    System.out.println(\"\u5f02\u6b65\u6267\u884c\u7ed3\u679c\uff1a\"+future.get());\r\n}\r\n\r\n @Async\r\npublic Future longtime2() {\r\n    System.out.println(\"\u6211\u5728\u6267\u884c\u4e00\u9879\u8017\u65f6\u4efb\u52a1\");\r\n\r\n    try {\r\n        Thread.sleep(8000);\r\n    } catch (InterruptedException e) {\r\n        e.printStackTrace();\r\n    }\r\n\r\n    System.out.println(\"\u5b8c\u6210\");\r\n    return new AsyncResult<>(3);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

@Test public void test0() throws Exception { \u00a0 System.o […]<\/p>\n","protected":false},"author":63,"featured_media":157266,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-157264","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/157264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=157264"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/157264\/revisions"}],"predecessor-version":[{"id":157301,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/157264\/revisions\/157301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/157266"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=157264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=157264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=157264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}