java中Executor、ExecutorService、ThreadPoolExecutor介绍

1.Excutor

源码非常简单,只有一个execute(Runnable command)回调接口

public interface Executor {

/**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the <tt>Executor</tt> implementation.
*
* @param command the runnable task
* @throws RejectedExecutionException if this task cannot be
* accepted for execution.
* @throws NullPointerException if command is null
*/
void execute(Runnable command);
}

执行已提交的Runnable任务对象。此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节、调度等)分离开来的方法。通常使用Executor而不是显式地创建线程。例如,可能会使用以下方法,而不是为一组任务中的每个任务调用new Thread(new(RunnableTask())).start()

 Executor executor = anExecutor;
 executor.execute(new RunnableTask1());
 executor.execute(new RunnableTask2());
 ...
 
不过,Executor接口并没有严格地要求执行是异步的。在最简单的情况下,执行程序可以在调用方的线程中立即运行已提交的任务:
 class DirectExecutor implements Executor {
     public void execute(Runnable r) {
         r.run();
     }
 }
更常见的是,任务是在某个不是调用方线程的线程中执行的。以下执行程序将为每个任务生成一个新线程。
 class ThreadPerTaskExecutor implements Executor {
     public void execute(Runnable r) {
         new Thread(r).start();
     }
 }
许多Executor实现都对调度任务的方式和时间强加了某种限制。以下执行程序使任务提交与第二个执行程序保持连续,这说明了一个复合执行程序。
 class SerialExecutor implements Executor {
     final Queue<Runnable> tasks = new LinkedBlockingQueue<Runnable>();
     final Executor executor;
     Runnable active;

     SerialExecutor(Executor executor) {
         this.executor = executor;
     }

     public synchronized void execute(final Runnable r) {
         tasks.offer(new Runnable() {
             public void run() {
                 try {
                     r.run();
                 } finally {
                     scheduleNext();
                 }
             }
         });
         if (active == null) {
             scheduleNext();
         }
     }

     protected synchronized void scheduleNext() {
         if ((active = tasks.poll()) != null) {
             executor.execute(active);
         }
     }
 }
 
2.ExcutorService接口
         ExecutorService提供了管理终止的方法,以及可为跟踪一个或多个异步任务执行状况而生成Future的方法。
可以关闭ExecutorService,这将导致其拒绝新任务<wbr>。提供两个方法来关闭ExecutorService。
<wbr><span style="color:rgb(153,51,0)">shutdown()</span><span style="color:rgb(0,51,102)">方法在终止前允许执行以前提交的任务,而</span><span style="color:rgb(153,51,0)">shutdownNow()</span><span style="color:rgb(0,51,102)">方法阻止等待任务的启动并试图停止当前正在执行的任务</span><wbr>。在终止后,执行程序没有任务在执行,也没有任务在等待执行,并且无法提交新任务。<wbr><span style="color:rgb(0,0,128)">应该关闭未使用的</span><span style="color:rgb(255,0,255)">ExecutorService</span><span style="color:rgb(0,0,128)">以允许回收其资源<wbr>。</wbr></span>
通过创建并返回一个可用于取消执行和/或等待完成的<span style="color:rgb(255,102,0)">Future</span>,方法submit扩展了基本方法<span style="color:rgb(0,0,255)">Executor.execute(java.lang.Runnable)</span>。
<wbr><span style="color:rgb(0,0,128)">方法</span><span style="color:rgb(255,102,0)">invokeAny</span><span style="color:rgb(0,0,128)">和</span><span style="color:rgb(255,102,0)">invokeAll</span><span style="color:rgb(0,0,128)">是批量执行的最常用形式<wbr>,</wbr></span>它们执行任务collection,然后等待<wbr><span style="color:rgb(0,0,128)">至少一个,
或全部任务完成</span><wbr>(可使用ExecutorCompletionService类来编写这些方法的自定义变体)。
Executors类为创建ExecutorService提供了便捷的工厂方法。
<wbr>注意1<wbr>:它只有一个直接实现类<wbr><span style="color:rgb(153,204,0)">ThreadPoolExecutor</span><wbr>和间接实现类<wbr><span style="color:rgb(128,128,0)">ScheduledThreadPoolExecutor</span><wbr>。
关于<span style="color:rgb(153,204,0)">ThreadPoolExecutor</span>的更多内容请参考<wbr>《<strong><a target="_blank" title="阅读全文" href="http://hubingforever.blog.163.com/blog/static/1710405792010964339151/" style="color:rgb(207,121,28); text-decoration:none">ThreadPoolExecutor</a></strong>》<wbr>
关于<span style="color:rgb(128,128,0)">ScheduledThreadPoolExecutor</span>的更多内容请参考<wbr>《<strong><a target="_blank" title="阅读全文" href="http://hubingforever.blog.163.com/blog/static/17104057920109643632988/" style="color:rgb(207,121,28); text-decoration:none">ScheduledThreadPoolExecutor</a></strong>》<wbr>
用法示例
下面给出了一个网络服务的简单结构,这里线程池中的线程作为传入的请求。它使用了预先配置的<span style="color:rgb(0,0,255)">Executors.newFixedThreadPool(int)</span>工厂方法:
<span style="line-height:normal; font-family:arial,sans-serif; font-size:13px"></span><pre code_snippet_id="96835" snippet_file_name="blog_20131204_1_1794569" class="prettyprint" name="code" style="white-space: pre-wrap; word-wrap: break-word; line-height: inherit; padding: 10px; border: 1px solid rgb(204, 204, 204); color: rgb(0, 112, 0); background-color: rgb(250, 250, 250); margin-top: 0px; margin-bottom: 1em; margin-left: 1em; overflow: auto;"><span style="line-height: 23px; color: rgb(0, 0, 136);">class</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">NetworkService</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">implements</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">Runnable</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">private</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">final</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">ServerSocket</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> serverSocket</span><span style="line-height: 23px; color: rgb(102, 102, 0);">;</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">private</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">final</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">ExecutorService</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> pool</span><span style="line-height: 23px; color: rgb(102, 102, 0);">;</span><span style="line-height: 23px; color: rgb(0, 0, 0);">

<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">public</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">NetworkService</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 0, 136);">int</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> port</span><span style="line-height: 23px; color: rgb(102, 102, 0);">,</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">int</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> poolSize</span><span style="line-height: 23px; color: rgb(102, 102, 0);">)</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">   </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">throws</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">IOException</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  serverSocket </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">=</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">new</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">ServerSocket</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 0, 0);">port</span><span style="line-height: 23px; color: rgb(102, 102, 0);">);</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  pool </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">=</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">Executors</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">newFixedThreadPool</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 0, 0);">poolSize</span><span style="line-height: 23px; color: rgb(102, 102, 0);">);</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">

