實驗十 多線程基礎編程阿.doc_第1頁
實驗十 多線程基礎編程阿.doc_第2頁
實驗十 多線程基礎編程阿.doc_第3頁
實驗十 多線程基礎編程阿.doc_第4頁
實驗十 多線程基礎編程阿.doc_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

實驗六 多線程基礎編程實驗目的通過繼承線程類java.lang.Thread創(chuàng)建具有特定功能的線程類,通過實現(xiàn)接口java.lang.Runnable創(chuàng)建可作為線程運行的類,創(chuàng)建線程類對象,啟動線程,并觀察運行、停止。創(chuàng)建實現(xiàn)了Runnable接口的線程類對象以后,啟動線程,通過觀察運行和停止,掌握線程類java.lang.Thread常用方法的使用,掌握對線程執(zhí)行過程中的異常的處理方法。實驗要求編寫一個實現(xiàn)接口java.lang.Runnable的簡單多線程應用程序。實驗內(nèi)容1. 使用java.lang.Thread創(chuàng)建具有特定功能的線程類;2. 創(chuàng)建以java.lang.Runnable為接口的線程類;3. 啟動線程,并觀察運行、停止。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#: 隨便選擇兩個城市作為預選旅游目標。實現(xiàn)兩個獨立的線程分別顯示10次城市名,每次顯示后休眠一段隨機時間(1000毫秒以內(nèi)),哪個城市先顯示完畢,就決定去哪個城市。分別用Runnable 接口和Thread類實現(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(); 實驗七 多線程并發(fā)編程實驗目的掌握使用關鍵字synchronized同步方法、使用關鍵字synchronized同步語句塊、和使用wait、notify(notifyAll)實現(xiàn)線程的通信,同時,練習使用join實現(xiàn)線程的協(xié)作。實驗要求在實驗六的程序基礎上,編寫一個多線程并發(fā)的應用程序。實驗內(nèi)容1. 創(chuàng)建多個線程;2. 使用synchronized方法實現(xiàn)線程同步;3. 運用wait、notify(notifyAll)實現(xiàn)線程的通信;4. 使用join實現(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#:設計兩個線程,一個充當電子表,每隔1秒在DOS窗口顯示下一系統(tǒng)時間;另一個充當鬧鐘,每到整點就開始報時,即在DOS窗口顯示5次整點提示,同時將第一個線程掛起,報時完畢再將第一個線程恢復運行。實驗步驟(1) 創(chuàng)建第一個類繼承Thread類用來顯示系統(tǒng)時間(2) 第一個類實現(xiàn)run()方法創(chuàng)建線程,線程體中每秒獲取一次系統(tǒng)時間并顯示。(3) 創(chuàng)建第二個類繼承Thread類用來實現(xiàn)鬧鐘功能(4) 第二個類實現(xiàn)run()方法創(chuàng)建線程,線程體中循環(huán)判斷當前時間是否為整點,若是則將第一個線程掛起,并每隔一秒輸出提示,提示完畢恢復第一個線程的執(zhí)行(5) 創(chuàng)建第三個類,包含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()+整點); public class

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論