{"id":9589,"date":"2023-11-28T21:06:39","date_gmt":"2023-11-28T13:06:39","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9589"},"modified":"2023-11-28T21:07:01","modified_gmt":"2023-11-28T13:07:01","slug":"java-zhong-executor-executorservice-he-executors-j","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhong-executor-executorservice-he-executors-j\/","title":{"rendered":"Java \u4e2d Executor, ExecutorService \u548c Executors \u95f4\u7684\u533a\u522b\u4e0e\u8054\u7cfb\uff1f"},"content":{"rendered":"<p>UML\u7b80\u8981\u7c7b\u56fe\u5173\u7cfb:<br \/>\n<img decoding=\"async\" src=\"https:\/\/wx.kaifamiao.info\/wp-content\/uploads\/2023\/11\/17011768166337.jpg\" alt=\"\" \/><\/p>\n<p>\u4e0b\u9762\u8be6\u7ec6\u770b\u4e00\u4e0b\u4e09\u8005\u7684\u533a\u522b:<br \/>\nExecutor vs ExecutorService vs Executors<\/p>\n<p>\u6b63\u5982\u4e0a\u9762\u6240\u8bf4\uff0c\u8fd9\u4e09\u8005\u5747\u662fExecutor\u6846\u67b6\u4e2d\u7684\u4e00\u90e8\u5206\u3002Java\u5f00\u53d1\u8005\u5f88\u6709\u5fc5\u8981\u5b66\u4e60\u548c\u7406\u89e3\u4ed6 \u4eec\uff0c\u4ee5\u4fbf\u66f4\u9ad8\u6548\u7684\u4f7f\u7528Java\u63d0\u4f9b\u7684\u4e0d\u540c\u7c7b\u578b\u7684\u7ebf\u7a0b\u6c60\u3002\u603b\u7ed3\u4e00\u4e0b\u8fd9\u4e09\u8005\u95f4\u7684\u533a\u522b\uff0c\u4ee5\u4fbf\u5927\u5bb6 \u66f4\u597d\u7684\u7406\u89e3\uff1a<br \/>\n1\u3001Executor \u548c ExecutorService \u8fd9\u4e24\u4e2a\u63a5\u53e3\u4e3b\u8981\u7684\u533a\u522b\u662f\uff1aExecutorService \u63a5\u53e3\u7ee7\u627f\u4e86 Executor \u63a5\u53e3\uff0c\u662f Executor \u7684\u5b50\u63a5\u53e3<\/p>\n<p>2\u3001\tExecutor \u548c ExecutorService \u7b2c\u4e8c\u4e2a\u533a\u522b\u662f\uff1aExecutor \u63a5\u53e3 \u5b9a\u4e49\u4e86 execute()\u65b9\u6cd5\u7528 \u6765\u63a5\u6536\u4e00\u4e2aRunnable\u63a5\u53e3\u7684\u5bf9\u8c61\uff0c\u800cExecutorService\u63a5\u53e3\u4e2d\u7684submit()\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7 Runnable \u548c Callable \u63a5\u53e3\u7684\u5bf9\u8c61\u3002<br \/>\n3\u3001\tExecutor\u548cExecutorService\u63a5\u53e3\u7b2c\u4e09\u4e2a\u533a\u522b\u662fExecutor\u4e2d\u7684execute()\u65b9\u6cd5\u4e0d\u8fd4\u56de \u4efb\u4f55\u7ed3\u679c\uff0c\u800cExecutorService\u4e2d\u7684submit()\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2aFuture\u5bf9\u8c61\u8fd4\u56de\u8fd0\u7b97\u7ed3\u679c\u3002<br \/>\n4\u3001\tExecutor\u548cExecutorService\u63a5\u53e3\u7b2c\u56db\u4e2a\u533a\u522b\u662f\u9664\u4e86\u5141\u8bb8\u5ba2\u6237\u7aef\u63d0\u4ea4\u4e00\u4e2a\u4efb\u52a1\uff0c ExecutorService\u8fd8\u63d0\u4f9b\u7528\u6765\u63a7\u5236\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5\u3002\u6bd4\u5982\uff1a\u8c03\u7528shutDown()\u65b9\u6cd5\u7ec8\u6b62\u7ebf\u7a0b\u6c60\u3002 \u53ef\u4ee5\u901a\u8fc7\u300aJava Concurrency in Practice\u300b\u4e00\u4e66\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5173\u95ed\u7ebf\u7a0b\u6c60\u548c\u5982\u4f55\u5904\u7406 pending\u7684\u4efb\u52a1\u7684\u77e5\u8bc6\u3002<br \/>\n5\u3001\tExecutors\u7c7b\u63d0\u4f9b\u5de5\u5382\u65b9\u6cd5\u7528\u6765\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u7ebf\u7a0b\u6c60\u3002\u6bd4<br \/>\n\u5982\uff1anewSingleThreadExecutor()\u521b\u5efa\u4e00\u4e2a\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\uff0cnewFixedThreadPool (int numOfThreads)\u6765\u521b\u5efa\u56fa\u5b9a\u7ebf\u7a0b\u6570\u7684\u7ebf\u7a0b\u6c60\uff0cnewCachedThreadPoo1()\u53ef\u4ee5\u6839\u636e\u9700\u8981\u521b\u5efa\u65b0\u7684\u7ebf \u7a0b\uff0c\u4f46\u5982\u679c\u5df2\u6709\u7ebf\u7a0b\u662f\u7a7a\u95f2\u7684\u4f1a\u91cd\u7528\u5df2\u6709\u7ebf\u7a0b\u3002<br \/>\n\u603b\u7ed3<br \/>\n\u4e0b\u8868\u5217\u51fa\u4e86 Executor \u548c ExecutorService \u7684\u533a\u522b\uff1a<br \/>\nExecutor\t                                                   ExecutorService<br \/>\nExecutor\u662fJava\u7ebf\u7a0b\u6c60\u7684\u6838\u5fc3\u63a5\u53e3\uff0c\u7528\u6765\u5e76ExecutorService\u662fExecutor\u63a5\u53e3\u7684\u6269\u5c55\uff0c \u63d0\u4f9b\u4e86\u5f02\u6b65\u6267\u884c\u548c\u5173\u95ed\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5<br \/>\n\u63d0\u4f9bsubmit()\u65b9\u6cd5\u7528\u6765\u63d0\u4ea4\u4efb\u52a1<br \/>\nsubmit()\u65b9\u6cd5\u8fd4\u56deFuture\u5bf9\u8c61\uff0c\u53ef\u7528\u6765\u83b7\u53d6\u4efb \u52a1\u6267\u884c\u7ed3\u679c<br \/>\n\u53ef\u4ee5\u901a\u8fc7Future.cancel()\u53d6\u6d88pending\u4e2d\u7684 \u4efb\u52a1<br \/>\n\u63d0\u4f9b\u4e86\u5173\u95ed\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UML\u7b80\u8981\u7c7b\u56fe\u5173\u7cfb: \u4e0b\u9762\u8be6\u7ec6\u770b\u4e00\u4e0b\u4e09\u8005\u7684\u533a\u522b: Executor vs ExecutorService vs [&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-9589","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9589","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=9589"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9589\/revisions"}],"predecessor-version":[{"id":9590,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9589\/revisions\/9590"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9589"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}