<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">public</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">void</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> run</span><span style="line-height: 23px; color: rgb(102, 102, 0);">()</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(136, 0, 0);">// run the service</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">try</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">   </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">for</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">(;;)</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">    pool</span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">execute</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 0, 136);">new</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">Handler</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 0, 0);">serverSocket</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">accept</span><span style="line-height: 23px; color: rgb(102, 102, 0);">()));</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">   </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">catch</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(102, 0, 102);">IOException</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> ex</span><span style="line-height: 23px; color: rgb(102, 102, 0);">)</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">   pool</span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">shutdown</span><span style="line-height: 23px; color: rgb(102, 102, 0);">();</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"></span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">

<span class="pln"></span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">class</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">Handler</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">implements</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">Runnable</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">private</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">final</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">Socket</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> socket</span><span style="line-height: 23px; color: rgb(102, 102, 0);">;</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(102, 0, 102);">Handler</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(102, 0, 102);">Socket</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> socket</span><span style="line-height: 23px; color: rgb(102, 102, 0);">)</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">this</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">socket </span><span style="line-height: 23px; color: rgb(102, 102, 0);">=</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> socket</span><span style="line-height: 23px; color: rgb(102, 102, 0);">;</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">public</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">void</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> run</span><span style="line-height: 23px; color: rgb(102, 102, 0);">()</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(136, 0, 0);">// read and service request on socket</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"></span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"></span></span></pre></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>下列方法分两个阶段关闭ExecutorService。第一阶段<wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">调用<wbr><wbr>shutdown拒绝传入任务</wbr></wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,然后等60秒后,任务还没执行完成,就调用shutdownNow(如有必要)取消所有遗留的任务</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">:</span><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px">
<span style="line-height:normal; font-family:arial,sans-serif; font-size:13px"></span><pre code_snippet_id="96835" snippet_file_name="blog_20131204_2_8828182" class="prettyprint" name="code" style="white-space: pre-wrap; word-wrap: break-word; line-height: inherit; padding: 10px; border: 1px solid rgb(204, 204, 204); color: rgb(0, 112, 0); background-color: rgb(250, 250, 250); margin-top: 0px; margin-bottom: 1em; margin-left: 1em; overflow: auto;"><span style="line-height: 23px; color: rgb(0, 0, 0);"></span><span style="line-height: 23px; color: rgb(0, 0, 136);">void</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> shutdownAndAwaitTermination</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(102, 0, 102);">ExecutorService</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> pool</span><span style="line-height: 23px; color: rgb(102, 102, 0);">)</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> pool</span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">shutdown</span><span style="line-height: 23px; color: rgb(102, 102, 0);">();</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(136, 0, 0);">// Disable new tasks from being submitted</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">try</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(136, 0, 0);">// Wait a while for existing tasks to terminate</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">if</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">(!</span><span style="line-height: 23px; color: rgb(0, 0, 0);">pool</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">awaitTermination</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 102, 102);">60</span><span style="line-height: 23px; color: rgb(102, 102, 0);">,</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">TimeUnit</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">SECONDS</span><span style="line-height: 23px; color: rgb(102, 102, 0);">))</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">   pool</span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">shutdownNow</span><span style="line-height: 23px; color: rgb(102, 102, 0);">();</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(136, 0, 0);">// Cancel currently executing tasks</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">   </span></span><span style="line-height: 23px; color: rgb(136, 0, 0);">// Wait a while for tasks to respond to being cancelled</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">   </span></span><span style="line-height: 23px; color: rgb(0, 0, 136);">if</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">(!</span><span style="line-height: 23px; color: rgb(0, 0, 0);">pool</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">awaitTermination</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 102, 102);">60</span><span style="line-height: 23px; color: rgb(102, 102, 0);">,</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 0, 102);">TimeUnit</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">SECONDS</span><span style="line-height: 23px; color: rgb(102, 102, 0);">))</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">     </span></span><span style="line-height: 23px; color: rgb(102, 0, 102);">System</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">err</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">println</span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(0, 136, 0);">"Pool did not terminate"</span><span style="line-height: 23px; color: rgb(102, 102, 0);">);</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(0, 0, 136);">catch</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">(</span><span style="line-height: 23px; color: rgb(102, 0, 102);">InterruptedException</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> ie</span><span style="line-height: 23px; color: rgb(102, 102, 0);">)</span><span style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span style="line-height: 23px; color: rgb(102, 102, 0);">{</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(136, 0, 0);">// (Re-)Cancel if current thread also interrupted</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  pool</span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">shutdownNow</span><span style="line-height: 23px; color: rgb(102, 102, 0);">();</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(136, 0, 0);">// Preserve interrupt status</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln">  </span></span><span style="line-height: 23px; color: rgb(102, 0, 102);">Thread</span><span style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span style="line-height: 23px; color: rgb(0, 0, 0);">currentThread</span><span style="line-height: 23px; color: rgb(102, 102, 0);">().</span><span style="line-height: 23px; color: rgb(0, 0, 0);">interrupt</span><span style="line-height: 23px; color: rgb(102, 102, 0);">();</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"> </span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"></span></span><span style="line-height: 23px; color: rgb(102, 102, 0);">}</span><span style="line-height: 23px; color: rgb(0, 0, 0);">
<span class="pln"></span></span></pre>内存一致性效果:线程中向ExecutorService提交Runnable或Callable任务之前的操作happen-before由该任务所提取的所有操作,<br>后者依次happen-before通过Future.get()获取的结果。<br><wbr>主要函数<wbr>:<br><wbr><span style="color:rgb(153,51,0)">void</span><span style="color:rgb(255,102,0)">shutdown()</span><wbr><br>启动一个关闭命令,不再接受新任务,当所有已提交任务执行完后,就关闭。如果已经关闭,则调用没有其他作用。<br>抛出:<br>SecurityException-如果安全管理器存在并且关闭,此ExecutorService可能操作某些不允许调用者修改的线程(因为它没有保持RuntimePermission("modifyThread")),或者安全管理器的checkAccess方法拒绝访问。<br><wbr>List&lt;Runnable&gt;<span style="color:rgb(255,102,0)">shutdownNow()</span><wbr><br>试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表。<br>无法保证能够停止正在处理的活动执行任务,但是会尽力尝试。例如,通过Thread.interrupt()来取消典型的实现,所以任何任务无法响应中断都可能永远无法终止。<br>返回:<br>从未开始执行的任务的列表<br>抛出:<br>SecurityException-如果安全管理器存在并且关闭,<br>此ExecutorService可能操作某些不允许调用者修改的线程(因为它没有保持RuntimePermission("modifyThread")),<br>或者安全管理器的checkAccess方法拒绝访问。<br><wbr>注意1: <span style="color:rgb(0,0,128)">它会返回等待执行的任务列表。</span><br><wbr>注意2: 无<span style="color:rgb(0,0,128)">法保证能够停止正在处理的活动执行任务,但是会尽力尝试。例如,通过Thread.interrupt()来取消,<br>所以任何任务无法响应中断都可能永远无法终止。</span><br><wbr><span style="color:rgb(153,51,0)">boolean</span><span style="color:rgb(255,102,0)">isShutdown()</span><wbr><br>如果此执行程序已关闭,则返回true。<br>返回:<br>如果此执行程序已关闭,则返回true<br><wbr><span style="color:rgb(153,51,0)">boolean</span><span style="color:rgb(255,102,0)">isTerminated()</span><wbr><br>如果关闭后所有任务都已完成,则返回true。注意,除非首先调用shutdown或shutdownNow,否则isTerminated永不为true。<br>返回:<br>如果关闭后所有任务都已完成,则返回true<br><wbr><span style="color:rgb(153,51,0)">boolean</span><span style="color:rgb(255,102,0)">awaitTermination</span>(longtimeout,TimeUnitunit)throwsInterruptedException<wbr><br>等待(阻塞)直到关闭或最长等待时间或发生中断<br>参数:<br>timeout-最长等待时间<br>unit-timeout参数的时间单位<br>返回:<br>如果此执行程序终止,则返回true;如果终止前超时期满,则返回false<br>抛出:<br>InterruptedException-如果等待时发生中断<br><wbr>注意1:如果此执行程序终止(关闭),则返回true;如果终止前超时期满,则返回false<wbr><br>&lt;T&gt;Future&lt;T&gt;<wbr><span style="color:rgb(255,102,0)">submit</span><wbr>(Callable&lt;T&gt;task)<br>提交一个返回值的任务用于执行,返回一个表示任务的未决结果的Future。该Future的get方法在成功完成时将会返回该任务的结果。<br>如果想立即阻塞任务的等待,则可以使用result=exec.submit(aCallable).get();形式的构造。<br>注:Executors类包括了一组方法,可以转换某些其他常见的类似于闭包的对象,<br>例如,将PrivilegedAction转换为Callable形式,这样就可以提交它们了。<br>参数:<br>task-要提交的任务<br>返回:<br>表示任务等待完成的Future<br>抛出:<br>RejectedExecutionException-如果任务无法安排执行<br>NullPointerException-如果该任务为null<br><wbr>注意<wbr>:关于submit的使用和Callable可以参阅《<strong><a target="_blank" title="阅读全文" href="http://hubingforever.blog.163.com/blog/static/171040579201083043930871/" style="color:rgb(207,121,28); text-decoration:none">使用Callable返回结果</a></strong>》</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</div>
<div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px">&lt;T&gt;Future&lt;T&gt;submit(Runnabletask,Tresult)<br>提交一个Runnable任务用于执行,并返回一个表示该任务的Future。该Future的get方法在成功完成时将会返回给定的结果。<br>参数:<br>task-要提交的任务<br>result-返回的结果<br>返回:<br>表示任务等待完成的Future<br>抛出:<br>RejectedExecutionException-如果任务无法安排执行<br>NullPointerException-如果该任务为null<br><wbr>注意<wbr>:关于submit的使用可以参阅《Callable》<br>Future&lt;?&gt;<wbr><span style="color:rgb(255,102,0)">submit</span><wbr>(Runnabletask)<br>提交一个Runnable任务用于执行,并返回一个表示该任务的Future。该Future的get方法在成功完成时将会返回null。<br>参数:<br>task-要提交的任务<br>返回:<br>表示任务等待完成的Future<br>抛出:<br>RejectedExecutionException-如果任务无法安排执行<br>NullPointerException-如果该任务为null<br>注意:关于submit的使用可以参阅《<strong><a target="_blank" title="阅读全文" href="http://hubingforever.blog.163.com/blog/static/171040579201083043930871/" style="color:rgb(207,121,28); text-decoration:none">使用Callable返回结果</a></strong>》<br>&lt;T&gt;List&lt;Future&lt;T&gt;&gt;<wbr><span style="color:rgb(255,102,0)">invokeAll</span><wbr>(Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks)throwsInterruptedException<br>执行给定的任务,当所有任务完成时,返回保持任务状态和结果的Future列表。返回列表的所有元素的Future.isDone()为true。<br>注意,可以正常地或通过抛出异常来终止已完成任务。如果正在进行此操作时修改了给定的collection,则此方法的结果是不确定的。<br>参数:<br>tasks-任务collection<br>返回:<br>表示任务的Future列表,列表顺序与给定任务列表的迭代器所生成的顺序相同,每个任务都已完成。<br>抛出:<br>InterruptedException-如果等待时发生中断,在这种情况下取消尚未完成的任务。<br>NullPointerException-如果任务或其任意元素为null<br>RejectedExecutionException-如果所有任务都无法安排执行<br><wbr>注意1<wbr>:该方法会一直阻塞直到所有任务完成。<br>&lt;T&gt;List&lt;Future&lt;T&gt;&gt;<wbr><span style="color:rgb(255,102,0)">invokeAll</span><wbr>(Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks,<br>longtimeout,<br>TimeUnitunit)<br>throwsInterruptedException<br>执行给定的任务,当所有任务完成或超时期满时(无论哪个首先发生),返回保持任务状态和结果的Future列表。返回列表的所有元素的Future.isDone()为true。一旦返回后,即取消尚未完成的任务。注意,可以正常地或通过抛出异常来终止已完成任务。如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。<br>参数:<br>tasks-任务collection<br>timeout-最长等待时间<br>unit-timeout参数的时间单位<br>返回:<br>表示任务的Future列表,列表顺序与给定任务列表的迭代器所生成的顺序相同。<br>如果操作未超时,则已完成所有任务。如果确实超时了,则某些任务尚未完成。<br>抛出:<br>InterruptedException-如果等待时发生中断,在这种情况下取消尚未完成的任务<br>NullPointerException-如果任务或其任意元素或unit为null<br>RejectedExecutionException-如果所有任务都无法安排执行<br><wbr>注意1<wbr>:该方法会一直阻塞直到所有任务完成或超时。<br><wbr>注意2<wbr>:如果确实超时了,则某些任务尚未完成。【那么这些尚未完成的任务应该被系统取消】。<br>&lt;T&gt;TinvokeAny(Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks)<br>throwsInterruptedException,<br>ExecutionException<br>执行给定的任务,如果某个任务已成功完成(也就是未抛出异常),则返回其结果。一旦正常或异常返回后,则取消尚未完成的任务。<br>如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。<br>参数:<br>tasks-任务collection<br>返回:<br>某个任务返回的结果<br>抛出:<br>InterruptedException-如果等待时发生中断<br>NullPointerException-如果任务或其任意元素为null<br>IllegalArgumentException-如果任务为空<br>ExecutionException-如果没有任务成功完成<br>RejectedExecutionException-如果任务无法安排执行<br><wbr>注意1<wbr>:该方法会一直阻塞直到有一个任务完成。<br><wbr>注意2<wbr>:一旦正常或异常返回后,<wbr><span style="color:rgb(0,0,128)">则取消尚未完成的任务</span><wbr><br>&lt;T&gt;T<wbr><span style="color:rgb(255,102,0)">invokeAny<wbr>(</wbr></span>Collection&lt;?extendsCallable&lt;T&gt;&gt;tasks,<br>longtimeout,<br>TimeUnitunit)<br>throwsInterruptedException,<br>ExecutionException,<br>TimeoutException<br><br>执行给定的任务,如果在给定的超时期满前某个任务已成功完成(也就是未抛出异常),则返回其结果。一旦正常或异常返回后,则取消尚未完成的任务。如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。<br><br>参数:<br>tasks-任务collection<br>timeout-最长等待时间<br>unit-timeout参数的时间单位<br>返回:<br>某个任务返回的结果<br>抛出:<br>InterruptedException-如果等待时发生中断<br>NullPointerException-如果任务或其任意元素或unit为null<br>TimeoutException-如果在所有任务成功完成之前给定的超时期满<br>ExecutionException-如果没有任务成功完成<br>RejectedExecutionException-如果任务无法安排执行<br><wbr>注意1<wbr>:<span style="color:rgb(0,0,128)">该方法会一直阻塞直到有一个任务完成。</span><br><wbr>注意2<wbr>:一旦正常或异常返回后,<wbr><span style="color:rgb(0,0,128)">则取消尚未完成的任务</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</div>
<div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(0,0,128)"><br></span></div>
<div style="color:rgb(51,51,51); font-family:Arial; line-height:25px"><span style="color:rgb(0,0,128)"><span style="font-size:32px"><strong>3.<span style="color:rgb(255,0,255); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; line-height:28px; white-space:pre; background-color:rgb(245,245,245)">ThreadPoolExecutor</span></strong></span></span></div>
<div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(0,0,128)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245); color:rgb(255,0,255)">        ThreadPoolExecutor</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">是</span><span style="color:#ff9900; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ExecutorService</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">的一个实现类,它使用可能的几个池线程之一执行每个提交的任务,通常使用Executors工厂方法配置。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">线程池可以解决两个不同问题:由于减少了每个任务调用的开销,它们通常可以在执行大量异步任务时提供增强的性能,并且还可以提供绑定和管理资源(包括执行任务集时使用的线程)的方法</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">每个</span><span style="color:#ff00ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ThreadPoolExecutor</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">还维护着一些基本的统计数据,如完成的任务数。为了便于跨大量上下文使用,此类提供了很多可调整的参数和扩展钩子(hook)。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">但是,强烈建议程序员使用较为方便的Executors工厂方法</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">Executors.<wbr>newCachedThreadPool<wbr>()</wbr></wbr></span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">无界线程池,可以进行自动线程回收</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">)、</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">Executors.newFixedThreadPool(int)</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">固定大小线程池</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">)和</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">Executors.newSingleThreadExecutor()</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">单个后台线程</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">),</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">它们均为大多数使用场景预定义了设置。否则,在手动配置和调整此类时,使用以下指导:</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300"><wbr>核心和最大池大小</wbr></span></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#ff00ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ThreadPoolExecutor</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">将根据</span><span style="color:#99cc00; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">corePoolSize</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(参见getCorePoolSize())和</span><span style="color:#808000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">maximumPoolSize</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(参见getMaximumPoolSize())</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">设置的边界自动调整池大小。</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">当新任务在方法</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">execute(java.lang.Runnable)</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">中提交时,如果运行的线程少于</span><span style="color:#99cc00; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">corePoolSize</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">,则创建新线程来处理请求,即使有线程是空闲的。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">如果运行的线程多于</span><span style="color:#99cc00; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">corePoolSize</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">而少于</span><span style="color:#808000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">maximumPoolSiz</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">e,则仅当队列满时才创建新线程。<br>如果设置的</span><span style="color:#99cc00; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">corePoolSize</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">和</span><span style="color:#808000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">maximumPoolSize</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">相同,则创建了固定大小的线程池。<br>如果将</span><span style="color:#808000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">maximumPoolSize</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">设置为基本的无界值(如</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">Integer.MAX_VALUE</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">),则允许池适应任意数量的并发任务。<br>在大多数情况下,核心和最大池大小仅基于构造来设置,不过也可以使用</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">setCorePoolSize(int)</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">和</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">setMaximumPoolSize(int)</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">进行动态更改。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">1:在新任务被提交时,如果运行的core线程少于corePoolSize,才创建新core线程。并不是一开始就创建corePoolSize个core线程。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">2:"如果运行的线程多于corePoolSize而少于maximumPoolSize,则仅当队列满时才创建新线程"</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300"><wbr>按需构造</wbr></span></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">核心线程最初只是在新任务到达时才被ThreadPoolExecutor创建和启动的,</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">但是</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">也可以手动调用方法</span><span style="color:#339966; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">prestartCoreThread()</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">或</span><span style="color:#808000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">prestartAllCoreThreads()</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">来的提前启动核心线程<wbr>。</wbr></span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">如果构造带有非空队列的池,这时则可能希望预先启动线程。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">1:核心线程即core线程,只有当前线程数小于等于corePoolSize时,这时的线程才叫核心线程。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300"><wbr>创建新线程</wbr></span></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">使用</span><span style="color:#ff00ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ThreadFactory</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">创建新线程。如果没有另外说明,则使用</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">Executors.defaultThreadFactory()</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">创建线程,他们在同一个ThreadGroup中<br>并且这些线程具有相同的NORM_PRIORITY优先级和非守护进程状态。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">通过提供不同的ThreadFactory,可以改变线程的名称、线程组、优先级、守护进程状态,等等。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">如果从newThread返回null时ThreadFactory未能创建线程,则执行程序将继续运行,但不能执行任何任务。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">1:可以指定创建线程的ThreadFactory,默认的是使用Executors.defaultThreadFactory()来创建线程,所有的线程都在一个ThreadGroup中。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300"><wbr>保持活动时间</wbr></span></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">如果池中当前有多于corePoolSize的线程,则这些多出的线程在空闲时间超过keepAliveTime时将会终止<br>(参见</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">getKeepAliveTime(java.util.concurrent.TimeUnit)</span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">)。这提供了当池处于非活动状态时减少资源消耗的方法。<br>如果池后来变得更为活动,则可以创建新的线程。也可以使用方法setKeepAliveTime(long,java.util.concurrent.TimeUnit)动态地更改此参数。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">如果把值设为</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">Long.MAX_VALUETimeUnit.NANOSECONDS</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">的话,</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">空闲线程不会被回收</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">直到ThreadPoolExecutor为Terminate。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">默认情况下,保持活动策略只在有多于corePoolSizeThreads的线程时应用。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">但是只要keepAliveTime值非0,</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">allowCoreThreadTimeOut(boolean)</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">方法也可将此超时策略应用于核心线程。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">1:setKeepAliveTime(long,java.util.concurrent.TimeUnit)用于设置空闲线程最长的活动时间,</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">即如果空闲时间超过设定值,就停掉该线程,对该线程进行回收。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">该策略默认只对非内核线程有用(即当前线程数大于corePoolSize),</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">可以调用allowCoreThreadTimeOut(boolean)方法将此超时策略扩大到核心线程</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">2:如果把值设为Long.MAX_VALUETimeUnit.NANOSECONDS的话,空闲线程不会被回收直到ThreadPoolExecutor为Terminate。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300"><wbr>排队</wbr></span></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">所有</span><span style="color:#ff6600; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">BlockingQueue</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">都可用于传输和保持提交的任务。可以使用此队列与池大小进行交互:</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">*如果运行的线程少于corePoolSize,则Executor始终首选添加新的线程,而不进行排队。<br>*如果运行的线程等于或多于corePoolSize,则Executor始终首选将请求加入队列,而不添加新的线程。<br>*如果无法将请求加入队列,则创建新的线程,除非创建此线程超出maximumPoolSize,在这种情况下,任务将被拒绝</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">排队有三种通用策略:</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>1.直接提交</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">。工作队列的默认选项是SynchronousQueue,它将任务直接提交给线程而不保持它们。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">在此,如果不存在可用于立即运行任务的线程,则试图把任务加入队列将失败,因此会构造一个新的线程。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">此策略可以避免在处理可能具有内部依赖性的请求集时出现锁。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">直接提交通常要求无界maximumPoolSizes以避免拒绝新提交的任务。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">当命令以超过队列所能处理的平均数连续到达时,此策略允许线程无界的增长。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">注意1</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">:此策略允许线程无界的增长。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>2.无界队列</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">。使用无界队列(例如,不具有预定义容量的LinkedBlockingQueue)将导致在所有corePoolSize线程都忙时新任务在队列中等待。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">这样,创建的线程就不会超过corePoolSize。(因此,maximumPoolSize的值也就无效了。)</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">当每个任务完全独立于其他任务,即任务执行互不影响时,适合于使用无界队列;例如,在Web页服务器中。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">这种排队可用于处理瞬态突发请求,当命令以超过队列所能处理的平均数连续到达时,此策略允许队列无限的增长。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">注意1:此策略允许队列无限的增长。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>3.有界队列</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">。当使用有限的maximumPoolSizes时,有界队列(如ArrayBlockingQueue)有助于防止资源耗尽,但是可能较难调整和控制。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">队列大小和最大池大小可能需要相互折衷:使用大型队列和小型池可以最大限度地降低CPU使用率、操作系统资源和上下文切换开销,</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">但是可能导致人工降低吞吐量。如果任务频繁阻塞(例如,如果它们是I/O边界),则系统可能为超过您许可的更多线程安排时间。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">使用小型队列通常要求较大的池大小,CPU使用率较高,但是可能遇到不可接受的调度开销,这样也会降低吞吐量。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300"><wbr>被拒绝的任务</wbr></span></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">当Executor已经关闭,或Executor将有限边界用于最大线程和工作队列容量,且已经饱和时,</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">在方法execute(java.lang.Runnable)中提交的新任务将被拒绝。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">在以上两种情况下,execute方法都将调用其</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#99cc00; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">RejectedExecutionHandler</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">的</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">RejectedExecutionHandler.rejectedExecution(java.lang.Runnable,java.util.concurrent.ThreadPoolExecutor)</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">方法。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">下面提供了</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">四种预定义的处理程序策略</span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">:</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">1.在默认的</span><span style="color:#ff9900; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ThreadPoolExecutor.AbortPolicy</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">中,处理程序遭到拒绝将抛出运行时RejectedExecutionException。<br>2.在</span><span style="color:#ff9900; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ThreadPoolExecutor.CallerRunsPolicy</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">中,线程调用运行该任务的execute本身。<br>此策略提供简单的反馈控制机制,能够减缓新任务的提交速度。<br>3.在</span><span style="color:#ff9900; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ThreadPoolExecutor.DiscardPolicy</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">中,不能执行的任务将被删除。<br>4.在</span><span style="color:#ff9900; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">ThreadPoolExecutor.DiscardOldestPolicy</span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">中,如果执行程序尚未关闭,<br>则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程)。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">定义和使用其他种类的RejectedExecutionHandler类也是可能的,但这样做需要非常小心,尤其是当策略仅用于特定容量或排队策略时</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">1:AbortPolicy,CallerRunsPolicy,DiscardPolicy和DiscardOldestPolicy都是rejectedExecution的一种实现。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">当然也可以自己定义个rejectedExecution实现。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><strong><wbr>钩子</wbr></strong><wbr>(hook)方法</wbr></span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">此类提供protected可重写的</span><span style="color:#0000ff; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"></span><span style="color:#339966; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">beforeExecute(</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">java.lang.Thread,java.lang.Runnable)</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">和</span><span style="color:#808000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">afterExecute</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(java.lang.Runnable,java.lang.Throwable)方法,这两种方法分别在执行每个任务之前和之后调用。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">它们可用于操纵执行环境;例如,重新初始化ThreadLocal、搜集统计信息或添加日志条目。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">此外,还可以重写方法</span><span style="color:#99cc00; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">terminated()</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">来执行Executor完全终止后需要完成的所有特殊处理。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>如果钩子(hook)或回调方法抛出异常,则ThreadPoolExecutor的所有线程将依次失败并突然终止<wbr>。<br><wbr>队列维护</wbr></wbr></wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">方法getQueue()允许出于监控和调试目的而访问工作队列。强烈反对出于其他任何目的而使用此方法。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#339966; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">remove</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(java.lang.Runnable)和</span><span style="color:#008000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">purge()</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">这两种方法可用于在取消大量已排队任务时帮助进行存储回收。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr>注意</wbr></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">1:如果任务取消,ThreadPoolExecutor应该自己是可以进行存储回收的。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">取消的任务不会再次执行,但是它们可能在工作队列中累积,直到worker线程主动将其移除</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">外部使用</span><span style="color:#339966; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">remove</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">(java.lang.Runnable)和</span><span style="color:#008000; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">purge()</span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">可以把它们立即从队列中移除。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="color:#993300"><wbr>终止</wbr></span></span><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#003366; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">如果ThreadPoolExecutor在程序中没有任何引用且没有任何活动线程,它也不会自动shutdown。<br>如果希望确保回收线程(即使用户忘记调用shutdown()),则必须安排未使用的线程最终终止:</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)"></span><span style="color:#000080; line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">设置适当保持活动时间,使用0核心线程的下边界和/或设置allowCoreThreadTimeOut(boolean)。</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">扩展示例。此类的大多数扩展可以重写一个或多个受保护的钩子(hook)方法。例如,下面是一个添加了简单的暂停/恢复功能的子类:</span><br style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><span style="background-color:rgb(245,245,245); color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span><pre class="prettyprint" style="line-height:inherit; white-space:pre-wrap; padding:10px; border:1px solid rgb(204,204,204); color:rgb(0,112,0); background-color:rgb(250,250,250); margin-top:0px; margin-bottom:1em; margin-left:1em; overflow:auto"><span style="line-height:22px; color:rgb(0,0,0)"></span><code style="line-height:1em"><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">class</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,0,102)"><span class="typ">PausableThreadPoolExecutor</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">extends</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,0,102)"><span class="typ">ThreadPoolExecutor</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">{</span></span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">private</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">boolean</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> isPaused</span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">;</span></span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">private</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,0,102)"><span class="typ">ReentrantLock</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> pauseLock </span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">=</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">new</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,0,102)"><span class="typ">ReentrantLock</span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">();</span></span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">private</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,0,102)"><span class="typ">Condition</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> unpaused </span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">=</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> pauseLock</span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">.</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln">newCondition</span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">();</span></span><span style="line-height:22px; color:rgb(0,0,0)">

