




已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
實(shí)驗(yàn)六 多線程基礎(chǔ)編程實(shí)驗(yàn)?zāi)康耐ㄟ^繼承線程類java.lang.Thread創(chuàng)建具有特定功能的線程類,通過實(shí)現(xiàn)接口java.lang.Runnable創(chuàng)建可作為線程運(yùn)行的類,創(chuàng)建線程類對(duì)象,啟動(dòng)線程,并觀察運(yùn)行、停止。創(chuàng)建實(shí)現(xiàn)了Runnable接口的線程類對(duì)象以后,啟動(dòng)線程,通過觀察運(yùn)行和停止,掌握線程類java.lang.Thread常用方法的使用,掌握對(duì)線程執(zhí)行過程中的異常的處理方法。實(shí)驗(yàn)要求編寫一個(gè)實(shí)現(xiàn)接口java.lang.Runnable的簡(jiǎn)單多線程應(yīng)用程序。實(shí)驗(yàn)內(nèi)容1. 使用java.lang.Thread創(chuàng)建具有特定功能的線程類;2. 創(chuàng)建以java.lang.Runnable為接口的線程類;3. 啟動(dòng)線程,并觀察運(yùn)行、停止。Exercise 1#:Write a program that displays the name of the thread that executes main.Exercise 2#: Creat two threads, one thread print ”A” and the other print “B” alternately. E.g. ABBABAABBA.etc.Exercise 3#: 隨便選擇兩個(gè)城市作為預(yù)選旅游目標(biāo)。實(shí)現(xiàn)兩個(gè)獨(dú)立的線程分別顯示10次城市名,每次顯示后休眠一段隨機(jī)時(shí)間(1000毫秒以內(nèi)),哪個(gè)城市先顯示完畢,就決定去哪個(gè)城市。分別用Runnable 接口和Thread類實(shí)現(xiàn)。 public class Testlvyou public static void main(String args) Thread t1=new SubThread(北京); Thread t2=new SubThread(海南); t1.start(); t2.start(); class SubThread extends Thread public SubThread(String s) super(s); public void run() for(int i=0;i10;i+) System.out.print(getName(); try sleep(int)(Math.random()*100); catch(InterruptedException e) e.printStackTrace(); System.out.println(去+getName(); 實(shí)驗(yàn)七 多線程并發(fā)編程實(shí)驗(yàn)?zāi)康恼莆帐褂藐P(guān)鍵字synchronized同步方法、使用關(guān)鍵字synchronized同步語句塊、和使用wait、notify(notifyAll)實(shí)現(xiàn)線程的通信,同時(shí),練習(xí)使用join實(shí)現(xiàn)線程的協(xié)作。實(shí)驗(yàn)要求在實(shí)驗(yàn)六的程序基礎(chǔ)上,編寫一個(gè)多線程并發(fā)的應(yīng)用程序。實(shí)驗(yàn)內(nèi)容1. 創(chuàng)建多個(gè)線程;2. 使用synchronized方法實(shí)現(xiàn)線程同步;3. 運(yùn)用wait、notify(notifyAll)實(shí)現(xiàn)線程的通信;4. 使用join實(shí)現(xiàn)線程的協(xié)作;Exercise 1#:Write a program that prints out the elapsed time each second from the start of execution, with another thread that prints a message every fifteen seconds. Have the message-printing thread be notified by the time-printing thread as each second passes by. Add another thread that prints a different message every seven seconds without modifying the time-printing thread.Exercise 2#:設(shè)計(jì)兩個(gè)線程,一個(gè)充當(dāng)電子表,每隔1秒在DOS窗口顯示下一系統(tǒng)時(shí)間;另一個(gè)充當(dāng)鬧鐘,每到整點(diǎn)就開始報(bào)時(shí),即在DOS窗口顯示5次整點(diǎn)提示,同時(shí)將第一個(gè)線程掛起,報(bào)時(shí)完畢再將第一個(gè)線程恢復(fù)運(yùn)行。實(shí)驗(yàn)步驟(1) 創(chuàng)建第一個(gè)類繼承Thread類用來顯示系統(tǒng)時(shí)間(2) 第一個(gè)類實(shí)現(xiàn)run()方法創(chuàng)建線程,線程體中每秒獲取一次系統(tǒng)時(shí)間并顯示。(3) 創(chuàng)建第二個(gè)類繼承Thread類用來實(shí)現(xiàn)鬧鐘功能(4) 第二個(gè)類實(shí)現(xiàn)run()方法創(chuàng)建線程,線程體中循環(huán)判斷當(dāng)前時(shí)間是否為整點(diǎn),若是則將第一個(gè)線程掛起,并每隔一秒輸出提示,提示完畢恢復(fù)第一個(gè)線程的執(zhí)行(5) 創(chuàng)建第三個(gè)類,包含main()方法,作為程序的執(zhí)行入口。import java.util.Date;class SubThread1 extends Thread public SubThread1(String s) super(s); public void run() Date date1=new Date(); try sleep(int)(Math.random()*100); catch(InterruptedException e) e.printStackTrace(); System.out.println(date1.toLocaleString(); class SubThread2 extends Thread public SubThread2(String s) super(s); public void run() Date date1=new Date(); try sleep(int)(Math.random()*30000); catch(InterruptedException e) e.printStackTrace(); System.out.println(date1.toLocaleString()+整點(diǎn)); public class
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 人工智能領(lǐng)域人才引進(jìn)措施
- 特殊學(xué)生遠(yuǎn)程教育幫扶措施
- 機(jī)電安裝施工進(jìn)度計(jì)劃和工期保證措施
- 2025高一下學(xué)期物理作業(yè)布置計(jì)劃
- 混凝土工程施工質(zhì)量驗(yàn)收措施
- 西師版五年級(jí)下冊(cè)數(shù)學(xué)學(xué)科競(jìng)賽計(jì)劃
- 2025幼兒園保教安全管理計(jì)劃
- 高校英語興趣小組比賽活動(dòng)計(jì)劃
- 中華師道視角的智慧校園建設(shè)范文
- 2025年職業(yè)技術(shù)院校教師培訓(xùn)計(jì)劃
- 2025年內(nèi)蒙古自治區(qū)中考數(shù)學(xué)真題試卷(含答案)
- CT增強(qiáng)掃描造影劑外滲的預(yù)防與處理
- 深靜脈置管的維護(hù)與護(hù)理
- Unit 2 Home Sweet Home 第6課時(shí)(Project Reading Plus) 2025-2026學(xué)年人教版英語八年級(jí)下冊(cè)
- 孤獨(dú)癥業(yè)務(wù)管理制度
- xx公司獎(jiǎng)金管理制度
- 勞務(wù)服務(wù)購(gòu)買協(xié)議書范本
- 2025-2030年中國(guó)生物醫(yī)學(xué)材料行業(yè)市場(chǎng)深度分析及發(fā)展前景與投資研究報(bào)告
- 2025年小學(xué)語文一年級(jí)下冊(cè)無紙筆測(cè)試題(小學(xué)一年級(jí)游園樂考無紙化檢測(cè))
- 2025至2030中國(guó)彈簧鋼行業(yè)產(chǎn)業(yè)運(yùn)行態(tài)勢(shì)及投資規(guī)劃深度研究報(bào)告
- 2025年地理中考時(shí)政熱點(diǎn)復(fù)習(xí)課件
評(píng)論
0/150
提交評(píng)論