2020最新Java线程池入门(超详细)
生活随笔
收集整理的這篇文章主要介紹了
2020最新Java线程池入门(超详细)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉 https://blog.csdn.net/weixin_43893397/article/details/104361154
【1】代碼示例?
/*** 線程池測試-自定義線程池創建方式* @since 2021/03/23 */ public class ThreadPoolMain2 {public static void main(String[] args) throws Exception {newMethod();}public static void newMethod() throws InterruptedException {/*** 開啟一個線程池 默認線程是10個* 使用默認線程工廠* 拒絕策略為CallerRunsPolicy策略,讓后面的線程先等待 */ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());//書寫一個循環 模擬100個用戶同時訪問for (int request = 0; request< 100;request ++){final int temp = request; //開啟一個線程threadPoolExecutor.execute(() ->{System.out.println("任務" + temp +"開始執行...");/*** 睡10秒 */try {Thread.sleep(1000L * 3);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("任務" + temp +"執行結束...");});}} }【2】 ThreadPoolExecutor 構造器解析
/*** Creates a new {@code ThreadPoolExecutor} with the given initial* parameters.** @param corePoolSize the number of threads to keep in the pool, even* if they are idle, unless {@code allowCoreThreadTimeOut} is set* @param maximumPoolSize the maximum number of threads to allow in the* pool* @param 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.* @param unit the time unit for the {@code keepAliveTime} argument* @param workQueue the queue to use for holding tasks before they are* executed. This queue will hold only the {@code Runnable}* tasks submitted by the {@code execute} method.* @param threadFactory the factory to use when the executor* creates a new thread* @param handler the handler to use when execution is blocked* because the thread bounds and queue capacities are reached* @throws IllegalArgumentException if one of the following holds:<br>* {@code corePoolSize < 0}<br>* {@code keepAliveTime < 0}<br>* {@code maximumPoolSize <= 0}<br>* {@code maximumPoolSize < corePoolSize}* @throws NullPointerException if {@code workQueue}* or {@code threadFactory} or {@code handler} is null*/public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) {【3】參數解析
| 序號 | 參數名 | 描述 | 中文 |
| 1 | corePoolSize | the number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set. | 除非設置了{@code allowCoreThreadTimeOut},否則即使它們處于空閑狀態也要保留在池中的線程數。 |
| 2 | maximumPoolSize | ?the maximum number of threads to allow in the pool | 池中允許的最大線程數 |
| 3 | 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. | 當線程數大于內核數時,這是多余的空閑線程將在終止之前等待新任務的最長時間。 |
| 4 | unit | the time unit for the {@code keepAliveTime} argument | {@code keepAliveTime}參數的時間單位 |
| 5 | workQueue | ?the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method. | 在執行任務之前用于保留任務的隊列。 此隊列將僅保存execute方法提交的Runnable任務。 |
| 6 | threadFactory | the factory to use when the executorcreates a new thread. | 執行程序創建新線程時要使用的工廠。 |
| 7 | handler | ?the handler to use when execution is blockedbecause the thread bounds and queue capacities are reached. | 當執行被阻塞時使用的處理程序,因為達到了線程界限和隊列容量。 |
【4】拋棄策略
// 當任務無法添加到阻塞隊列時的處理策略-直接拋出異常RejectedExecutionHandler rejectHandler1 = new ThreadPoolExecutor.AbortPolicy(); // -會調用當前線程池的所在的線程去執行被拒絕的任務。RejectedExecutionHandler rejectHandler2 = new ThreadPoolExecutor.CallerRunsPolicy(); // 會拋棄任務隊列中最舊的任務也就是最先加入隊列的,再把這個新任務添加進去。RejectedExecutionHandler rejectHandler3 = new ThreadPoolExecutor.DiscardOldestPolicy();// 會讓被線程池拒絕的任務直接拋棄,不會拋異常也不會執行。RejectedExecutionHandler rejectHandler4 = new ThreadPoolExecutor.DiscardPolicy(); RejectedExecutionHandler rejectHandler = rejectHandler4; ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),Executors.defaultThreadFactory(), rejectHandler);?
?
?
?
總結
以上是生活随笔為你收集整理的2020最新Java线程池入门(超详细)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 医美产品备案查询(医美备案查询)
- 下一篇: Java之Socket与HTTP区别