{"id":10225,"date":"2023-11-28T21:30:11","date_gmt":"2023-11-28T13:30:11","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10225"},"modified":"2023-11-28T21:30:15","modified_gmt":"2023-11-28T13:30:15","slug":"java-zhong-xian-cheng-chi-zhongsubmit-heexecute","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhong-xian-cheng-chi-zhongsubmit-heexecute\/","title":{"rendered":"Java\u4e2d\u7ebf\u7a0b\u6c60\u4e2dsubmit()\u548cexecute()\u65b9\u6cd5\u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p>\u5728\u4f7f\u7528java.util.concurrent\u4e0b\u5173\u4e8e\u7ebf\u7a0b\u6c60\u4e00\u4e9b\u7c7b\u7684\u65f6\u5019\uff0c\u76f8\u4fe1\u5f88\u591a\u4eba\u548c\u6211\u4e00\u6837\uff0c\u603b\u662f\u5206\u4e0d \u6e05submit()\u548cexecute()\u7684\u533a\u522b\uff0c\u4eca\u5929\u4ece\u6e90\u7801\u65b9\u9762\u5206\u6790\u603b\u7ed3\u4e00\u4e0b\u3002<br \/>\n\u901a\u5e38\uff0c\u6211\u4eec\u901a\u8fc7Executors\u8fd9\u4e2a\u5de5\u5177\u7c7b\u63d0\u4f9b\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u9002\u5408\u4e0d\u540c\u573a\u666f\u7684\u7ebf\u7a0b\u6c60\uff0c\u8fd9\u91cc\u5c31\u4e0d \u4e00\u4e00\u4ecb\u7ecd\u4e86\u3002<br \/>\n\u4f8b\u5982\uff0c\u521b\u5efa\u53ef\u7f13\u5b58\u7ebf\u7a0b\u6c60\uff0cExecutors.newCachedThreadPool(),\u6e90\u7801\u5982\u4e0b\uff1a<br \/>\npublic static ExecutorService newCachedThreadPool() {<br \/>\nreturn new ThreadPoolExecutor (0, Integer.MAX_VALUE,<br \/>\n60L, TimeUnit.SECONDS,<br \/>\nnew SynchronousQueue<Runnable>());<br \/>\n}<br \/>\n\u901a\u8fc7\u4e0a\u9762\u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2aExecutorService\u63a5\u53e3 ,\u800c\u8fd9\u4e2a\u63a5\u53e3\u7ee7\u627f<br \/>\nExecutor\u63a5\u53e3\uff0cExecutor\u662f\u6700\u4e0a\u5c42\u7684\uff0c\u5176\u4e2d\u53ea\u5305\u542b\u4e00\u4e2aexecute()<br \/>\n\u65b9\u6cd5\uff1a<br \/>\npublic interface Executor {<br \/>\nvoid execute(Runnable command);<br \/>\n}<br \/>\nexecute()\u65b9\u6cd5\u7684\u5165\u53c2\u4e3a\u4e00\u4e2aRunnable,\u8fd4\u56de\u503c\u4e3avoid,\u8fd9\u65f6\u5019\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86 execute()\u65b9 \u6cd5\u7684\u6765\u6e90\u4ee5\u53ca\u5176\u5b9a\u4e49\u3002<br \/>\n\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u770b\u770b\uff0csubmit() \u4ece\u54ea\u6765\u7684\u5462\uff1f<br \/>\n\u901a\u8fc7\u731c\u6d4b\uff0c\u5e94\u8be5\u662fExecutorService\u63a5\u53e3\u4e2d\u7684\uff0c\u679c\u7136\uff0c\u6253\u5f00\u6e90\u7801\uff0c\u770b\u5230\u4e86 submit()\u65b9\u6cd5\uff1a<br \/>\npublic interface ExecutorService extends Executor {<br \/>\n\u2026<br \/>\n<T> Future<T> submit(Callable<T> task):<br \/>\n<T> Future<T> submit(Runnable task, T result);<br \/>\nFuture&lt;?&gt; submit(Runnable task);<br \/>\n\u2026<br \/>\n\u53ef\u4ee5\u770b\u51fa\uff0c\u5728ExecutorService\u63a5\u53e3\u4e2d\uff0c\u4e00\u5171\u6709\u4ee5\u4e0a\u4e09\u4e2asumbit()\u65b9\u6cd5\uff0c\u5165\u53c2\u53ef\u4ee5\u4e3aCallable, \u4e5f\u53ef\u4ee5\u4e3aRunnable,\u800c\u4e14\u65b9\u6cd5\u6709\u8fd4\u56de\u503cFuture;<br \/>\n(\u8865\u5145\u8bf4\u660e\uff1aCallable\u4e0eRunnable\u7c7b\u4f3c\uff0c\u4e5f\u662f\u521b\u5efa\u7ebf\u7a0b\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5b9e\u73b0\u5176call()\u65b9\u6cd5\u5373\u53ef, \u65b9\u6cd5\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\uff0c\u800c\u4e14\u65b9\u6cd5\u4e0a\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38;)<br \/>\n\u603b\u7ed3\uff0c\u4ece\u4e0a\u9762\u7684\u6e90\u7801\u4ee5\u53ca\u8bb2\u89e3\u53ef\u4ee5\u603b\u7ed3execute()\u548csubmit()\u65b9\u6cd5\u7684\u533a\u522b\uff1a<br \/>\n\u76f8\u540c\u70b9\uff1a<br \/>\n\u76f8\u540c\u70b9\u5c31\u662f\u90fd\u53ef\u4ee5\u5f00\u542f\u7ebf\u7a0b\u6267\u884c\u6c60\u4e2d\u7684\u4efb\u52a1\u3002<br \/>\n\u4e0d\u540c\u70b9\uff1a<br \/>\n1\u3001\t\u63a5\u6536\u53c2\u6570\uff1aexecute()\u53ea\u80fd\u6267\u884cRunnable\u7c7b\u578b\u7684\u4efb\u52a1\uff0csubmit()\u53ef\u4ee5\u6267\u884cRunnable\u548c Callable\u7c7b\u578b\u7684\u4efb\u52a1\u3002<br \/>\n2\u3001\t\u8fd4\u56de\u503c\uff1asubmit()\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u6301\u6709\u8ba1\u7b97\u7ed3\u679c\u7684Future\u5bf9\u8c61\uff0c\u800cexecute0\u6ca1\u6709<br \/>\n3\u3001\t\u5f02\u5e38\u5904\u7406\uff1asubmit()\u65b9\u4fbfException\u5904\u7406<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528java.util.concurrent\u4e0b\u5173\u4e8e\u7ebf\u7a0b\u6c60\u4e00\u4e9b\u7c7b\u7684\u65f6\u5019\uff0c\u76f8\u4fe1\u5f88\u591a\u4eba\u548c\u6211\u4e00\u6837\uff0c\u603b\u662f\u5206\u4e0d \u6e05sub [&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-10225","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10225","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=10225"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10225\/revisions"}],"predecessor-version":[{"id":10226,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10225\/revisions\/10226"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10225"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}