




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、大多數(shù)并發(fā)都是通過任務(wù)執(zhí)行的方式來實(shí)現(xiàn)的。一般有兩種方式執(zhí)行任務(wù):串行和并行。class singlethreadwebserver public static void main(string args) throws exception serversocket socket = new serversocket(80);while(true) socket conn = socket.accept (); handlerequest(conn);class threadpertaskwebserver public static void main(string args) throws
2、 exception serversocket socket = new serversocket(80);while(true) final socket conn = socket.accept ();runnable task = new runnable() public void run() handlerequest(conn);;new thread(task)start();當(dāng)然上面的這兩種方式都是有問題的。單線程的問題就是并發(fā)量會是瓶頸,多線程版本就是 無限制的創(chuàng)建線程會導(dǎo)致資源不足問題。executor 框架任務(wù)是一組邏輯工作單元,而線程是使任務(wù)異步執(zhí)行的機(jī)制。jdk 提供
3、了 executor 接口:publie interface executor void execute(runnable command);雖然executor接口比較簡單,但是卻是異步任務(wù)執(zhí)行框架的基礎(chǔ),該框架能支持多種不同 類型的任務(wù)執(zhí)行策略。它提供了一種標(biāo)準(zhǔn)的方式把任務(wù)的捉交過程與執(zhí)行過程進(jìn)行了解耦。 用runnable來代表任務(wù)。executor的實(shí)現(xiàn)提供了對生命周期的支持以及統(tǒng)計信息應(yīng)用程序 管理等機(jī)制。executor是基于生產(chǎn)者消費(fèi)者模式的,提交任務(wù)的操作相當(dāng)于生產(chǎn)者,執(zhí)行任務(wù)的線程相 當(dāng)于消費(fèi)?;趀xecutor的 webserver例子如卜:publie class ta
4、skexecutorwebserver private static final int nthreads = 100;private static final executor exec = executors.newfixedthreadpool(nthread s);public static void main(string args) throws exception serversocket serversocket = new serversocket(80);while (true) final socket conn = serversocket accept ();runn
5、able task = new runnable() ©override public void run() handlerequest(conn);;exec.executetask);另外可以白己實(shí)現(xiàn)executor來控制是并發(fā)還是并行的,如卜-面代碼:*執(zhí)行己提交的runnable任務(wù)的對象。*此接口提供一種將任務(wù)提交與毎個任務(wù)將如何運(yùn)行的機(jī)制(包括線程使用的細(xì)節(jié)、調(diào)度等)分離開來的 方法。*通常使用executor而不是顯式地創(chuàng)建線程。* author renchunxiao*/publie class executordemo public static void main
6、(string args) executor executor = new threadexecutor ();executorexecute(new runnable() ©overridepublic void run ()/ do something);executor executor2 = new serialexecutor(); executor2 execute(new runnable() ©overridepublic void run() / do something);/ * *創(chuàng)建i個線程來執(zhí)行command* author renchunxiao
7、*/class threadexecutor implements executor ©overridepublic void executerunnable command) new thread(command)start();/ * *串行執(zhí)行command* author renchunxiao* /class serialexecutor implements executor ©overridepublic void execute(runnable command) command run();線程池線程池就是線程的資源池,可以通過executors屮的靜態(tài)工
8、廠方法來創(chuàng)建線程池。 newfixedthreadpoou創(chuàng)建固定長度的線程池,每次提交任務(wù)創(chuàng)建一個線程,直到達(dá)到 線程池的最人數(shù)最,線程池的人小不再變化。 newsinglethreadexecutor° 單個線程池。 newcachedthreadpool。根據(jù)任務(wù)規(guī)模變動的線程池。 newscheduledthreadpoolo創(chuàng)建固定長度的線程池,以延遲或定時的方式來執(zhí)行任務(wù)。jvm只有在所冇非守護(hù)線程全部終止后才會退出,所以,如果無法正確的關(guān)閉executor, 那么jvm就無法結(jié)束。為了解決執(zhí)行服務(wù)的牛命周期問題,有個擴(kuò)展executor接口的新接口 executorse
9、rviceopublic interface executorservice extends executor void shutdown();list<runnable> shutdownnow();boolean isshutdown();boolean isterminated ();boolean awaittermination(long timeout, timeunit unit)throws interruptedexception;<t> future<t> submit(callable<t> task);<t>
10、future<t> submit(runnable task, t result);future<?> submit(runnable task);<t> list<future<t>> invokeallcollection<? www.babal20comextends callable<t>> tasks)throws interruptedexception;<t> list<future<t>> invokeallcollection<? extends ca
11、llable<t>> tasksz long timeout, timeunit unit)throws interruptedexception;<t> t invokeany(collection<? extends callatasks)throws interruptedexception, executionexception;<t> t invokeany(collection<? extends callable<t>> tasks,long timeoutz timeunit unit)throws int
12、erruptedexception, executionexceptionz timeoutexception;executorservice生命周期有三種狀態(tài):運(yùn)行、關(guān)閉、已終止。executorservice在初始創(chuàng)建 時處于運(yùn)行狀態(tài)。shutdown方法會平緩關(guān)閉:不在接受新的任務(wù),并且等待已經(jīng)執(zhí)行的任 務(wù)執(zhí)行完成(包括那些還未開始的任務(wù))o shutdownnow方法將粗暴關(guān)閉:它將嘗試取消所冇 運(yùn)行中的任務(wù),并且不再啟動隊列中尚未開始的任務(wù)。所有任務(wù)都執(zhí)行完成后進(jìn)入到已終止 狀態(tài)。callable 和 futureexecutor框架使用runnable作為基木的任務(wù)表示形式。runnable是一種有局限性的抽象, 它的run方法不能返回值和拋出一個受檢查異常。許多任務(wù)實(shí)際上是存在延時的計算,例如數(shù)據(jù)庫查詢,從網(wǎng)絡(luò)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國圣誕老人提手四方籃市場調(diào)查研究報告
- 2025年中國衛(wèi)星式家庭影院市場調(diào)查研究報告
- 28 海的女兒 教學(xué)設(shè)計-2023-2024學(xué)年語文四年級下冊統(tǒng)編版
- 2024-2025學(xué)年高中政治課時分層作業(yè)7幸福生活中的家庭美德含解析新人教版選修6
- 虛擬同步機(jī) 第2部分:電力儲能虛擬同步機(jī) 編制說明
- 滬教版高中信息技術(shù)必修 第三章第1節(jié) 信息的加工1.5聲音信息的加工 教學(xué)設(shè)計
- 第三單元 秦漢時期:統(tǒng)一多民族國家的建立和鞏固 教學(xué)設(shè)計 2023-2024學(xué)年部編版七年級歷史上學(xué)期
- 2025年WBC水性汽車金屬閃光涂料項目合作計劃書
- 第二單元千米和噸(教學(xué)設(shè)計)-2023-2024學(xué)年三年級下冊數(shù)學(xué)蘇教版
- 粵教版 信息技術(shù) 必修4.1 編制計算機(jī)程序解決實(shí)際問題 教學(xué)設(shè)計
- 《個人所得稅征管問題及對策研究》
- 江蘇省中小學(xué)生金鑰匙科技競賽(初中組)考試題及答案
- 生活垃圾我知道(課件)二年級下冊勞動
- 2020-2024年五年高考?xì)v史真題分類匯編(全國)專題14 中國古代史(非選擇題)(原卷版)
- 事業(yè)單位考試職業(yè)能力傾向測驗(yàn)(醫(yī)療衛(wèi)生類E類)試卷及答案指導(dǎo)
- 每日系列-計算小紙條-3年級下冊
- JGJT46-2024《施工現(xiàn)場臨時用電安全技術(shù)標(biāo)準(zhǔn)》條文解讀
- 2024年廣西區(qū)公務(wù)員考試《行測》真題及答案解析
- 第二單元 社會主義制度的建立與社會主義建設(shè)的探索(單元解讀)- 八年級歷史下冊同步備課系列
- 闌尾炎的護(hù)理查房腹腔鏡
- 大學(xué)輔導(dǎo)員崗位考核參考指標(biāo)
評論
0/150
提交評論