java線程池核心線程不被摧毀的原理及分析
為什么要用線程池?
- 減少了創(chuàng)建和銷毀線程的次數(shù),每個(gè)工作線程都可以被重復(fù)利用
- 可以根據(jù)系統(tǒng)的承受能力,調(diào)整線程池中工作線線程的數(shù)目,防止因線程過多消耗內(nèi)存,也避免了因線程過少,浪費(fèi)系統(tǒng)資源
如何做到每個(gè)工作線程都可以被重復(fù)利用呢?
先看下線程池的工作原理:
原理如上圖,線程池有七個(gè)核心參數(shù)
corePoolSize
線程池核心線程數(shù)maximumPoolSize
線程池最大線程數(shù)量keepAliveTime
空閑線程存活時(shí)間unit
空閑線程存活時(shí)間單位workQueue
工作隊(duì)列threadFactory
線程工廠handler
拒絕策略
線程池之所以能做到重復(fù)利用,是因?yàn)榫€程池的核心線程不會(huì)被摧毀,執(zhí)行完任務(wù)后會(huì)重復(fù)利用
線程池是如何保持核心線程不被摧毀呢?
首先看先線程池是如何處理任務(wù)的,如下圖
下面我們看下核心部分源碼:
- 當(dāng)有一個(gè)任務(wù)添加進(jìn)來時(shí),線程池會(huì)創(chuàng)建一個(gè)Worker,Worker是實(shí)現(xiàn)Runnable方法的,所以Worker執(zhí)行時(shí)會(huì)調(diào)用run方法,run方法會(huì)接著調(diào)用runWoker方法
- 主要看這一行代碼,調(diào)用getTask方法獲取任務(wù)并且執(zhí)行 while (task != null || (task = getTask()) != null)
final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // allow interrupts boolean completedAbruptly = true; try { // 主要看這一行代碼,調(diào)用getTask方法獲取任務(wù)并且執(zhí)行 while (task != null || (task = getTask()) != null) { w.lock(); // If pool is stopping, ensure thread is interrupted; // if not, ensure thread is not interrupted. This // requires a recheck in second case to deal with // shutdownNow race while clearing interrupt if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))) && !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown = null; try { task.run(); } catch (RuntimeException x) { thrown = x; throw x; } catch (Error x) { thrown = x; throw x; } catch (Throwable x) { thrown = x; throw new Error(x); } finally { afterExecute(task, thrown); } } finally { task = null; w.completedTasks++; w.unlock(); } } completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); } }
- 看下getTask方法是如何實(shí)現(xiàn)的
private Runnable getTask() { boolean timedOut = false; // Did the last poll() time out? for (;;) { int c = ctl.get(); int rs = runStateOf(c); // Check if queue empty only if necessary. if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) { decrementWorkerCount(); return null; } int wc = workerCountOf(c); // Are workers subject to culling? boolean timed = allowCoreThreadTimeOut || wc > corePoolSize; if ((wc > maximumPoolSize || (timed && timedOut)) && (wc > 1 || workQueue.isEmpty())) { if (compareAndDecrementWorkerCount(c)) return null; continue; } try { Runnable r = timed ? workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : workQueue.take(); if (r != null) return r; timedOut = true; } catch (InterruptedException retry) { timedOut = false; } } }
- 主要看這幾行代碼
- getTask方法通過調(diào)用任務(wù)隊(duì)列的take方法,不斷的獲取線程
- 如果任務(wù)隊(duì)列里面數(shù)量為0,則會(huì)一直阻塞,一直等到有任務(wù)加入,從而保證了核心線程不被摧毀
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA中使用FileWriter寫數(shù)據(jù)到文本文件步驟詳解
這篇文章主要介紹了JAVA中使用FileWriter寫數(shù)據(jù)到文本文件步驟詳解,FileWriter類提供了多種寫入字符的方法,包括寫入單個(gè)字符、寫入字符數(shù)組和寫入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下2023-10-10Socket結(jié)合線程池使用實(shí)現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來實(shí)現(xiàn)客戶端和服務(wù)端通信實(shí)戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
這篇文章主要介紹了詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(21)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07springboot處理url中帶斜杠/\字符的參數(shù)報(bào)400問題
這篇文章主要介紹了springboot處理url中帶斜杠/\字符的參數(shù)報(bào)400問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01SpringCloud 服務(wù)注冊(cè)IP錯(cuò)誤的解決
這篇文章主要介紹了SpringCloud 服務(wù)注冊(cè)IP錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07