




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、多線程讀取文件 Java多線程文件讀取單線程讀取10K個 文件的話會顯的非常的慢,但是如果我們開啟多個線程去讀取,會讓讀取的速度大大的加快,順便說一下多線程的注意事項:synchronized 代碼快中 盡量簡短,不要有 類似socket.accept() inputstream.read() 這樣的阻塞式的方法,這樣會讓程序減慢,如果synchronized代碼快中的東西太多,容易造成單線程的囧地,而且速度還不如單線程快。廢話少說,我們來看看代碼:-1.生成10K個 文件:package com.fileThread;import java.io.File;import java.io.Fi
2、leOutputStream;import java.io.OutputStream;public class GenFile /* * param args * throws Exception */ public static void main(String args) throws Exception
3、0; File file = null; OutputStream out = null; file = new File("c:/file"); if(!file.exists() file.mkdirs();
4、; for (int i = 0; i < 10000; i+) file = new File("c:/file/file" + i + ".txt"); out = new FileOutputStream(f
5、ile); out.write("nihao1hh1hh1".getBytes(); out.close(); file = null;
6、0; out = null; -多線程 讀取package com.fileThread;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;public class ReadFile public static void main(String args) throws Exception
7、 Sum sum = new Sum(); Thread t1 = new Thread(sum); Thread t2 = new Thread(sum); Thread t3 = new Thread(sum); t1.sta
8、rt(); t2.start(); t3.start(); / t1.run(); / t2.run(); / t3.run(); / System.out
9、.println(sum.getSum()+"="); class Sum implements Runnable private Integer i = 0; private Integer sum = 0; static long time; public void run() File file = null;
10、160; InputStream is = null; StringBuffer sb = null; while (true) if (i = 0)
11、; time = System.currentTimeMillis(); if (i = 10000) break;
12、60; synchronized (this) file = new File("c:/file/file" + i + ".txt");
13、60; / System.out.println(i + "currentThread=" + / Thread.currentThread().getName(); i+;
14、; try is = new FileInputStream(file); catch (Fil
15、eNotFoundException e) byte data = new byte2048; int len = 0; sb = new Stri
16、ngBuffer(); try while (len = is.read(data) != -1) sb.append(new String(da
17、ta, 0, len); catch (IOException e) String result = sb.
18、toString(); String arr = result.split("D+"); synchronized (this) for (String s : arr)
19、0; if (s != null && s.trim().length() > 0) sum += Integer.parseInt(s);
20、0; file = null; &
21、#160; sb = null; is = null; System.out.println(this.sum); System.out.println(System.currentTimeMillis() - time); public Integer getI(
22、) return i; public void setI(Integer i) this.i = i; public Integer getSum() return sum;
23、 public void setSum(Integer sum) this.sum = sum; -所有的東西 都放到 synchronazied代碼快中的速度比較,這個類似單線程 package com.fileThread;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOExceptio
24、n;import java.io.InputStream;public class ReadFileAll public static void main(String args) throws Exception Sums sum = new Sums(); Thread t1 = new Thread(sum); Thread t2
25、 = new Thread(sum); Thread t3 = new Thread(sum); t1.start(); t2.start(); t3.start(); / 22562
26、0; / 14625 class Sums implements Runnable private Integer i = 0; private Integer sum = 0; static long time; public void run() File file = null;
27、0; InputStream is = null; StringBuffer sb = null; while (true) if (i = 0)
28、time = System.currentTimeMillis(); if (i = 10000) break;
29、; synchronized (this) file = new File("c:/file/file" + i + ".txt");/ Sys
30、tem.out.println(i + "currentThread="/ + Thread.currentThread().getName(); i+;
31、 try is = new FileInputStream(file); catch (FileNotFoundException e)
32、; byte data = new byte2048; int len = 0;
33、160; sb = new StringBuffer(); try while (len = is.read(data) != -1)
34、 sb.append(new String(data, 0, len); catch (IOExceptio
35、n e) String result = sb.toString(); String arr = result.split("D+&qu
36、ot;); for (String s : arr) if (s != null && s.trim().length() > 0)
37、160; sum += Integer.parseInt(s);
38、160; file = null; sb = null; is = null; System.out.println(this.sum); &
39、#160; System.out.println(System.currentTimeMillis() - time); public Integer getI() return i; public void setI(Integer i) this.i = i;
40、 public Integer getSum() return sum; public void setSum(Integer sum) this.sum = sum; -單線程去讀的速度:package com.fileThread;import java.io.File;import java.
41、io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;public class ReadFileSingleThread public static void main(String args) throws Exception int i = 0;
42、0; long time = 0; int sum =0; File file = null; InputStream is = null; StringBuffer sb = null; while (true) &
43、#160; if (i = 0) time = System.currentTimeMillis(); if (i = 10000)
44、 break; file = new File("c:/file/file" + i + ".txt"); &
45、#160; i+; try is = new FileInputStream(file); catch (FileNotFoundException e)
46、60; byte data = new byte2048; int len = 0; sb = new StringBuffer(); try while (len = is.read(data) != -1) &
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 省級中小學現代教育技術裝備標準實施指南
- 基于二進制分析的iOS應用漏洞動態行為研究-洞察及研究
- 微膿腫組織病理特征-洞察及研究
- 在役橋梁檢測與健康監測技術融合及診斷體系創新研究
- 權責清單管理暫行辦法
- 關鍵技術改進下的期盼
- 血液透析專業理論與實踐考核要點解析
- 安全生產三卡是指
- 生產安全事故調查處理報告
- 綠色金融估值體系-洞察及研究
- 企業數字化轉型服務協議
- 人工智能算法的倫理規制研究
- 變電所設備更換申請報告
- 學習2025年全國教育工作會議精神解讀
- 福建省寧德市2024-2025學年八年級上學期期末考試數學試題
- 初級中式面點師培訓教學大綱和教學計劃
- 銀行外包人員培訓課件
- 數控銑床編程與操作項目教程 第4版 課件 項目三 孔加工 任務四 鏜孔
- 《公路工程預算定額》(JTGT3832-2018)
- 純電動汽車高壓上電流程與故障分析
- 三方比價合同協議書范文范本
評論
0/150
提交評論