<span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">public</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,0,102)"><span class="typ">PausableThreadPoolExecutor</span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">(...)</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">{</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)"><span class="kwd">super</span></span><span style="line-height:22px; color:rgb(102,102,0)"><span class="pun">(...);</span></span><span style="line-height:22px; color:rgb(0,0,0)"><span class="pln"> </span></span></code><span style="line-height:22px; color:rgb(0,0,0)">

<span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)">protected</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">void</span><span style="line-height:22px; color:rgb(0,0,0)"> beforeExecute</span><span style="line-height:22px; color:rgb(102,102,0)">(</span><span style="line-height:22px; color:rgb(102,0,102)">Thread</span><span style="line-height:22px; color:rgb(0,0,0)"> t</span><span style="line-height:22px; color:rgb(102,102,0)">,</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,0,102)">Runnable</span><span style="line-height:22px; color:rgb(0,0,0)"> r</span><span style="line-height:22px; color:rgb(102,102,0)">)</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(0,0,136)">super</span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,0)">beforeExecute</span><span style="line-height:22px; color:rgb(102,102,0)">(</span><span style="line-height:22px; color:rgb(0,0,0)">t</span><span style="line-height:22px; color:rgb(102,102,0)">,</span><span style="line-height:22px; color:rgb(0,0,0)"> r</span><span style="line-height:22px; color:rgb(102,102,0)">);</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  pauseLock</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,136)">lock</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(0,0,136)">try</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   </span></span><span style="line-height:22px; color:rgb(0,0,136)">while</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">(</span><span style="line-height:22px; color:rgb(0,0,0)">isPaused</span><span style="line-height:22px; color:rgb(102,102,0)">)</span><span style="line-height:22px; color:rgb(0,0,0)"> unpaused</span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,0)">await</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">catch</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">(</span><span style="line-height:22px; color:rgb(102,0,102)">InterruptedException</span><span style="line-height:22px; color:rgb(0,0,0)"> ie</span><span style="line-height:22px; color:rgb(102,102,0)">)</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   t</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,0)">interrupt</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">finally</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   pauseLock</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,0)">unlock</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)">

