實(shí)驗(yàn)十 多線程基礎(chǔ)編程阿.doc_第1頁(yè)
實(shí)驗(yàn)十 多線程基礎(chǔ)編程阿.doc_第2頁(yè)
實(shí)驗(yàn)十 多線程基礎(chǔ)編程阿.doc_第3頁(yè)
實(shí)驗(yàn)十 多線程基礎(chǔ)編程阿.doc_第4頁(yè)
實(shí)驗(yàn)十 多線程基礎(chǔ)編程阿.doc_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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)康耐ㄟ^(guò)繼承線程類java.lang.Thread創(chuàng)建具有特定功能的線程類,通過(guò)實(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)線程,通過(guò)觀察運(yùn)行和停止,掌握線程類java.lang.Thread常用方法的使用,掌握對(duì)線程執(zhí)行過(guò)程中的異常的處理方法。實(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同步語(yǔ)句塊、和使用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)就開(kāi)始報(bào)時(shí),即在DOS窗口顯示5次整點(diǎn)提示,同時(shí)將第一個(gè)線程掛起,報(bào)時(shí)完畢再將第一個(gè)線程恢復(fù)運(yùn)行。實(shí)驗(yàn)步驟(1) 創(chuàng)建第一個(gè)類繼承Thread類用來(lái)顯示系統(tǒng)時(shí)間(2) 第一個(gè)類實(shí)現(xiàn)run()方法創(chuàng)建線程,線程體中每秒獲取一次系統(tǒng)時(shí)間并顯示。(3) 創(chuàng)建第二個(gè)類繼承Thread類用來(lái)實(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論