- 浏览: 13431005 次
- 性别:
- 来自: 大连
-
文章分类
最新评论
-
sanrenxing_1:
GoEasy 实时推送支持IE6-IE11及大多数主流浏览器的 ...
WindowsPhone消息推送服务 -
张砚辉:
两侧照片绕Y轴旋转后有锯齿,请问锯齿解决方案,很长时间没解决
自定义带倒影和偏转的超炫Gallery -
knight_black_bob:
能不能把你自己的博客整理下分类下,写了这么多 ,都不知道怎么查 ...
Android_View,ViewGroup,Window之间的关系 -
jeasonyoung:
你这个代码实现在iOS8下应该是滑不动的
UISlider 滑块控件—IOS开发 -
wx_hello:
如果能写个可运行的java程序,不胜感激。。。
rs232串口通信原理
Executor
Executor<wbr style="line-height:25px"><br style="line-height:25px">
java.util.concurrent<br style="line-height:25px">
接口Executor<br style="line-height:25px">
所有已知子接口:<br style="line-height:25px">
ExecutorService,ScheduledExecutorService<br style="line-height:25px">
所有已知实现类:<br style="line-height:25px">
AbstractExecutorService,<span style="line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="color:#ff9900; line-height:25px">ScheduledThreadPoolExecutor</span><span style="line-height:25px">,</span><span style="color:#ff9900; line-height:25px">ThreadPoolExecutor</span><wbr style="line-height:25px"><br style="line-height:25px">
它用于执行Runnable任务。此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节、调度等)分离开来的方法。<br style="line-height:25px">
通常使用Executor而不是显式地创建线程。<br style="line-height:25px">
例如,可能会使用以下方法,而不是为一组任务中的每个任务调用newThread(new(RunnableTask())).start():<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(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">Executor</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> executor </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><em><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">anExecutor</span></span></em><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)">executor</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)">RunnableTask1</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)">executor</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)">RunnableTask2</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 style="line-height:25px"><span style="color:#ff00ff; line-height:25px">Executo</span><span style="color:#003366; line-height:25px">r接口并没有严格地要求执行是异步的<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 因此执行程序可以在调用者的线程中立即运行已提交的任务:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></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)">DirectExecutor</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)">Executor</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)"> 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(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)"> r</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)"> r</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)">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)"><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></pre> 更常见的是,任务是在某个不是调用者线程的线程中执行的。以下执行程序将为每个任务生成一个新线程。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></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)">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)">ThreadPerTaskExecutor</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)">Executor</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)"> 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(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)"> r</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)">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)">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)">r</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)">start</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></pre> 许多Executor的实现都对调度任务的方式和时间强加了某种限制。以下执行程序使任务提交与第二个执行程序保持连续,这说明了一个复合执行程序。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例1</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></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><code style="line-height:1em; color:rgb(0,112,0); font-family:monospace"><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)">SerialExecutor</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)">Executor</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)">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)">Queue</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> tasks </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)">ArrayDeque</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)">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)">Executor</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> executor</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)">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)"> active</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(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">SerialExecutor</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)">Executor</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> executor</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)">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)">executor </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)"> executor</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></code><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)">synchronized</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)"> 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)">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)">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)"> r</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)"> tasks</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)">offer</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)">Runnable</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(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)"> r</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)">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)"><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)">finally</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)"> scheduleNext</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"><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)">active </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)">null</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)"> scheduleNext</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)">protected</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)">synchronized</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)"> scheduleNext</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)">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)">active </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)"> tasks</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)">poll</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)">null</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)"> executor</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,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">active</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></pre> <span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:许多Executor的实现都对调度任务的方式和时间强加了某种限制。<br style="line-height:25px"> 所以如果我们想要任务<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">一个一个的顺序被执行,可以才采用示例1(SerialExecutor)的方式<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 此包中提供的Executor实现都实现了ExecutorService,ExecutorService是Executor的一个子类,它提供了一个使用更广泛的接口。<br style="line-height:25px"> 比如ThreadPoolExecutor类就同时实现了Executor和ExecutorService接口,它提供一个可扩展的线程池实现。<br style="line-height:25px"> Executors类为创建这些Executor提供了便捷的工厂方法。<br style="line-height:25px"><span style="line-height:25px">注意</span>:Executor的直接子接口ExecutorService,ExecutorService的直接子接口(Executor的间接子接口)ScheduledExecutorService提供了更为广泛使用的接口。<br style="line-height:25px"> 关于ExecutorService的更多知识请参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920109544134947/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ExecutorService</a></strong>》,<br style="line-height:25px"> 关于ScheduledExecutorService的更多知识请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920109544814639/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ScheduledExecutorService</a></strong>》。<br style="line-height:25px"> 内存一致性效果:线程中将Runnable对象提交到Executor之前的操作happen-before其执行开始(可能在另一个线程中)。<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"><span style="color:#993300; line-height:25px">void</span><span style="color:#ff6600; line-height:25px">execute</span>(Runnablecommand)<br style="line-height:25px"> 在未来某个时间执行给定的命令。该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由Executor实现决定。<br style="line-height:25px"> 参数:<br style="line-height:25px"> command-可运行的任务<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">:该方法是它自己<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">唯一</span><wbr style="line-height:25px">的方法。 </wbr></wbr></wbr></wbr></wbr></wbr></wbr>
不过,<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">Executo</span><span style="color:#003366; line-height:25px">r接口并没有严格地要求执行是异步的<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 因此执行程序可以在调用者的线程中立即运行已提交的任务:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></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)">DirectExecutor</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)">Executor</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)"> 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(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)"> r</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)"> r</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)">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)"><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></pre> 更常见的是,任务是在某个不是调用者线程的线程中执行的。以下执行程序将为每个任务生成一个新线程。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></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)">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)">ThreadPerTaskExecutor</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)">Executor</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)"> 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(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)"> r</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)">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)">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)">r</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)">start</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></pre> 许多Executor的实现都对调度任务的方式和时间强加了某种限制。以下执行程序使任务提交与第二个执行程序保持连续,这说明了一个复合执行程序。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例1</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></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><code style="line-height:1em; color:rgb(0,112,0); font-family:monospace"><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)">SerialExecutor</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)">Executor</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)">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)">Queue</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> tasks </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)">ArrayDeque</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)">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)">Executor</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> executor</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)">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)"> active</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(102,0,102)"><span class="typ" style="line-height:23px; color:rgb(102,0,102)">SerialExecutor</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)">Executor</span></span><span style="line-height:23px; color:rgb(0,0,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> executor</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)">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)">executor </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)"> executor</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></code><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)">synchronized</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)"> 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)">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)">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)"> r</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)"> tasks</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)">offer</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)">Runnable</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(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)"> r</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)">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)"><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)">finally</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)"> scheduleNext</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"><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)">active </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)">null</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)"> scheduleNext</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)">protected</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)">synchronized</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)"> scheduleNext</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)">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)">active </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)"> tasks</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)">poll</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)">null</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)"> executor</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,0)"><span class="pln" style="line-height:23px; color:rgb(0,0,0)">active</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></pre> <span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:许多Executor的实现都对调度任务的方式和时间强加了某种限制。<br style="line-height:25px"> 所以如果我们想要任务<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">一个一个的顺序被执行,可以才采用示例1(SerialExecutor)的方式<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 此包中提供的Executor实现都实现了ExecutorService,ExecutorService是Executor的一个子类,它提供了一个使用更广泛的接口。<br style="line-height:25px"> 比如ThreadPoolExecutor类就同时实现了Executor和ExecutorService接口,它提供一个可扩展的线程池实现。<br style="line-height:25px"> Executors类为创建这些Executor提供了便捷的工厂方法。<br style="line-height:25px"><span style="line-height:25px">注意</span>:Executor的直接子接口ExecutorService,ExecutorService的直接子接口(Executor的间接子接口)ScheduledExecutorService提供了更为广泛使用的接口。<br style="line-height:25px"> 关于ExecutorService的更多知识请参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920109544134947/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ExecutorService</a></strong>》,<br style="line-height:25px"> 关于ScheduledExecutorService的更多知识请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920109544814639/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ScheduledExecutorService</a></strong>》。<br style="line-height:25px"> 内存一致性效果:线程中将Runnable对象提交到Executor之前的操作happen-before其执行开始(可能在另一个线程中)。<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"><span style="color:#993300; line-height:25px">void</span><span style="color:#ff6600; line-height:25px">execute</span>(Runnablecommand)<br style="line-height:25px"> 在未来某个时间执行给定的命令。该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由Executor实现决定。<br style="line-height:25px"> 参数:<br style="line-height:25px"> command-可运行的任务<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">:该方法是它自己<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">唯一</span><wbr style="line-height:25px">的方法。 </wbr></wbr></wbr></wbr></wbr></wbr></wbr>
相关推荐
XXL-JOB是一个分布式任务调度平台,而"xxl-job-executor-go-master"则表示这个项目是XXL-JOB的Go语言实现版本的源码仓库。这个仓库是针对XXL-JOB执行器的一个Go语言实现,它使得开发者可以利用Go语言来编写调度任务...
hadoop自带的Container-executor在配置yarn-kerberos时存在问题,以及在配置cgroup时需要把container-executor.cfg的上级目录拥有者均改为root,带来不便。 所以需要重新编译Container-executor,这边提供重新编译好...
### Spark:Executor分配详解 #### 一、Spark的资源分配机制概述 在深入探讨Executor的分配之前,我们先从整体上理解一下Spark集群的工作原理。Spark集群架构主要包括以下几个核心组件:SparkMaster(简称Master)...
Executor框架是Java并发编程的核心组件,它在Java 5中被引入,极大地简化了多线程编程。这个框架是基于`java.util.concurrent`包中的接口和类构建的,旨在提供线程池服务、任务调度以及并发执行任务的能力。Executor...
Executor 2.1 Pre-Release 11是一款专为非苹果电脑设计的Macintosh OS模拟程序,它允许用户在非Macintosh系统上运行基于Classic Mac OS的应用程序。这个软件版本是一个预发布版,意味着它是公开发布前的最后一个测试...
Executor是一款高效且便捷的应用启动工具,它允许用户通过简单的配置,快速地打开电脑中的任何应用程序,从而极大地提升了桌面管理的效率。在日常工作中,我们常常会安装很多软件,导致桌面图标繁多,查找起来十分...
而`Executor`则是`SqlSession`内部的一个执行器接口,是MyBatis实现数据库操作的核心部分。本文将深入探讨`SqlSession`和`Executor`的实现机制及其在MyBatis中的作用。 `SqlSession`是MyBatis的对外接口,它提供了...
Executor是一款高效的快捷启动软件,它的主要功能是帮助用户快速启动应用程序、文件或系统命令,极大地提高了用户的工作效率。在IT领域,这样的工具被广泛使用,尤其是对于经常需要频繁切换和执行各种任务的用户来说...
FANUC C Language Executor
hadoop自带的Container-executor在配置yarn-kerberos时存在问题,这边给出编译后的Container-executor,默认加载配置文件路径/etc/container-executor.cfg,大家不用再重新编译了
**Flask Executor** 是一个基于Python的Web框架——**Flask** 的扩展,它允许你在Flask应用中异步执行任务。这个库的核心功能是引入了线程池或进程池来处理那些耗时较长的任务,避免阻塞主线程,从而提高Web应用的...
【Executor、Executors和ExecutorService详解】 在Java并发编程中,`Executor`、`Executors`和`ExecutorService`是核心组件,它们帮助开发者高效管理线程资源,提高程序的并发性能。理解这三个概念的区别和用途是...
安装时,你需要解压这个文件,根据提供的文档配置服务器环境,如设置JVM参数、配置executor.properties文件,并启动Executor服务,使其能够接收和处理任务请求。 **Azkaban Web Server** 则是用户界面和任务管理的...
3. Multiple-Executor Mode:在大型或分布式环境中使用,Web Server和多个Executor Server分布在不同的机器上,同样使用MySQL数据库,提供更高的可扩展性和容错性。 部署Azkaban的Solo Server模式通常涉及以下几个...
标题提到的"azkaban 3.4 编译后安装文件,分布式多executor"指的是Azkaban的3.4版本,该版本支持构建并部署到一个具有多个Executor节点的集群中,以提高任务处理能力。 安装文档是指导用户如何正确安装和配置...
《ha-executor_v09842一键秒开软件:高效快捷的启动工具解析》 在信息化时代,软件已经成为我们日常工作中不可或缺的一部分。为了提高工作效率,快速启动常用软件显得尤为重要。ha-executor_v09842一键秒开软件正是...
"Executor汉化版"作为其中的佼佼者,不仅以其炫酷的界面吸引用户,还以其实用的功能满足不同用户的需求。本文将对"Executor汉化版"这一快速启动工具的功能、特点以及使用方式进行详细解读。 首先,"Executor汉化版...
该文档详细记录了Executor框架结构、使用示意图、ThreadPoolExecutor使用示例、线程池原理分析、几种常见线程池(FixedThreadPool、SingleThreadExecutor、CachedThreadPool)的详解以及线程池大小确定等内容
`Executor`线程池是一种优秀的解决方案,它可以帮助开发者更好地控制并发执行的任务,避免过度创建线程导致资源浪费和系统稳定性下降。本文将深入探讨`Android Executor`线程池的工作原理、优势以及如何在实际开发中...