<span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)">public</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">void</span><span style="line-height:22px; color:rgb(0,0,0)"> pause</span><span style="line-height:22px; color:rgb(102,102,0)">()</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  pauseLock</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,136)">lock</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(0,0,136)">try</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   isPaused </span></span><span style="line-height:22px; color:rgb(102,102,0)">=</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">true</span><span style="line-height:22px; color:rgb(102,102,0)">;</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">finally</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   pauseLock</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,0)">unlock</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)">

<span class="pln"> </span></span><span style="line-height:22px; color:rgb(0,0,136)">public</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">void</span><span style="line-height:22px; color:rgb(0,0,0)"> resume</span><span style="line-height:22px; color:rgb(102,102,0)">()</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  pauseLock</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,136)">lock</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(0,0,136)">try</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   isPaused </span></span><span style="line-height:22px; color:rgb(102,102,0)">=</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">false</span><span style="line-height:22px; color:rgb(102,102,0)">;</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   unpaused</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,0)">signalAll</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(0,0,136)">finally</span><span style="line-height:22px; color:rgb(0,0,0)"> </span><span style="line-height:22px; color:rgb(102,102,0)">{</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">   pauseLock</span></span><span style="line-height:22px; color:rgb(102,102,0)">.</span><span style="line-height:22px; color:rgb(0,0,0)">unlock</span><span style="line-height:22px; color:rgb(102,102,0)">();</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln">  </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln"> </span></span><span style="line-height:22px; color:rgb(102,102,0)">}</span><span style="line-height:22px; color:rgb(0,0,0)">
<span class="pln"></span></span><span style="line-height:22px; color:rgb(102,102,0)">}}</span></pre>
<span style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr></wbr></span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">关于它的使用请参考《</span><span style="line-height:24px; font-size:16px; background-color:rgb(245,245,245); font-family:Arial,Helvetica,simsun,u5b8bu4f53; white-space:nowrap"><strong><a target="_blank" title="阅读全文" href="http://hubingforever.blog.163.com/blog/static/17104057920109544134947/" style="color:rgb(207,121,28); line-height:28px; text-decoration:none">ExecutorService</a></strong></span><span style="font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:28px; background-color:rgb(245,245,245)">》</span><div style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)"><wbr><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span><table id="nestedclasses" style="line-height:21px; margin:0px 0px 1em 1em; padding:0px; border:0px; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="margin:0px; padding:0px; border:0px">
<tr style="margin:0px; padding:0px; border:0px"><th colspan="12" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:rgb(222,232,241)">Nested
 Classes</th></tr>
