MainActivity如下:
package cc.vv; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 線程池(ThreadPoolExecutor)及其拒絕策略(RejectedExecutionHandler)使用示例 * * 工作原理: * 線程池的工作中主要涉及到:corePoolSize,workQueue,maximumPoolSize,RejectedExecutionHandler * 它們的調用原理: * 1 當線程池中線程數量小於corePoolSize則創建線程,並處理請求 * 2 當線程池中線程數量等於corePoolSize則把請求放入workQueue中,線程池中的的空閑線程就從workQueue中取任務並處理 * 3 當workQueue已滿存放不下新入的任務時則新建線程入池,並處理請求; * 如果線程池中線程數大於maximumPoolSize則用RejectedExecutionHandler使用一定的策略來做拒絕處理 * * 在該機制中還有一個keepAliveTime,文檔描述如下: * when the number of threads is greater than the core, * this is the maximum time that excess idle threads will wait for new tasks before terminating. * 它是什麼意思呢? * 比如線程池中一共有5個線程,其中3個為核心線程(core)其餘兩個為非核心線程. * 當超過一定時間(keepAliveTime)非核心線程仍然閑置(即沒有執行任務或者說沒有任務可執行)那麼該非核心線程就會被終止. * 即線程池中的非核心且空閑線程所能持續的最長時間,超過該時間後該線程被終止. * * * RejectedExecutionHandler的四種拒絕策略 * * hreadPoolExecutor.AbortPolicy: * 當線程池中的數量等於最大線程數時拋出java.util.concurrent.RejectedExecutionException異常. * 涉及到該異常的任務也不會被執行. * * ThreadPoolExecutor.CallerRunsPolicy(): * 當線程池中的數量等於最大線程數時,重試添加當前的任務;它會自動重復調用execute()方法 * * ThreadPoolExecutor.DiscardOldestPolicy(): * 當線程池中的數量等於最大線程數時,拋棄線程池中工作隊列頭部的任務(即等待時間最久Oldest的任務),並執行新傳入的任務 * * ThreadPoolExecutor.DiscardPolicy(): * 當線程池中的數量等於最大線程數時,丟棄不能執行的新加任務 * * 參考資料: * https://blog.csdn.net/cutesource/article/details/6061229 * https://blog.csdn.net/longeremmy/article/details/8231184 * https://blog.163.com/among_1985/blog/static/275005232012618849266/ * https://blog.csdn.net/longeremmy/article/details/8231184 * https://ifeve.com/java-threadpool/ * https://www.blogjava.net/xylz/archive/2010/07/08/325587.html * https://blog.csdn.net/ns_code/article/details/17465497 * Thank you very much * */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); testRejectedExecutionHandler(); } // 在此分別測試四種策略,替換ThreadPoolExecutor()方法最後一個參數即可. private void testRejectedExecutionHandler() { int produceTaskMaxNumber = 10; // 構造一個線程池 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new LinkedBlockingQueue(3), new ThreadPoolExecutor.DiscardPolicy()); for (int i = 1; i " + e.toString()); } } } private class RunnableImpl implements Runnable { private String taskName; private int consumerTaskSleepTime = 2000; RunnableImpl(String taskName) { this.taskName = taskName; } public void run() { System.out.println("開始 " + taskName); try { // 模擬耗時任務 Thread.sleep(consumerTaskSleepTime); } catch (Exception e) { e.printStackTrace(); } System.out.println("完成 " + taskName); } } }
main.xml如下: