`
mmdev
  • 浏览: 13254827 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

ExecutorService

 
阅读更多
ExecutorService<wbr style="line-height:25px"><br style="line-height:25px"> java.util.concurrent<br style="line-height:25px"> 接口ExecutorService<br style="line-height:25px"> 所有超级接口:<br style="line-height:25px"> Executor<br style="line-height:25px"> 所有已知子接口:<br style="line-height:25px"> ScheduledExecutorService<br style="line-height:25px"> 所有已知实现类:<br style="line-height:25px"> AbstractExecutorService,<wbr style="line-height:25px"><span style="color:#99cc00; line-height:25px">ScheduledThreadPoolExecutor</span><span style="line-height:25px">,</span><span style="color:#808000; line-height:25px">ThreadPoolExecutor</span><wbr style="line-height:25px"><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">ExecutorService</span><span style="color:#003366; line-height:25px">提供了管理终止的方法,以及可为跟踪一个或多个异步任务执行状况而生成</span><span style="color:#ff6600; line-height:25px">Future</span><span style="color:#003366; line-height:25px">的方法。<br style="line-height:25px"> 可以关闭</span><span style="color:#ff00ff; line-height:25px">ExecutorService</span><span style="color:#003366; line-height:25px">,这将导致其拒绝新任务</span><wbr style="line-height:25px">。提供两个方法来关闭ExecutorService。<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">shutdown()</span><span style="color:#003366; line-height:25px">方法在终止前允许执行以前提交的任务,而</span><span style="color:#993300; line-height:25px">shutdownNow()</span><span style="color:#003366; line-height:25px">方法阻止等待任务的启动并试图停止当前正在执行的任务</span><wbr style="line-height:25px">。在终止后,执行程序没有任务在执行,也没有任务在等待执行,并且无法提交新任务。<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">应该关闭未使用的</span><span style="color:#ff00ff; line-height:25px">ExecutorService</span><span style="color:#000080; line-height:25px">以允许回收其资源<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 通过创建并返回一个可用于取消执行和/或等待完成的<span style="color:#ff6600; line-height:25px">Future</span>,方法submit扩展了基本方法<span style="color:#0000ff; line-height:25px">Executor.execute(java.lang.Runnable)</span>。<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">方法</span><span style="color:#ff6600; line-height:25px">invokeAny</span><span style="color:#000080; line-height:25px">和</span><span style="color:#ff6600; line-height:25px">invokeAll</span><span style="color:#000080; line-height:25px">是批量执行的最常用形式<wbr style="line-height:25px">,</wbr></span>它们执行任务collection,然后等待<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">至少一个,<br style="line-height:25px"> 或全部任务完成</span><wbr style="line-height:25px">(可使用ExecutorCompletionService类来编写这些方法的自定义变体)。<br style="line-height:25px"> Executors类为创建ExecutorService提供了便捷的工厂方法。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:它只有一个直接实现类<wbr style="line-height:25px"><span style="color:#99cc00; line-height:25px">ThreadPoolExecutor</span><wbr style="line-height:25px">和间接实现类<wbr style="line-height:25px"><span style="color:#808000; line-height:25px">ScheduledThreadPoolExecutor</span><wbr style="line-height:25px">。<br style="line-height:25px"> 关于<span style="color:#99cc00; line-height:25px">ThreadPoolExecutor</span>的更多内容请参考<wbr style="line-height:25px">《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792010964339151/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ThreadPoolExecutor</a></strong>》<wbr style="line-height:25px"><br style="line-height:25px"> 关于<span style="color:#808000; line-height:25px">ScheduledThreadPoolExecutor</span>的更多内容请参考<wbr style="line-height:25px">《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920109643632988/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ScheduledThreadPoolExecutor</a></strong>》<wbr style="line-height:25px"><br style="line-height:25px"> 用法示例<br style="line-height:25px"> 下面给出了一个网络服务的简单结构,这里线程池中的线程作为传入的请求。它使用了预先配置的<span style="color:#0000ff; line-height:25px">Executors.newFixedThreadPool(int)</span>工厂方法:<br style="line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <pre class="prettyprint" style="line-height:inherit; padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); color:rgb(0,112,0); font-family:monospace; background-color:rgb(250,250,250); margin-top:0px; margin-bottom:1em; margin-left:1em; overflow-x:auto; overflow-y:auto"><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">class</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">NetworkService</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">implements</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Runnable</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">private</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">final</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">ServerSocket</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> serverSocket</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">;</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">private</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">final</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">ExecutorService</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">;</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">public</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">NetworkService</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">int</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> port</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">,</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">int</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> poolSize</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">)</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">throws</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">IOException</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> serverSocket </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">new</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">ServerSocket</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">port</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Executors</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">newFixedThreadPool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">poolSize</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">public</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">void</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> run</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">()</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// run the service</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">try</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">for</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(;;)</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">execute</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">new</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Handler</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">serverSocket</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">accept</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">()));</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">catch</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">IOException</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> ex</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">)</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">shutdown</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">class</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Handler</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">implements</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Runnable</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">private</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">final</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Socket</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> socket</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">;</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Handler</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Socket</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> socket</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">)</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">this</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">socket </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> socket</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">;</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">public</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">void</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> run</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">()</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// read and service request on socket</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span></pre> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>下列方法分两个阶段关闭ExecutorService。第一阶段<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">调用<wbr style="line-height:25px"><wbr style="line-height:25px">shutdown拒绝传入任务</wbr></wbr></span><wbr style="line-height:25px">,然后等60秒后,任务还没执行完成,就调用shutdownNow(如有必要)取消所有遗留的任务<wbr style="line-height:25px">: <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <pre class="prettyprint" style="line-height:inherit; padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); color:rgb(0,112,0); font-family:monospace; background-color:rgb(250,250,250); margin-top:0px; margin-bottom:1em; margin-left:1em; overflow-x:auto; overflow-y:auto"><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">void</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> shutdownAndAwaitTermination</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">ExecutorService</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">)</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">shutdown</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// Disable new tasks from being submitted</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">try</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// Wait a while for existing tasks to terminate</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">if</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(!</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">awaitTermination</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,102,102)"><span class="lit" style="line-height:23px; color:rgb(0,102,102)">60</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">,</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">TimeUnit</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">SECONDS</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">))</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">shutdownNow</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// Cancel currently executing tasks</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// Wait a while for tasks to respond to being cancelled</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">if</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(!</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">awaitTermination</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,102,102)"><span class="lit" style="line-height:23px; color:rgb(0,102,102)">60</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">,</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">TimeUnit</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">SECONDS</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">))</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">System</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">err</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">println</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(0,136,0)"><span class="str" style="line-height:23px; color:rgb(0,136,0)">"Pool did not terminate"</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">);</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(0,0,136)"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">catch</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">InterruptedException</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> ie</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">)</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">{</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// (Re-)Cancel if current thread also interrupted</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> pool</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">shutdownNow</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(136,0,0)"><span class="com" style="line-height:23px; color:rgb(136,0,0)">// Preserve interrupt status</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Thread</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">currentThread</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">().</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">interrupt</span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">();</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span><span style="line-height:23px; color:rgb(102,102,0)"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">}</span></span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"></span></span></pre> 内存一致性效果:线程中向ExecutorService提交Runnable或Callable任务之前的操作happen-before由该任务所提取的所有操作,<br style="line-height:25px"> 后者依次happen-before通过Future.get()获取的结果。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">主要函数</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">void</span><span style="color:#ff6600; line-height:25px">shutdown()</span><wbr style="line-height:25px"><br style="line-height:25px"> 启动一个关闭命令,不再接受新任务,当所有已提交任务执行完后,就关闭。如果已经关闭,则调用没有其他作用。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> SecurityException-如果安全管理器存在并且关闭,此ExecutorService可能操作某些不允许调用者修改的线程(因为它没有保持RuntimePermission("modifyThread")),或者安全管理器的checkAccess方法拒绝访问。<br style="line-height:25px"><wbr style="line-height:25px">List&lt;Runnable&gt;<span style="color:#ff6600; line-height:25px">shutdownNow()</span><wbr style="line-height:25px"><br style="line-height:25px"> 试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表。<br style="line-height:25px"> 无法保证能够停止正在处理的活动执行任务,但是会尽力尝试。例如,通过Thread.interrupt()来取消典型的实现,所以任何任务无法响应中断都可能永远无法终止。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 从未开始执行的任务的列表<br style="line-height:25px"> 抛出:<br style="line-height:25px"> SecurityException-如果安全管理器存在并且关闭,<br style="line-height:25px"> 此ExecutorService可能操作某些不允许调用者修改的线程(因为它没有保持RuntimePermission("modifyThread")),<br style="line-height:25px"> 或者安全管理器的checkAccess方法拒绝访问。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:<span style="color:#000080; line-height:25px">它会返回等待执行的任务列表。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:<span style="color:#000080; line-height:25px">法保证能够停止正在处理的活动执行任务,但是会尽力尝试。例如,通过Thread.interrupt()来取消,<br style="line-height:25px"> 所以任何任务无法响应中断都可能永远无法终止。</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">boolean</span><span style="color:#ff6600; line-height:25px">isShutdown()</span><wbr style="line-height:25px"><br style="line-height:25px"> 如果此执行程序已关闭,则返回true。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 如果此执行程序已关闭,则返回true<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">boolean</span><span style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">isTerminated()</span><wbr style="line-height:25px"><br style="line-height:25px"> 如果关闭后所有任务都已完成,则返回true。注意,除非首先调用shutdown或shutdownNow,否则isTerminated永不为true。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 如果关闭后所有任务都已完成,则返回true<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">boolean</span><span style="color:#ff6600; line-height:25px">awaitTermination</span>(longtimeout,TimeUnitunit)throwsInterruptedException<wbr style="line-height:25px"><br style="line-height:25px"> 等待(阻塞)直到关闭或最长等待时间或发生中断<br style="line-height:25px"> 参数:<br style="line-height:25px"> timeout-最长等待时间<br style="line-height:25px"> unit-timeout参数的时间单位<br style="line-height:25px"> 返回:<br style="line-height:25px"> 如果此执行程序终止,则返回true;如果终止前超时期满,则返回false<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果等待时发生中断<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span>:如果此执行程序终止(关闭),则返回true;如果终止前超时期满,则返回false<wbr style="line-height:25px"><br style="line-height:25px"> &lt;T&gt;Future&lt;T&gt;<wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">submit</span><wbr style="line-height:25px">(Callable&lt;T&gt;task)<br style="line-height:25px"> 提交一个返回值的任务用于执行,返回一个表示任务的未决结果的Future。该Future的get方法在成功完成时将会返回该任务的结果。<br style="line-height:25px"> 如果想立即阻塞任务的等待,则可以使用result=exec.submit(aCallable).get();形式的构造。<br style="line-height:25px"> 注:Executors类包括了一组方法,可以转换某些其他常见的类似于闭包的对象,<br style="line-height:25px"> 例如,将PrivilegedAction转换为Callable形式,这样就可以提交它们了。<br style="line-height:25px"> 参数:<br style="line-height:25px"> task-要提交的任务<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示任务等待完成的Future<br style="line-height:25px"> 抛出:<br style="line-height:25px"> RejectedExecutionException-如果任务无法安排执行<br style="line-height:25px"> NullPointerException-如果该任务为null<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:关于submit的使用和Callable可以参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201083043930871/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">使用Callable返回结果</a></strong>》</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="line-height:25px">&lt;T&gt;Future&lt;T&gt;submit(Runnabletask,Tresult)<br style="line-height:25px"> 提交一个Runnable任务用于执行,并返回一个表示该任务的Future。该Future的get方法在成功完成时将会返回给定的结果。<br style="line-height:25px"> 参数:<br style="line-height:25px"> task-要提交的任务<br style="line-height:25px"> result-返回的结果<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示任务等待完成的Future<br style="line-height:25px"> 抛出:<br style="line-height:25px"> RejectedExecutionException-如果任务无法安排执行<br style="line-height:25px"> NullPointerException-如果该任务为null<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:关于submit的使用可以参阅《Callable》<br style="line-height:25px"> Future&lt;?&gt;<wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">submit</span><wbr style="line-height:25px">(Runnabletask)<br style="line-height:25px"> 提交一个Runnable任务用于执行,并返回一个表示该任务的Future。该Future的get方法在成功完成时将会返回null。<br style="line-height:25px"> 参数:<br style="line-height:25px"> task-要提交的任务<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示任务等待完成的Future<br style="line-height:25px"> 抛出:<br style="line-height:25px"> RejectedExecutionException-如果任务无法安排执行<br style="line-height:25px"> NullPointerException-如果该任务为null<br style="line-height:25px"> 注意:关于submit的使用可以参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201083043930871/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">使用Callable返回结果</a></strong>》<br style="line-height:25px"> &lt;T&gt;List&lt;Future&lt;T&gt;&gt;<wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">invokeAll</span><wbr style="line-height:25px">(Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks)throwsInterruptedException<br style="line-height:25px"> 执行给定的任务,当所有任务完成时,返回保持任务状态和结果的Future列表。返回列表的所有元素的Future.isDone()为true。<br style="line-height:25px"> 注意,可以正常地或通过抛出异常来终止已完成任务。如果正在进行此操作时修改了给定的collection,则此方法的结果是不确定的。<br style="line-height:25px"> 参数:<br style="line-height:25px"> tasks-任务collection<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示任务的Future列表,列表顺序与给定任务列表的迭代器所生成的顺序相同,每个任务都已完成。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果等待时发生中断,在这种情况下取消尚未完成的任务。<br style="line-height:25px"> NullPointerException-如果任务或其任意元素为null<br style="line-height:25px"> RejectedExecutionException-如果所有任务都无法安排执行<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:该方法会一直阻塞直到所有任务完成。<br style="line-height:25px"> &lt;T&gt;List&lt;Future&lt;T&gt;&gt;<wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">invokeAll</span><wbr style="line-height:25px">(Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks,<br style="line-height:25px"> longtimeout,<br style="line-height:25px"> TimeUnitunit)<br style="line-height:25px"> throwsInterruptedException<br style="line-height:25px"> 执行给定的任务,当所有任务完成或超时期满时(无论哪个首先发生),返回保持任务状态和结果的Future列表。返回列表的所有元素的Future.isDone()为true。一旦返回后,即取消尚未完成的任务。注意,可以正常地或通过抛出异常来终止已完成任务。如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。<br style="line-height:25px"> 参数:<br style="line-height:25px"> tasks-任务collection<br style="line-height:25px"> timeout-最长等待时间<br style="line-height:25px"> unit-timeout参数的时间单位<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示任务的Future列表,列表顺序与给定任务列表的迭代器所生成的顺序相同。<br style="line-height:25px"> 如果操作未超时,则已完成所有任务。如果确实超时了,则某些任务尚未完成。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果等待时发生中断,在这种情况下取消尚未完成的任务<br style="line-height:25px"> NullPointerException-如果任务或其任意元素或unit为null<br style="line-height:25px"> RejectedExecutionException-如果所有任务都无法安排执行<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:该方法会一直阻塞直到所有任务完成或超时。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:如果确实超时了,则某些任务尚未完成。【那么这些尚未完成的任务应该被系统取消】。<br style="line-height:25px"> &lt;T&gt;TinvokeAny(Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks)<br style="line-height:25px"> throwsInterruptedException,<br style="line-height:25px"> ExecutionException<br style="line-height:25px"> 执行给定的任务,如果某个任务已成功完成(也就是未抛出异常),则返回其结果。一旦正常或异常返回后,则取消尚未完成的任务。<br style="line-height:25px"> 如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。<br style="line-height:25px"> 参数:<br style="line-height:25px"> tasks-任务collection<br style="line-height:25px"> 返回:<br style="line-height:25px"> 某个任务返回的结果<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果等待时发生中断<br style="line-height:25px"> NullPointerException-如果任务或其任意元素为null<br style="line-height:25px"> IllegalArgumentException-如果任务为空<br style="line-height:25px"> ExecutionException-如果没有任务成功完成<br style="line-height:25px"> RejectedExecutionException-如果任务无法安排执行<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:该方法会一直阻塞直到有一个任务完成。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:一旦正常或异常返回后,<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">则取消尚未完成的任务</span><wbr style="line-height:25px"><br style="line-height:25px"> &lt;T&gt;T<wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">invokeAny<wbr style="line-height:25px">(</wbr></span>Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks,<br style="line-height:25px"> longtimeout,<br style="line-height:25px"> TimeUnitunit)<br style="line-height:25px"> throwsInterruptedException,<br style="line-height:25px"> ExecutionException,<br style="line-height:25px"> TimeoutException<br style="line-height:25px"><br style="line-height:25px"> 执行给定的任务,如果在给定的超时期满前某个任务已成功完成(也就是未抛出异常),则返回其结果。一旦正常或异常返回后,则取消尚未完成的任务。如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。<br style="line-height:25px"><br style="line-height:25px"> 参数:<br style="line-height:25px"> tasks-任务collection<br style="line-height:25px"> timeout-最长等待时间<br style="line-height:25px"> unit-timeout参数的时间单位<br style="line-height:25px"> 返回:<br style="line-height:25px"> 某个任务返回的结果<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果等待时发生中断<br style="line-height:25px"> NullPointerException-如果任务或其任意元素或unit为null<br style="line-height:25px"> TimeoutException-如果在所有任务成功完成之前给定的超时期满<br style="line-height:25px"> ExecutionException-如果没有任务成功完成<br style="line-height:25px"> RejectedExecutionException-如果任务无法安排执行<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:<span style="color:#000080; line-height:25px">该方法会一直阻塞直到有一个任务完成。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:一旦正常或异常返回后,<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">则取消尚未完成的任务</span><wbr style="line-height:25px"><br style="line-height:25px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr>
分享到:
评论

相关推荐

    在spring boot中使用java线程池ExecutorService的讲解

    在 Spring Boot 中使用 Java 线程池 ExecutorService 的讲解 Spring Boot 作为一个流行的 Java 框架,提供了许多便捷的功能来帮助开发者快速构建应用程序。其中之一就是使用 Java 线程池 ExecutorService 来管理...

    ExecutorService与CompletionService对比详解.docx

    ExecutorService 和 CompletionService 是Java并发处理中的两种工具,它们用于管理和执行多个任务。ExecutorService 是一个接口,它是java.util.concurrent.Executor 接口的扩展,提供了一组方法来管理和控制线程池...

    Executor,Executors,ExecutorService比较.docx

    【Executor、Executors和ExecutorService详解】 在Java并发编程中,`Executor`、`Executors`和`ExecutorService`是核心组件,它们帮助开发者高效管理线程资源,提高程序的并发性能。理解这三个概念的区别和用途是...

    ExecutorService的execute和submit方法

    在Java多线程编程中,`ExecutorService`是线程池的核心接口,它提供了一种管理线程的方式,包括创建线程、调度线程执行以及控制线程的生命周期。`ExecutorService`通过`execute()`和`submit()`这两个方法来提交任务...

    ExecutorService.shutdown()应该是在线程执行完毕后,才会去关闭

    在Java多线程编程中,`ExecutorService`是线程池的一个重要接口,它提供了管理和控制线程执行的能力。当我们创建一个`ExecutorService`实例并提交任务时,我们可以通过调用`shutdown()`方法来关闭线程池。然而,标题...

    ExecutorService方法案例文件.zip

    ExecutorService方法案例文件.zip

    ExecutorService用法详解.doc

    接口 java.util.concurrent.ExecutorService 表述了异步执行的机制,并且可以让任务在后台执行。壹個 ExecutorService 实例因此特别像壹個线程池。事实上,在 java.util.concurrent 包中的 ExecutorService 的实现...

    ExecutorService线程池

    ExecutorService线程池是Java并发编程中的核心组件,它位于`java.util.concurrent`包下,是`java.util.concurrent.Executor`接口的一个实现。ExecutorService提供了一种管理线程的方式,允许我们创建、管理和控制...

    运用JAVA的concurrent.ExecutorService线程池实现socket的TCP和UDP连接JAVA语言

    运用JAVA的concurrent.ExecutorService线程池实现socket的TCP和UDP连接

    2_ExecutorService源码阅读1

    ExecutorService 是 Java 中用于管理和控制线程执行的核心接口,它是 java.util.concurrent 包的一部分。ExecutorService 扩展了 Executor 接口,提供了更丰富的功能,如任务的提交、关闭服务、检查服务状态等。这个...

    java并发编程:Executor、Executors、ExecutorService.docx

    Java并发编程中的Executor、Executors和ExecutorService是Java并发编程框架的重要组成部分,它们为开发者提供了高效管理和控制线程执行的工具。以下是对这些概念的详细解释: 1. Executor: Executor是一个接口,它...

    运用JAVA的concurrent.ExecutorService线程池实现socket的TCP和UDP连接.doc

    根据提供的文档标题、描述以及部分内容,我们可以总结出以下关于如何运用Java中的`concurrent.ExecutorService`线程池实现socket的TCP和UDP连接的关键知识点: ### 1. 理解Java中的`concurrent.ExecutorService` `...

    2011.08.30(2)——— java BlockingQueue ExecutorService

    标题 "2011.08.30(2)——— java BlockingQueue ExecutorService" 涉及到Java并发编程中的两个核心组件:BlockingQueue(阻塞队列)和ExecutorService。这篇博客可能深入探讨了如何使用这两个工具来优化多线程环境...

    详解Java利用ExecutorService实现同步执行大量线程

    Java中的ExecutorService是Java并发编程的重要组成部分,它提供了一种高效、灵活的方式来管理和控制线程的执行。在处理大量线程或并发操作时,ExecutorService能够确保系统的稳定性和资源的有效利用,避免线程间的不...

    java ExecutorService使用方法详解

    Java中的`ExecutorService`是Java并发编程的重要组成部分,它提供了线程池的管理,使得开发者可以更有效地控制并发任务的执行。在Java的`java.util.concurrent`包中,`ExecutorService`接口作为线程池的核心接口,...

    java线程池工具--ExecutorService,简单例子

    NULL 博文链接:https://x125858805.iteye.com/blog/2191873

    Java使用ExecutorService来停止线程服务

    Java 使用 ExecutorService 来停止线程服务 Java 中的 ExecutorService 是一个非常强大的线程池管理工具,它提供了多种方式来停止线程服务。今天,我们将详细介绍如何使用 ExecutorService 来停止线程服务。 首先...

    ExecutorService10个要诀和技巧编程开发技术

    ExecutorService10个要诀和技巧编程开发技术共9页.pdf.zip

    详解JDK中ExecutorService与Callable和Future对线程的支持

    Java并发编程中的ExecutorService、Callable和Future Java并发编程中,ExecutorService、Callable和Future是三大核心组件,它们之间紧密相连,共同实现了高效、安全的并发编程。下面我们将详细介绍这些组件的作用和...

Global site tag (gtag.js) - Google Analytics