<tr style="margin:0px; padding:0px; border:0px; background-color:rgb(246,246,246)">
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"><nobr>class</nobr></td>
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.AbortPolicy.html" style="color:rgb(0,102,153); text-decoration:none">ThreadPoolExecutor.AbortPolicy</a></td>
<td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit">A
 handler for rejected tasks that throws a<code style="line-height:1em; color:rgb(0,112,0)">RejectedExecutionException</code>.</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px">
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"><nobr>class</nobr></td>
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.CallerRunsPolicy.html" style="color:rgb(0,102,153); text-decoration:none">ThreadPoolExecutor.CallerRunsPolicy</a></td>
<td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit">A
 handler for rejected tasks that runs the rejected task directly in the calling thread of the<code style="line-height:1em; color:rgb(0,112,0)">execute</code>method, unless the executor has been shut down, in which case the task is discarded.</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px; background-color:rgb(246,246,246)">
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"><nobr>class</nobr></td>
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.DiscardOldestPolicy.html" style="color:rgb(0,102,153); text-decoration:none">ThreadPoolExecutor.DiscardOldestPolicy</a></td>
<td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit">A
 handler for rejected tasks that discards the oldest unhandled request and then retries<code style="line-height:1em; color:rgb(0,112,0)">execute</code>, unless the executor is shut down, in which case the task is discarded.</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px">
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"><nobr>class</nobr></td>
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.DiscardPolicy.html" style="color:rgb(0,102,153); text-decoration:none">ThreadPoolExecutor.DiscardPolicy</a></td>
<td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit">A
 handler for rejected tasks that silently discards the rejected task.</td>
