實(shí)驗(yàn)報(bào)告五 多線程_第1頁(yè)
實(shí)驗(yàn)報(bào)告五 多線程_第2頁(yè)
實(shí)驗(yàn)報(bào)告五 多線程_第3頁(yè)
實(shí)驗(yàn)報(bào)告五 多線程_第4頁(yè)
實(shí)驗(yàn)報(bào)告五 多線程_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)序號(hào):5 日期: 2011 年 6 月 3 日班 級(jí)姓名學(xué)號(hào)實(shí)驗(yàn)項(xiàng)目名稱多線程學(xué)時(shí)指導(dǎo)教師實(shí)驗(yàn)?zāi)康模? 掌握線程的4種狀態(tài):新建、運(yùn)行、中斷、死亡。2 學(xué)習(xí)用Thread類創(chuàng)建線程,掌握哪些數(shù)據(jù)是線程之間共享的,哪些數(shù)據(jù)是線程獨(dú)有的。3 學(xué)習(xí)使用wait方法掛起線程的執(zhí)行,使用notifyAll()方法恢復(fù)線程的執(zhí)行。實(shí)驗(yàn)內(nèi)容:1完成實(shí)驗(yàn)五(一),補(bǔ)全下列程序中缺少的語(yǔ)句。程序1:編寫一個(gè)Java應(yīng)用程序,在主線程中再創(chuàng)建2個(gè)線程,要求線程經(jīng)歷4種狀態(tài):新建、運(yùn)行、中斷、死亡。程序2:編寫一個(gè)Java應(yīng)用程序,在主線程中用Thread類再創(chuàng)建2個(gè)線程,2個(gè)線程共享一

2、個(gè)int型的數(shù)據(jù),并各自有自己獨(dú)占的數(shù)據(jù)。程序3:通過單擊“開始”按鈕啟動(dòng)線程,該線程負(fù)責(zé)移動(dòng)一個(gè)紅色的標(biāo)簽。通過單擊“掛起”按鈕暫時(shí)中斷線程的執(zhí)行,單擊“恢復(fù)”按鈕恢復(fù)線程。通過單擊“終止”按鈕終止線程。2閱讀理解運(yùn)行實(shí)驗(yàn)五(二)。實(shí)驗(yàn)完成情況(一)、程序1:線程的4種狀態(tài)ThreadExample.javaclass Tortoise extends Thread int sleepTime=0,liveLength=0; Tortoise(int sleepTime,String name,int liveLength) this.sleepTime=sleepTime; this.li

3、veLength=liveLength; setName(name); public void run() while(true) liveLength-; System.out.print(""); try sleep(sleepTime);/讓線程調(diào)用 sleep方法進(jìn)入中斷狀態(tài),sleepTime毫秒后線程重新排隊(duì) / 等待CUP資源。 catch(InterruptedException e) if(liveLength<=0) System.out.print(getName()+"進(jìn)入死亡狀態(tài)n"); return;/結(jié)束run方法的語(yǔ)

4、句。 class Rabbit extends Thread int sleepTime=0,liveLength; Rabbit(int sleepTime,String name,int liveLength) this.sleepTime=sleepTime; this.liveLength=liveLength; setName(name); public void run() while(true) liveLength-; System.out.print("*"); try sleep(sleepTime);/讓線程調(diào)用 sleep方法進(jìn)入中斷狀態(tài),sleep

5、Time毫秒后線程重新排隊(duì) / 等待CUP資源。 catch(InterruptedException e) if(liveLength<=0) System.out.print(getName()+"進(jìn)入死亡狀態(tài)n"); return;/結(jié)束run方法的語(yǔ)句。 public class ThreadExample public static void main(String args ) Rabbit rabit; rabit=new Rabbit(500,"虛幻",5); /新建線程rabit。 Tortoise tortoise; torto

