




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 班級:13科技2班 學號:201324131225 姓名:許耿寧 Java多線程和輸入輸出流一、實驗目的:1.熟悉利用Thread類建立多線程方法。2.熟悉利用Thread接口建立多線程方法。3.熟悉Java的文件讀寫機制,練習輸入輸出流的使用。二、實驗內(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)建三個線程,每個線程應當顯示它所運行的時間(可以考慮使用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)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 出租車司機培訓補貼及職業(yè)發(fā)展合同
- 編織袋行業(yè)市場調(diào)研與分析服務合同
- 保險市場占有率提升合同
- 車輛指標租賃與車輛購置稅代繳合同
- 老兵短線技術(shù)課件下載
- 翻身技巧護理課件
- 美術(shù)種類介紹課件
- 美術(shù)教師課件介紹
- 楚辭考試題及答案
- 美縫培訓課件圖片
- 《出生醫(yī)學證明》單親母親情況聲明
- PCS-915母差保護裝置介紹
- 第一套路面工程考試試題及答案
- GB∕T 22590-2021 軋鋼加熱爐用耐火澆注料
- 研發(fā)部程序文件bom管理
- 大件運輸管理制度
- 結(jié)構(gòu)化思維PPT通用課件
- 跨境電商模擬試題 (1)
- 意外受傷賠償協(xié)議書的格式
- PE管閉水試驗表
- 沈陽市內(nèi)中小學教育培訓機構(gòu)調(diào)查研究報告
評論
0/150
提交評論