</tr>
</tbody></table>
<strong>主要构造函数</strong><wbr>:<br><span style="color:#993300">public</span><span style="color:#ff00ff"><wbr>ThreadPoolExecutor</wbr></span><wbr>(intcorePoolSize,<br>intmaximumPoolSize,<br>longkeepAliveTime,<br>TimeUnitunit,<br><span style="color:#ff9900">BlockingQueue&lt;Runnable&gt;workQueue</span>)<br><br>用给定的初始参数和默认的线程工厂及被拒绝的执行处理程序创建新的ThreadPoolExecutor。<br>使用Executors工厂方法之一比使用此通用构造方法方便得多。<br>参数:<br>corePoolSize-池中所保存的线程数,包括空闲线程。<br>maximumPoolSize-池中允许的最大线程数。<br>keepAliveTime-当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。<br>unit-keepAliveTime参数的时间单位。<br>workQueue-执行前用于保持任务的队列。此队列仅保持由execute方法提交的Runnable任务。<br>抛出:<br>IllegalArgumentException-如果corePoolSize或keepAliveTime小于0,或者maximumPoolSize小于等于0,<br>或者corePoolSize大于maximumPoolSize。<br>NullPointerException-如果workQueue为null<br><span style="color:#993300">public</span><wbr><span style="color:#ff00ff">ThreadPoolExecutor</span><wbr>(intcorePoolSize,<br>intmaximumPoolSize,<br>longkeepAliveTime,<br>TimeUnitunit,<br><span style="color:#ff9900">BlockingQueue&lt;Runnable&gt;workQueue,</span><br>ThreadFactorythreadFactory)<br><br>用给定的初始参数和默认被拒绝的执行处理程序创建新的ThreadPoolExecutor。<br><br>参数:<br>corePoolSize-池中所保存的线程数,包括空闲线程。<br>maximumPoolSize-池中允许的最大线程数。<br>keepAliveTime-当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。<br>unit-keepAliveTime参数的时间单位。<br>workQueue-执行前用于保持任务的队列。此队列仅保持由execute方法提交的Runnable任务。<br>threadFactory-执行程序创建新线程时使用的工厂。<br>抛出:<br>IllegalArgumentException-如果corePoolSize或keepAliveTime小于0,或者maximumPoolSize小于等于0,或者corePoolSize大于maximumPoolSize。<br>NullPointerException-如果workQueue或threadFactory为null。<br><span style="color:#993300">public</span><span style="color:#ff00ff"><wbr>ThreadPoolExecutor</wbr></span><wbr>(intcorePoolSize,<br>intmaximumPoolSize,<br>longkeepAliveTime,<br>TimeUnitunit,<br>BlockingQueue&lt;Runnable&gt;workQueue,<br>RejectedExecutionHandlerhandler)<br><br>用给定的初始参数和默认的线程工厂创建新的ThreadPoolExecutor。<br><br>参数:<br>corePoolSize-池中所保存的线程数,包括空闲线程。<br>maximumPoolSize-池中允许的最大线程数。<br>keepAliveTime-当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。<br>unit-keepAliveTime参数的时间单位。<br>workQueue-执行前用于保持任务的队列。此队列仅由保持execute方法提交的Runnable任务。<br>handler-由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。<br>抛出:<br>IllegalArgumentException-如果corePoolSize或keepAliveTime小于0,或者maximumPoolSize小于等于0,<br>或者corePoolSize大于maximumPoolSize。</wbr></wbr></wbr></wbr></wbr></wbr></div>
<div style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(245,245,245)">
<strong>主要成员函数</strong><br><span style="color:#993300">publicvoid</span><span style="color:#ff6600"><wbr>execute</wbr></span><wbr>(Runnablecommand)<br>在将来某个时间执行给定任务。可以在新线程中或者在现有池线程中执行该任务。如果无法将任务提交执行,或者因为此执行程序已关闭,或者因为已达到其容量,则该任务由当前RejectedExecutionHandler处理。<br>参数:<br>command-要执行的任务。<br>抛出:<br>RejectedExecutionException-如果无法接收要执行的任务,则由RejectedExecutionHandler决定是否抛出RejectedExecutionException<br>NullPointerException-如果命令为null<br><span style="color:#993300">publicvoid</span><span style="color:#ff6600"><wbr>shutdown<wbr>()</wbr></wbr></span><br>按过去执行已提交任务的顺序发起一个有序的关闭,但是不接受新任务。如果已经关闭,则调用没有其他作用。<br>抛出:<br>SecurityException-如果安全管理器存在并且关闭此ExecutorService可能操作某些不允许调用者修改的线程(因为它没有RuntimePermission("modifyThread")),或者安全管理器的checkAccess方法拒绝访问。<br><span style="color:#993300">public</span>List&lt;Runnable&gt;<span style="color:#ff6600"><wbr>shutdownNow<wbr>()</wbr></wbr></span><br>尝试停止所有的活动执行任务、暂停等待任务的处理,并返回等待执行的任务列表。在从此方法返回的任务队列中排空(移除)这些任务。<br>并不保证能够停止正在处理的活动执行任务,但是会尽力尝试。此实现通过Thread.interrupt()取消任务,所以无法响应中断的任何任务可能永远无法终止。<br>返回:<br>从未开始执行的任务的列表。<br>抛出:<br>SecurityException-如果安全管理器存在并且关闭此ExecutorService<br>可能操作某些不允许调用者修改的线程(因为它没有RuntimePermission("modifyThread")),<br>或者安全管理器的checkAccess方法拒绝访问。<br><span style="color:#993300">publicint</span><span style="color:#ff6600">prestartAllCoreThreads()</span><br>启动所有核心线程,使其处于等待工作的空闲状态。仅当执行新任务时,此操作才重写默认的启动核心线程策略。<br>返回:<br>已启动的线程数<br><span style="color:#993300">publicboolean</span><wbr><span style="color:#ff6600">allowsCoreThreadTimeOut<wbr>()</wbr></span><br>如果此池允许核心线程超时和终止,如果在keepAlive时间内没有任务到达,新任务到达时正在替换(如果需要),则返回true。当返回true时,适用于非核心线程的相同的保持活动策略也同样适用于核心线程。当返回false(默认值)时,由于没有传入任务,核心线程不会终止。<br>返回:<br>如果允许核心线程超时,则返回true;否则返回false<br><span style="color:#993300">publicvoid</span><span style="color:#ff6600">allowCoreThreadTimeOut</span>(booleanvalue)<br>如果在保持活动时间内没有任务到达,新任务到达时正在替换(如果需要),则设置控制核心线程是超时还是终止的策略。当为false(默认值)时,由于没有传入任务,核心线程将永远不会中止。当为true时,适用于非核心线程的相同的保持活动策略也同样适用于核心线程。为了避免连续线程替换,保持活动时间在设置为true时必须大于0。通常应该在主动使用该池前调用此方法。<br>参数:<br>value-如果应该超时,则为true;否则为false<br>抛出:<br>IllegalArgumentException-如果value为true并且当前保持活动时间不大于0。<br><span style="color:#993300">publicboolean</span><span style="color:#ff6600"><strong></strong><wbr>remove</wbr></span><wbr>(Runnabletask)<br>从执行程序的内部队列中移除此任务(如果存在),从而如果尚未开始,则让其不再运行。<br>此方法可用作取消方案的一部分。它可能无法移除在放置到内部队列之前已经转换为其他形式的任务。<br>例如,使用submit输入的任务可能被转换为维护Future状态的形式。但是,在此情况下,purge()方法可用于移除那些已被取消的Future。<br>参数:<br>task-要移除的任务<br>返回:<br>如果已经移除任务,则返回true<br><span style="color:#993300">publicvoid</span><wbr><strong></strong><span style="color:#ff6600">purge</span><span style="color:#ff6600"><wbr>()</wbr></span><br>尝试从工作队列移除所有已取消的Future任务。此方法可用作存储回收操作,它对功能没有任何影响。<br>取消的任务不会再次执行,但是它们可能在工作队列中累积,直到worker线程主动将其移除。<br>调用此方法将<wbr><span style="color:#000080">试图立即移除它们</span><wbr>。但是,如果出现其他线程的干预,那么此方法移除任务将失败。<div>
<span style="color:#003366">当然它还实现了的</span><span style="color:rgb(255,153,0)">ExecutorService</span><span style="color:#003366">的</span><span style="color:#ff6600">submit</span><span style="color:#003366">系列接口</span>
</div>
<div>
<span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span><table id="pubmethods" style="line-height:21px; margin:0px 0px 1em 1em; padding:0px; border:0px; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="margin:0px; padding:0px; border:0px">
<tr style="margin:0px; padding:0px; border:0px">
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"><nobr>abstract
 &lt;T&gt;<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/Future.html" style="color:rgb(0,102,153); text-decoration:none">Future</a>&lt;T&gt;</nobr></td>
