




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
實(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的簡單多線程應(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)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 動(dòng)物世界的奧秘話題類作文(13篇)
- 初中化學(xué)實(shí)驗(yàn)操作安全教育
- 人力資源行業(yè)個(gè)人工資收入證明(5篇)
- 文化遺產(chǎn)保護(hù)與文旅產(chǎn)業(yè)協(xié)同發(fā)展路徑
- 能源行業(yè)專業(yè)技術(shù)資格證明書(6篇)
- 《全球氣候變化及其影響》地理教學(xué)教案
- 農(nóng)產(chǎn)品種植技術(shù)轉(zhuǎn)讓及使用合同
- DB15-T 2577-2022 草種質(zhì)資源普查技術(shù)規(guī)程
- 英語翻譯實(shí)踐與聽力練習(xí)題集
- 雙減政策的背景與實(shí)施初衷分析
- 電大《11872國際經(jīng)濟(jì)法》期末終考題庫及答案
- 《水泥砂漿地面找平》課件
- 貴州貴安發(fā)展集團(tuán)有限公司筆試
- 供應(yīng)鏈管理在電網(wǎng)企業(yè)物資管理中的應(yīng)用
- 《螺栓連接多層全裝配式混凝土墻板結(jié)構(gòu)技術(shù)規(guī)程》
- 輸血科內(nèi)部審核檢查表
- 【MOOC】物聯(lián)網(wǎng)工程專業(yè)綜合實(shí)踐-河海大學(xué) 中國大學(xué)慕課MOOC答案
- 八年級(jí)信息科技物聯(lián)網(wǎng)專項(xiàng)試題
- 健康管理中心服務(wù)規(guī)范制度
- 檢驗(yàn)檢查結(jié)果審核管理制度
- 浙江省杭州市2023-2024學(xué)年高一下學(xué)期期末教學(xué)質(zhì)量檢測政治試題
評(píng)論
0/150
提交評(píng)論