




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、1班級:13科技2班 學(xué)號:201324131225:許耿寧Java多線程和輸入輸出流一、實驗?zāi)康模?.熟悉利用 Thread 類建立多線程方法。2.熟悉利用 Thread 接口建立多線程方法。3.熟悉 Java 的文件讀寫機制,練習(xí)輸入輸出流的使用。二、實驗容:1.閱讀下列程序,分析并上機檢驗其功能。 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 接口改寫,并上機檢驗。3. 創(chuàng)建簡單的程序 ThreeThread.java ,該程序?qū)?chuàng)建三個線程, 每個線程應(yīng) 當(dāng)顯示它所運行的時間(可以考慮使用 Date 類或 Calendar 類)。4.
4、鍵盤輸入 10 個整數(shù),從小到大進行排序。5. 接收鍵盤輸入的字符串,用 FileInputStream 類將字符串寫入文件,用FileOutputStream 類讀出文件容顯示在屏幕上。6. 將一個文本文件的容按行讀出,每讀出一行就順序加上行號,并寫入到另一個文件中。三、實驗要求:1. 通過實驗掌握 Thread 、 Runnable 使用方法;2. 程序必須能夠?qū)崿F(xiàn)多線程;3. 程序必須能夠完成題目要求;4. 通過實驗掌握文件輸入輸出流的使用方法;5. 程序必須能夠從鍵盤接收字符串并保存在文件中;6. 程序必須能夠讀出文件容顯示在屏幕上;7. 寫出實驗報告。四、實驗代碼及截圖: 第一題:
5、在編譯器上運行程序得到截圖所示結(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第二題:實驗代碼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);實驗結(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第三題:實驗代碼: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第四題:實驗代碼: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);實驗結(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第五題:實驗代碼: 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);/將 文件讀入二進制數(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();實驗結(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í),天天向上第六題:實驗代碼: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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 四川文理學(xué)院《供熱工程與鍋爐》2023-2024學(xué)年第一學(xué)期期末試卷
- 內(nèi)蒙古商貿(mào)職業(yè)學(xué)院《基礎(chǔ)緬甸語四》2023-2024學(xué)年第二學(xué)期期末試卷
- 南京林業(yè)大學(xué)《人力資源戰(zhàn)略與規(guī)劃》2023-2024學(xué)年第二學(xué)期期末試卷
- 平頂山工業(yè)職業(yè)技術(shù)學(xué)院《電子系統(tǒng)基礎(chǔ)》2023-2024學(xué)年第二學(xué)期期末試卷
- 河北科技工程職業(yè)技術(shù)大學(xué)《汽車電器與電子技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 建筑工程勞務(wù)施工承包合同
- 中央空調(diào)施工合同
- 場地短期租賃合同書
- 大型商鋪租賃合同書
- 廠房租賃合同協(xié)議書
- FZ∕T 73037-2019 針織運動襪行業(yè)標(biāo)準(zhǔn)
- 新疆維吾爾自治區(qū)和田地區(qū)墨玉縣2023-2024學(xué)年七年級下學(xué)期5月期中道德與法治試題
- 工廠化循環(huán)水產(chǎn)養(yǎng)殖項目投資計劃書
- 全民國家安全教育日培訓(xùn)課件模板(可編輯)
- 精神疾病患者自殺風(fēng)險評估與預(yù)防措施
- 山西省太原市2023-2024學(xué)年七年級下學(xué)期期中數(shù)學(xué)試題
- XF-T 3004-2020 汽車加油加氣站消防安全管理
- 江蘇省鹽城市建湖縣2023-2024學(xué)年七年級下學(xué)期期中語文試題
- 印刷廠常用生產(chǎn)工藝、設(shè)備作業(yè)指導(dǎo)書一整套
- 甘肅省農(nóng)墾集團有限責(zé)任公司人才招聘考試試題及答案
- 濕式電除塵施工方案(完整常用版)
評論
0/150
提交評論