<td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit">
<nobr><span style="margin-right:2px"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ExecutorService.html#submit(java.lang.Runnable,%20T)" style="color:rgb(0,102,153); text-decoration:none">submit</a></span>(<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/lang/Runnable.html" style="color:rgb(0,102,153); text-decoration:none">Runnable</a>task,
 T result)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border:0px">Submits a Runnable task for execution and returns a Future representing that task.</div>
<div style="line-height:25px; margin:0px; padding:3px 1em 0px; border:0px">如果执行成功就返回<span style="color:#0000ff; line-height:21px">T
 result</span>
</div>
</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px; background-color:rgb(246,246,246)">
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"><nobr>abstract
 &lt;T&gt;<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/Future.html" style="color:rgb(0,102,153); text-decoration:none">Future</a>&lt;T&gt;</nobr></td>
<td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit">
<nobr><span style="margin-right:2px"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ExecutorService.html#submit(java.util.concurrent.Callable&lt;T&gt;)" style="color:rgb(0,102,153); text-decoration:none">submit</a></span>(<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/Callable.html" style="color:rgb(0,102,153); text-decoration:none">Callable</a>&lt;T&gt;
 task)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border:0px">Submits a value-returning task for execution and returns a Future representing the pending results of the task.</div>
</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px">
<td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"><nobr>abstract<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/Future.html" style="color:rgb(0,102,153); text-decoration:none">Future</a>&lt;?&gt;</nobr></td>
<td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit">
<nobr><span style="margin-right:2px"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ExecutorService.html#submit(java.lang.Runnable)" style="color:rgb(0,102,153); text-decoration:none">submit</a></span>(<a target="_blank" rel="nofollow" href="http://developer.android.com/reference/java/lang/Runnable.html" style="color:rgb(0,102,153); text-decoration:none">Runnable</a>task)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border:0px">Submits
 a Runnable task for execution and returns a Future representing that task.</div>
</td>
</tr>
</tbody></table>
</div></wbr></wbr></wbr></wbr></wbr></wbr>
</div>
<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></div>
<br></wbr></wbr></wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值