6、ise=new Tortoise(100,"現(xiàn)實(shí)",5); /新建線程tortoise tortoise.start(); /啟動(dòng)線程tortoise。 rabit.start(); /啟動(dòng)線程rabit。 實(shí)驗(yàn)結(jié)果:程序2:線程之間共享數(shù)據(jù)BankExample.javaclass Bank implements Runnable int money=100; /聲明一個(gè)int型變量money,初值為100。 Thread zhang,keven; Bank() zhang=new Thread(this); /創(chuàng)建zhang,Bank對(duì)象為zhang的目標(biāo)對(duì)象。 zha

7、ng.setName("會(huì)計(jì)"); keven=new Thread(this); /創(chuàng)建keven,Bank對(duì)象為keven的目標(biāo)對(duì)象。 keven.setName("出納"); public void run() int i=0; /聲明一個(gè)int型變量i,初值為0。 while(true) if(Thread.currentThread()=zhang) /判斷線程zhang是否正在占有CUP資源。 i=i+1; money=money+1; System.out.println(zhang.getName()+"將money的值改為&q

8、uot;+money); System.out.print(zhang.getName()+"的局部變量i="+i); if(i>=6) System.out.println(zhang.getName()+"線程進(jìn)入死亡狀態(tài)"); return; /使得線程zhang進(jìn)入死亡狀態(tài)。 try Thread.sleep(1000); catch(InterruptedException e) else if(Thread.currentThread()=keven) /判斷線程keven是否正在占有CUP資源。 i=i-1; money=money-

9、1; System.out.println(keven.getName()+"將money的值改為"+money); System.out.print(keven.getName()+"的局部變量i="+i); if(i<=-6) System.out.println(keven.getName()+"線程進(jìn)入死亡狀態(tài)"); return; /使得線程keven進(jìn)入死亡狀態(tài)。 try Thread.sleep(1000); catch(InterruptedException e) class BankExample publi

10、c static void main(String args ) Bank bank=new Bank(); bank.zhang.start(); bank.keven.start(); 實(shí)驗(yàn)結(jié)果:程序3:掛起、恢復(fù)和終止線程StartOrStop.javaimport java.awt.*;import java.awt.event.*;class Win extends Frame implements Runnable,ActionListener Thread moveOrStop; Button start,hang,resume,die; Label moveLabel; boo

11、lean move=false,dead=false; Win() moveOrStop=new Thread(this); /創(chuàng)建線程moveOrStop,當(dāng)前窗口為moveOrStop的目標(biāo)對(duì)象。 start=new Button("線程開始"); hang=new Button("線程掛起"); resume=new Button("線程恢復(fù)"); die=new Button("線程終止"); start.addActionListener(this); hang.addActionListener(thi

12、s); resume.addActionListener(this); die.addActionListener(this); moveLabel=new Label("線程負(fù)責(zé)運(yùn)動(dòng)我"); moveLabel.setBackground(Color.red); setLayout(new FlowLayout(); add(start); add(hang); add(resume); add(die); add(moveLabel); setSize(500,500); validate(); setVisible(true); addWindowListener(n

13、ew WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) if(e.getSource()=start) try move=true; moveOrStop.start(); /啟動(dòng)線程moveOrStop。 catch(Exception event) else if(e.getSource()=hang) move=false; else if(e.getSource()=resume) move=true

14、; resumeThread(); /調(diào)用resumeThread()方法恢復(fù)線程。 else if(e.getSource()=die) dead=true; public void run() while(true) while(!move) try hangThread(); /調(diào)用hangThread()方法掛起線程。 catch(InterruptedException e1) int x=moveLabel.getBounds().x; int y=moveLabel.getBounds().y; y=y+2; if(y>=200) y=10; moveLabel.setLocation(x,y); try moveOrStop.sleep(200); catch(InterruptedException e2) if(dead=true) return; /結(jié)束run()方法終止線程。 public synchronized void hangThread() throws InterruptedException wait(); /調(diào)用wait()方

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論