




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、1班級:13科技2班 學(xué)號:201324131225:許耿寧Java多線程和輸入輸出流一、實(shí)驗(yàn)?zāi)康模?.熟悉利用 Thread 類建立多線程方法。2.熟悉利用 Thread 接口建立多線程方法。3.熟悉 Java 的文件讀寫機(jī)制,練習(xí)輸入輸出流的使用。二、實(shí)驗(yàn)容:1.閱讀下列程序,分析并上機(jī)檢驗(yàn)其功能。 public class DelayRunnableimplements Runnable private static int count=0;private int no;private int delay;public DelayRunnable()count+; no=count;pu
2、blic void run()tryfor (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 DelayRunnabl
3、e();Thread thread1=new Thread(r1);Thread thread2=new Thread(r2);thread1.start();2thread2.start();tryThread.sleep(1000);catch(InterruptedException e)System.out.println(Thread wrong);2. 將上列程序利用 Runnable 接口改寫,并上機(jī)檢驗(yàn)。3. 創(chuàng)建簡單的程序 ThreeThread.java ,該程序?qū)?chuàng)建三個線程, 每個線程應(yīng) 當(dāng)顯示它所運(yùn)行的時間(可以考慮使用 Date 類或 Calendar 類)。4.
4、鍵盤輸入 10 個整數(shù),從小到大進(jìn)行排序。5. 接收鍵盤輸入的字符串,用 FileInputStream 類將字符串寫入文件,用FileOutputStream 類讀出文件容顯示在屏幕上。6. 將一個文本文件的容按行讀出,每讀出一行就順序加上行號,并寫入到另一個文件中。三、實(shí)驗(yàn)要求:1. 通過實(shí)驗(yàn)掌握 Thread 、 Runnable 使用方法;2. 程序必須能夠?qū)崿F(xiàn)多線程;3. 程序必須能夠完成題目要求;4. 通過實(shí)驗(yàn)掌握文件輸入輸出流的使用方法;5. 程序必須能夠從鍵盤接收字符串并保存在文件中;6. 程序必須能夠讀出文件容顯示在屏幕上;7. 寫出實(shí)驗(yàn)報告。四、實(shí)驗(yàn)代碼及截圖: 第一題:
5、在編譯器上運(yùn)行程序得到截圖所示結(jié)果:3terminMyRunnable Java ApplieationJ D:Js;Thread1 with a delayThread2wtha delayThread2with白delayThread1witha delayThread2 witha delayThread1witha delayThre白d 2witha del白yThread1withdLdelayThread2witha delayThread1witha delayThread2 with a delayThread2with a delayThread2 with a delyT
6、hread1witha delayThread1witha delayThread2witha delayThread1witha delayThread2withdelayThread1with a delayThread1witha delay32873870353148724491416461132684200220811015851681274312303400236023984810298第二題:實(shí)驗(yàn)代碼public class DelayThread exte nds Threadprivate static int coun t=0;private int no;private
7、int delay;public DelayThread()coun t+;no=co unt;public void run()tryfor (i nt i=0;i10;i+)delay=(i nt)(Math.ra ndom()*5000);sleep(delay);System.out.pri ntl n(Thread +no+ with a delay+delay); catch(I nterruptedExcepti on e)4class MyThreadpublic static void main( Stri ng args) DelayThread thread 仁 new
8、DelayThread();DelayThread thread2=new DelayThread(); thread1.start();thread2.start();tryThread.sleep(1000);catch(l nterruptedExcepti on e) System.out.pri ntl n(Thread wron g);實(shí)驗(yàn)結(jié)果截圖: MyTliread Java Applkaticn D:JavajThread 1 with adelay720Thread 2評ith a delay2041Thread 1 with adelay2560Thread 2 with
9、 adelay 4080Thread 1 with adelay3169Thread 1 with adelay490Thread 2 with adelay960Thread 1 with adelay 960Thread 2 with adelay1179Thread 1 with adelay 410Thread 2 with adelay80 CThread 1 with adelay3415Thread 2叩ith a delay4053Thread 1 with adely3159Thread 2 with adelay2476Thread 1 with日delay1361Thre
10、ad 2 with adelay3926Thread 1 with adelay2845Thread 2 with adelay1052Thread 2 with adelay 422第三題:實(shí)驗(yàn)代碼:public class MultiThread static Lock mylock;public static void main( Stri ng args) Runnin gObject1 r1 = new Runnin gObject1();Thread t1 = new Thread(r1, t1);Thread t2 = new Thread(r1, t2);Thread t3 =
11、 new Thread(r1, t3);t1.start();t3.start();5t2.start();static class RunningObjectl implements Runnable public void run() syn chro nized(this)Stri ng n ame=Thread.curre ntThread().getName();System.out.pri ntln(n ame+開始日寸間:+Cale ndar.getl nsta nce().getTimel nMillis();for (int i = 0; i MultrThread JJav
12、a Applicdtion DiJavdjreL9.0_9rbintl幵始時I環(huán)1466407635147=結(jié)束時間4466407635200t2ff始時間:L46640763520014664076352211466407635222結(jié)莉寸間:1466407635227第四題:實(shí)驗(yàn)代碼:public class Test1 public static void main(String args) throws IOExceptionBufferedReaderbr=newBufferedReader( newIn putStreamReader(System.i n);Stri ng str
13、;int array=new in t10;246for(int i=0;i10;i+)int a=i+1;System.out.println(請輸入第+a+整數(shù):);str=br.readL in e();arrayi=l nteger.parsel nt(str);for(i nt j=0;j9;j+)for(int k=j+1;k10;k+)if(arraykarrayj)int temp=arrayk;arrayk=arrayj; arrayj=temp;for(i nt m=0;m10;m+)if(m!=9) System.out.pri nt(arraym+,);elseSyst
14、em.out.pri nt(arraym);實(shí)驗(yàn)結(jié)果截圖:termi n dte-destl刖Appiic腫on U;y| rel,b inj vaJ.Vi口I請諭入第l整情諭入第2整數(shù):請輸入第3整數(shù):請輸入第4整數(shù):34請諭入第5整數(shù):請輸入第6整數(shù):請輸入第7龜數(shù):66請輸入第g整數(shù):99請輸入第9整數(shù):744請輸入策10整數(shù):201,13,20,22,24,34,44,53,6r99第五題:實(shí)驗(yàn)代碼: public class Test2 public static void main( Stri ng args) try System.out.println(”請輸入容:);File
15、 myfile = new File(save.txt);byte outCh = new byte100;int bytes = System.i n.read(outCh, 0, 100);/將 文件讀入二進(jìn)制數(shù)組中FileOutputStream Fout = new FileOutputStream(myfile);Fout.write(outCh,0,bytes);/將數(shù)組中的字節(jié)輸入到這個流中 byte inCh= new bytebytes;File In putStream Fin = new File In putStream(myfile);Fin. read(i nCh)
16、;System.out.pri ntln(讀取文件容:);System.out.pri ntln(n ew Strin g(i nCh); catch (IOExcepti on e) System.out.pri ntl n( e.toStri ng();實(shí)驗(yàn)結(jié)果截圖:utermi門曰tedn T皿2 (1 Java Application D:Jdvajre 1.3.1 binjavaw.exe 20166冃20日 請輸入內(nèi)容:好好學(xué)習(xí),天天向上快取文件內(nèi)容;好好學(xué)習(xí),天天問上文件(F)務(wù)任格式 grt(V)亀旬(H好好學(xué)習(xí),天天向上第六題:實(shí)驗(yàn)代碼:public class Test3
17、public static void main(String args) throws lOException Stri ng str=n ull;FileReader file=new FileReader(F:/1.txt);8FileWriter b=new FileWriter(F:/2.txt); BufferedWriter c=newBufferedWriter(b);Lin eNumberReader in=new Lin eNumberReader(file); while(str=in.readLi ne()!=null)System.out.pri ntl n(i n.getLi neNumber()+:+str); c.write(st
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 無人機(jī)應(yīng)用技術(shù)3.3.視覺定位
- 廣東省佛山市重點(diǎn)中學(xué)2025屆高二化學(xué)第二學(xué)期期末質(zhì)量跟蹤監(jiān)視模擬試題含解析
- 四川省瀘縣五中2025年高一化學(xué)第二學(xué)期期末監(jiān)測模擬試題含解析
- 政治原創(chuàng)改編題目及答案
- 2025至2030年中國氯林可霉素行業(yè)投資前景及策略咨詢報告
- 2025年中國電暈線行業(yè)投資前景及策略咨詢研究報告
- 項(xiàng)目建筑垃圾處理方案
- 大型商用車駕駛考試題及答案
- 船舶三管考試題庫及答案
- 初級精算師考試題庫及答案
- 江蘇省南京市六校聯(lián)合體2024-2025學(xué)年高一下學(xué)期期末調(diào)研測試歷史試題(含答案)
- 2024年個人信用報告(個人簡版)樣本(帶水印-可編輯)
- 16J914-1 公用建筑衛(wèi)生間
- 芬頓試劑投加量計算
- 建筑自動化課件2013 10.通信網(wǎng)絡(luò)技術(shù)
- LS/T 8008-2010糧油倉庫工程驗(yàn)收規(guī)程
- GB/T 18749-2002耐化學(xué)腐蝕陶瓷塔填料技術(shù)條件
- GB/T 17431.1-1998輕集料及其試驗(yàn)方法第一部分:輕集料
- 2023年德陽市旌陽區(qū)廣播電視臺(融媒體中心)招聘筆試題庫及答案解析
- 新編阿拉伯語第三冊第二課課文及單詞
- 焊接工藝評定氬弧焊
評論
0/150
提交評論