![JavaExecutor框架學習總結_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/eb94201d-3e21-4df3-81ec-9bce13cf8c81/eb94201d-3e21-4df3-81ec-9bce13cf8c811.gif)
![JavaExecutor框架學習總結_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/eb94201d-3e21-4df3-81ec-9bce13cf8c81/eb94201d-3e21-4df3-81ec-9bce13cf8c812.gif)
![JavaExecutor框架學習總結_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/eb94201d-3e21-4df3-81ec-9bce13cf8c81/eb94201d-3e21-4df3-81ec-9bce13cf8c813.gif)
![JavaExecutor框架學習總結_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/eb94201d-3e21-4df3-81ec-9bce13cf8c81/eb94201d-3e21-4df3-81ec-9bce13cf8c814.gif)
![JavaExecutor框架學習總結_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/eb94201d-3e21-4df3-81ec-9bce13cf8c81/eb94201d-3e21-4df3-81ec-9bce13cf8c815.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、大多數(shù)并發(fā)都是通過任務執(zhí)行的方式來實現(xiàn)的。一般有兩種方式執(zhí)行任務:串行和并行。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();當然上面的這兩種方式都是有問題的。單線程的問題就是并發(fā)量會是瓶頸,多線程版本就是 無限制的創(chuàng)建線程會導致資源不足問題。executor 框架任務是一組邏輯工作單元,而線程是使任務異步執(zhí)行的機制。jdk 提供
3、了 executor 接口:publie interface executor void execute(runnable command);雖然executor接口比較簡單,但是卻是異步任務執(zhí)行框架的基礎,該框架能支持多種不同 類型的任務執(zhí)行策略。它提供了一種標準的方式把任務的捉交過程與執(zhí)行過程進行了解耦。 用runnable來代表任務。executor的實現(xiàn)提供了對生命周期的支持以及統(tǒng)計信息應用程序 管理等機制。executor是基于生產(chǎn)者消費者模式的,提交任務的操作相當于生產(chǎn)者,執(zhí)行任務的線程相 當于消費?;趀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);另外可以白己實現(xiàn)executor來控制是并發(fā)還是并行的,如卜-面代碼:*執(zhí)行己提交的runnable任務的對象。*此接口提供一種將任務提交與毎個任務將如何運行的機制(包括線程使用的細節(jié)、調度等)分離開來的 方法。*通常使用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)建固定長度的線程池,每次提交任務創(chuàng)建一個線程,直到達到 線程池的最人數(shù)最,線程池的人小不再變化。 newsinglethreadexecutor° 單個線程池。 newcachedthreadpool。根據(jù)任務規(guī)模變動的線程池。 newscheduledthreadpoolo創(chuàng)建固定長度的線程池,以延遲或定時的方式來執(zhí)行任務。jvm只有在所冇非守護線程全部終止后才會退出,所以,如果無法正確的關閉executor, 那么jvm就無法結束。為了解決執(zhí)行服務的牛命周期問題,有個擴展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):運行、關閉、已終止。executorservice在初始創(chuàng)建 時處于運行狀態(tài)。shutdown方法會平緩關閉:不在接受新的任務,并且等待已經(jīng)執(zhí)行的任 務執(zhí)行完成(包括那些還未開始的任務)o shutdownnow方法將粗暴關閉:它將嘗試取消所冇 運行中的任務,并且不再啟動隊列中尚未開始的任務。所有任務都執(zhí)行完成后進入到已終止 狀態(tài)。callable 和 futureexecutor框架使用runnable作為基木的任務表示形式。runnable是一種有局限性的抽象, 它的run方法不能返回值和拋出一個受檢查異常。許多任務實際上是存在延時的計算,例如數(shù)據(jù)庫查詢,從網(wǎng)絡
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中標企業(yè)轉讓合同范本
- 供貨協(xié)議簡易合同范本
- 2025年度家政服務與綠色環(huán)保清潔產(chǎn)品供應合同
- 代購帶貨合同范例
- 催收協(xié)議合同范本
- 公司度業(yè)務合同范本
- 2025年度企業(yè)培訓與員工技能提升服務協(xié)議
- 應收會計個人工作總結
- 四年級下學期安全教育工作計劃
- 幼兒園第一學期教科研工作計劃
- 中國冰沙機行業(yè)市場現(xiàn)狀分析及競爭格局與投資發(fā)展研究報告2024-2029版
- 2024算力工廠建設指南白皮書-33正式版
- 2024年廣州市中考語文試卷真題(含官方答案)
- 2024年吉林省吉林市中考一模物理試題(解析版)
- CJT 290-2008 城鎮(zhèn)污水處理廠污泥處置 單獨焚燒用泥質
- 飛行員陸空通話(2)智慧樹知到期末考試答案章節(jié)答案2024年中國民航大學
- 內審員審核規(guī)則與技巧
- 預應力混凝土管樁(L21G404)
- Unit 2 Last weekend C Story time (教學設計)人教PEP版英語六年級下冊
- 圖解《匠心筑夢職啟未來》主題團日活動課件
- 2024年上海市普通高中學業(yè)水平等級性考試化學試卷(含答案)
評論
0/150
提交評論