




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论