



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 班級:13科技2班 學(xué)號:201324131225 姓名:許耿寧 Java多線程和輸入輸出流一、實驗?zāi)康模?.熟悉利用Thread類建立多線程方法。2.熟悉利用Thread接口建立多線程方法。3.熟悉Java的文件讀寫機制,練習(xí)輸入輸出流的使用。二、實驗內(nèi)容: 1.閱讀下列程序,分析并上機檢驗其功能。 public class DelayRunnable implements Runnableprivate static int count=0;private int no;private int delay;public DelayRunnable() count+; no=count;
2、public void run() try for (int i=0;i10;i+) delay=(int)(Math.random()*5000); Thread.sleep(delay); System.out.println(Thread +no+ with a delay +delay); catch(InterruptedException e)class MyRunnablepublic static void main(String args)DelayRunnable r1 = new DelayRunnable();DelayRunnable r2 = new DelayRu
3、nnable();Thread thread1=new Thread(r1);Thread thread2=new Thread(r2);thread1.start();thread2.start();tryThread.sleep(1000);catch(InterruptedException e)System.out.println(Thread wrong);2.將上列程序利用Runnable接口改寫,并上機檢驗。3.創(chuàng)建簡單的程序ThreeThread.java,該程序?qū)?chuàng)建三個線程,每個線程應(yīng)當(dāng)顯示它所運行的時間(可以考慮使用Date類或Calendar類)。4.鍵盤輸入10個整數(shù)
4、,從小到大進行排序。5.接收鍵盤輸入的字符串,用FileInputStream類將字符串寫入文件,用FileOutputStream類讀出文件內(nèi)容顯示在屏幕上。6.將一個文本文件的內(nèi)容按行讀出,每讀出一行就順序加上行號,并寫入到另一個文件中。三、實驗要求:1.通過實驗掌握Thread 、Runnable使用方法;2.程序必須能夠?qū)崿F(xiàn)多線程;3.程序必須能夠完成題目要求;4.通過實驗掌握文件輸入輸出流的使用方法;5.程序必須能夠從鍵盤接收字符串并保存在文件中;6.程序必須能夠讀出文件內(nèi)容顯示在屏幕上;7.寫出實驗報告。四、實驗代碼及截圖:第一題:在編譯器上運行程序得到截圖所示結(jié)果:第二題:實驗代
5、碼public class DelayThread extends Thread private static int count=0; private int no; private int delay; public DelayThread() count+; no=count; public void run() try for (int i=0;i10;i+) delay=(int)(Math.random()*5000); sleep(delay); System.out.println(Thread +no+ with a delay +delay); catch(Interrup
6、tedException e) class MyThread public static void main(String args) DelayThread thread1=new DelayThread(); DelayThread thread2=new DelayThread(); thread1.start(); thread2.start(); tryThread.sleep(1000);catch(InterruptedException e)System.out.println(Thread wrong); 實驗結(jié)果截圖:第三題:實驗代碼:public class MultiT
7、hread static Lock mylock; public static void main(String args) RunningObject1 r1 = new RunningObject1(); Thread t1 = new Thread(r1, t1); Thread t2 = new Thread(r1, t2); Thread t3 = new Thread(r1, t3); t1.start(); t3.start(); t2.start(); static class RunningObject1 implements Runnable public void run
8、() synchronized(this) String name=Thread.currentThread().getName(); System.out.println(name+開始時間:+Calendar.getInstance().getTimeInMillis(); for (int i = 0; i 100000000; i+) if (i = 9999999) System.out.println(name+結(jié)束時間:+Calendar.getInstance().getTimeInMillis(); break;實驗結(jié)果截圖:第四題:實驗代碼:public class Tes
9、t1 public static void main(String args) throws IOExceptionBufferedReader br=new BufferedReader(new InputStreamReader(System.in);String str;int array=new int10;for(int i=0;i10;i+)int a=i+1;System.out.println(請輸入第+a+整數(shù):);str=br.readLine();arrayi=Integer.parseInt(str);for(int j=0;j9;j+)for(int k=j+1;k1
10、0;k+)if(arraykarrayj)int temp=arrayk; arrayk=arrayj; arrayj=temp;for(int m=0;m10;m+)if(m!=9)System.out.print(arraym+,);elseSystem.out.print(arraym);實驗結(jié)果截圖:第五題:實驗代碼:public class Test2 public static void main(String args) try System.out.println(請輸入內(nèi)容:); File myfile = new File(save.txt); byte outCh = n
11、ew byte100; int bytes = System.in.read(outCh, 0, 100);/將文件讀入二進制數(shù)組中 FileOutputStream Fout = new FileOutputStream(myfile); Fout.write(outCh,0,bytes);/將數(shù)組中的字節(jié)輸入到這個流中 byte inCh = new bytebytes; FileInputStream Fin = new FileInputStream(myfile); Fin.read(inCh); System.out.println(讀取文件內(nèi)容:); System.out.pri
12、ntln(new String(inCh); catch (IOException e) System.out.println(e.toString(); 實驗結(jié)果截圖:第六題:實驗代碼:public class Test3 public static void main(String args) throws IOExceptionString str=null;FileReader file=new FileReader(F:/1.txt);FileWriter b=new FileWriter(F:/2.txt);BufferedWriter c=new BufferedWriter(b);LineNumberReader in=new LineNumberReader(file);while(str=in.readLine()!=null)System.out.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 員工代領(lǐng)工資協(xié)議書范本
- 商場租鋪面意向合同協(xié)議
- 民宿小院托管合同協(xié)議
- 總代理門票協(xié)議合同協(xié)議
- 商業(yè)保險工傷協(xié)議書模板
- 商會執(zhí)照轉(zhuǎn)讓協(xié)議書模板
- 快遞店合同協(xié)議
- 微信公眾號補充協(xié)議合同
- 員工工裝定制合同協(xié)議
- 2025設(shè)備儀器租賃合同范本
- 重大刑事案件緊急處置預(yù)案
- 《體育舞蹈》課件
- 高三一??偨Y(jié)主題班會課件
- 幼兒園優(yōu)質(zhì)公開課:中班語言繪本《明天我再來吃掉你》第二版課件
- 注塑APQP資料參考
- 類比法-創(chuàng)新思維
- DB4403T339-2023城市級實景三維數(shù)據(jù)規(guī)范
- 設(shè)備維護工程師簡歷
- 2023版押品考試題庫必考點含答案
- 口腔與健康智慧樹知到答案章節(jié)測試2023年溫州醫(yī)科大學(xué)
- 變電檢修技能考試計算
評論
0/150
提交評論