




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、第十五講線程的管理一、線程的常用屬性:isalive屬性:獲収當前線程的執(zhí)行狀態(tài)。已啟動并且尚未正常終止,則為true;否則為false0 name屬性:獲取或設(shè)置線程的名稱。線程的命名static void main()thread.currentthread.name = "main"/將當前的線程命名為 mainthread worker = new thread(go);/創(chuàng)建一個線程 worker worker.name = "worker"將線程 worker 命名為 workerworker.start();/線程 worker 開始運行g(shù)
2、o(); 主線程開始運行)static void go()console.writeline(h 由"+thread.currentthread.name +"開始”);注意:線程的name屬性設(shè)置后不能修改。isbackground屬性:獲取或設(shè)置線程是否為后臺線程。前臺線程與后臺線程,默認的線程為前臺線程,主線程結(jié)束時,它創(chuàng)建的前臺線程不會 自動結(jié)束,一個應(yīng)用程序只要有一個線程沒有結(jié)束,應(yīng)用程序就不會結(jié)束。可將線程的屬性 改為true,將它設(shè)置為后臺線程,主線程結(jié)束時,它創(chuàng)建的后臺線程會自動結(jié)束。public partial class forml : formthre
3、ad t;public forml ()initializecomponent();checkforl1legalcrossthreadcalis 二false ; t 二 new threcid (go);t.isbackground = true;private void buttonlclick(object sender, eventargs e)if(t. tsalive =false )t. start 0 ;void go ()int i=0;while (true)i卄;label 1. text = i. tostringo ;private void button2_cli
4、ck(object sender, eventargs e)this. close ();priority屬性:獲取或設(shè)置線程的調(diào)度優(yōu)先級。highest (高)、abovenormal (高于一般)、normal (一般)、belownormal (低于一般)、lowesto (低) 高優(yōu)先級的線程通常會比一般優(yōu)先級的線程得到更多的cpu時間。低優(yōu)先級的線程在執(zhí)行時遇到了高優(yōu)先級的線程,它將讓出cpu給高優(yōu)先級的線程。 新創(chuàng)建的線程優(yōu)先級為一般優(yōu)先級。threadstate屈性:獲取當前線程的狀態(tài)。un started (未啟動)、running (運行)、waitsleepjoin (休眠
5、)、 suspendrequested (正被請求掛起)、suspended 狀態(tài)(掛起)、 abortrequested (正被請求終止)、stopped (已停止)。線程被創(chuàng)建時處于未啟動狀態(tài),調(diào)用線程對象的start。方法,可使該線程狀態(tài)變?yōu)檫\行 狀態(tài),線程被銷毀或者終止,處于己停止狀態(tài)。當前運行屮的線程調(diào)用sleep方法,將使z處于休眠狀態(tài)。線程將暫停運行,直到sleep 方法設(shè)左的時間到后,重新冋到running狀態(tài)。或者被其他的線程通過調(diào)用thread.intenupt ()或者thread.abort ()方法喚醒,重新回到running狀態(tài)。調(diào)用線程對象的suspend方法,可
6、使之處于掛起狀態(tài)。若調(diào)用掛起的線程的resume () 方法使其重新變?yōu)閞unning狀態(tài)。二、線程常用的方法:interrupt ():中斷處于waitsleepjoin狀態(tài)的線程;join ():阻塞調(diào)用線程,直到某個線程終止時或經(jīng)過了指定的時間為止;resume ():繼續(xù)已掛起的線程;sleep ():將當前線程暫停一段吋i'可,單位毫秒數(shù);注意:該方法為靜態(tài)成員。start ():啟動線程的執(zhí)行;suspend ():掛起線程;abort ():終止線程的狀態(tài)。三、join與sleep的區(qū)別:static void main()thread.sleep(o);/放棄 cpu
7、時間片thread.sleep(looo);/暫停 1000 毫秒thread.sleep(timespan.fromhours( 1);/ 暫停 1 小吋thread.sleep(timeout.infinite);/ 暫停到被打斷sleepo方法為thread類的靜態(tài)方法,調(diào)用thread.slcep(o)將當前線程進入休眠狀態(tài)。static void main()thread t = new thread(delegate() console.readline(); );t.start();t.join();/當前線程進入休眠狀態(tài)直到線程t結(jié)束 console.writeline(&qu
8、ot;thread trs readline complete!0);注意:join()方法為實例化方法。運行結(jié)果:等待用于輸入后,輸出。static void main()thread t = new thread(delegate() console.readline(); );t.start();t.join(iooo);/當前線程進入休眠狀態(tài)1000亳秒console.writeline(hthread trs readline complete!h);)運行結(jié)果:等待用于輸入后,輸出;若等待時間較長,則先輸出后再等待。static void main()thread t = new
9、thread(delegate() console.readline(););t.isbackground = true;t.start();t.join(looo);console.writeline(”thread tfs readline complete!”);運行結(jié)果:等待用于輸入后,輸出;若等待時間較長,則先輸汕后結(jié)束。四、線程的中斷與取消休眠狀態(tài)中的線程可以通過inteirupt方法中斷休眠,重新回到運行狀態(tài),但會拋出一個threadlnterruptedexception異常,用丿、'可以通過捕獲這個異常結(jié)束進程。static void main()thread t =
10、 new thread(p);t.start ();t.intenupt ();/'|* 斷線程 t)static void p()trythread.sleep(timeout.infinite);/ 暫停到被打斷catch (threadlnteituptedexception)console.write("強制執(zhí)行”);1console.writeline(h完成! ”);運行結(jié)果:強制執(zhí)行一個處于掛起狀態(tài)的線程也可以通過使用abort方法而強行釋放資源,但會拋出一個 threadabortexception異常,用戶可以通過捕獲這個異常結(jié)束進程。五. 管理線程thre
11、ad nowthroad;public form2()tnitiali zecomponent();checkfori11egalcrossthreadcal1s = false;nowthroad = now thread(do);public void do()for (int i = 0; ; i+)if (i > 10000) i = 0;labell. text = i. tostring();private void buttonl click(object sender, evontargs c)newthread. start ();private void button
12、2 click(object sender, eventargs e)private void button3 click (objectsender,eventargse)thread. sleep(5000);private void button4_click(objectsen der,eventargse)newthread. join(5000);private void buttons click(objectsender,eventargsnewthread suspend();private void button6_click(objectsender,eventargsn
13、ewthread. resume();存在的問題:未結(jié)束線程時,直接關(guān)閉窗口,會報錯。 原因是會出現(xiàn)線程訪問不存在的控件。六.跨線程訪問控件bool stop;thread newthread;delegate void delegateupdate(int i);public form2()in itializecomp on ent(); newthread = new thread(do);void update_label(int i)label 1 .text = i.tostringo;public void do()for (int i = 0; i+)if(i> 1000
14、0)1 = 0;delegateupdate updatelabel = new delegateupdate(update_label); label l.invoke (updatelabel,i);if (stop) break;private void button l_click(object sender, eventargs e)stop = false;newthread.start();private void button2_click(obj ect sender, eventargs e)stop = true;只有創(chuàng)建用戶控件的線程才能安全地訪問或更新這些控件,若在線
15、程內(nèi)部更新由其他線 程創(chuàng)建的控件,應(yīng)使用invoke方法,即暫停當前線程,在創(chuàng)建控件的線程上執(zhí)行指定的委 托,保證訪問控件時控件一定是存在的,從而實現(xiàn)線程i'可訪問控件的安全交互。public partial class forml : formthread newthread;delegate void update(int i);定義一個委托類型update up;定義一個委托類型的變量public form 1 () initializecomponent();/checkforlllegalcrossthreadcalls = false; newthread = new th
16、read(new threadstail(do); newthread.isbackground = true;up += new update(updatelabel);/將委托類型的變量指向方法定義一個方法,完成label 1的顯示void updatelabel(int i)label 1.text = i.tostringo;)public void do()for (int i = 0; i+)if(i> 10000)i = 0;object param = new objectfl i ;定義一個對象類的數(shù)組param,存入一個對象,對象的值為ilabell.invoke(u
17、p, param);在創(chuàng)建label 1的線程中執(zhí)行委托,傳遞參數(shù)/up(i);/執(zhí)行委托并傳遞參數(shù)private void button3_click(object sender, eventargs e)n ewthread.start();private void button6_click(object sender, eventargs e)newthread.abort();private void button 1 _click(object sender, eventargs e)n ewthread.suspe nd();private void button4_click(
18、object sender, eventargs e)newthread.resume();private void button2_click(object sender, eventargs e)thread.sleep(5000);private void button5_click(object sender, eventargs e)newthread.join();)七.線程同步共同完成某一個任務(wù)的多個線程之間,可以相互傳遞消息,實現(xiàn)同步。用于線程間同步的類如:monitor類,排他鎖常用方法:enter()進入臨界資源。exit()釋放臨界資源。wait()等待臨界資源。plus
19、e()喚醉等待臨界資源的進程。using system;using system collections generic;using systemlinq;using system .text;using system .threading;namespace consoleapplication7class zb/7帳木public int lp = 0; /領(lǐng)票publ ic int sp = 0; /售票public int syp = 0; /剩余class monitorsampleprivate zb zb = new zb();/新建一個帳本public void addcp0 領(lǐng)票方法monitoi'. enter (zb); 進入臨界區(qū)int i = zb. lp + 1;int j = zb.syp + 1;thread. sleep(50);zb. lp = i;zb. syp = j;賣出車票總賣出車票總console. wri telinec新領(lǐng)車票,領(lǐng)到車票總數(shù):"+ zb. lp. tostring () +數(shù):"+ zb. sp. tostring () + "剩余車票總數(shù)為:"+ zb. syp. tostringo);monitor. pulse (zb);/喚醒等待的進程moni
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 餐飲集團市場部經(jīng)理聘用合同
- 四年級數(shù)學(xué)(上)計算題專項練習(xí)及答案匯編
- 廣東理工職業(yè)學(xué)院《室內(nèi)家具設(shè)計》2023-2024學(xué)年第二學(xué)期期末試卷
- 基層醫(yī)療衛(wèi)生機構(gòu)信息化建設(shè)中的醫(yī)療信息化與醫(yī)療服務(wù)創(chuàng)新人才培養(yǎng)報告
- 2025年家居新零售線上線下融合模式下的智能家居產(chǎn)業(yè)商業(yè)模式報告
- 注塑模具項目可行性研究報告申請報告
- 裝配式建筑施工的分析報告與研究成果
- 新能源汽車輕量化的高強度鎂合金材料成型工藝研發(fā)與應(yīng)用可行性研究報告
- 高優(yōu)農(nóng)業(yè)技術(shù)示范基地項目可行性研究報告
- 吊裝安全管理制度
- 廣州市人力資源和社會保障局事業(yè)單位招聘工作人員【共500題附答案解析】模擬檢測試卷
- 產(chǎn)品定價和定價策略課程課件
- 鎂的理化性質(zhì)及危險特性表MSDS
- JC-MM-會計核算手冊模板(生產(chǎn)制造業(yè))V1
- 頂管工程施工組織設(shè)計方案
- 常用數(shù)學(xué)物理英語詞匯
- 2021年浙江省杭州市西湖區(qū)杭州綠城育華小學(xué)一級下冊期末數(shù)學(xué)試卷
- 國家儲備林改培外業(yè)調(diào)查技術(shù)
- 季節(jié)熱能儲存技術(shù)現(xiàn)狀
- T∕CNEA 001.1-2021 核能行業(yè)供應(yīng)商評價與管理規(guī)范 第1部分:合格供應(yīng)商要求及判定規(guī)則
- 貝朗CRRT操作常見報警及處理
評論
0/150
提交評論