多线程读取文件.docx

上传人:scccc 文档编号:13887408 上传时间:2022-01-25 格式:DOCX 页数:13 大小:29.96KB
返回 下载 相关 举报
多线程读取文件.docx_第1页
第1页 / 共13页
多线程读取文件.docx_第2页
第2页 / 共13页
多线程读取文件.docx_第3页
第3页 / 共13页
多线程读取文件.docx_第4页
第4页 / 共13页
多线程读取文件.docx_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《多线程读取文件.docx》由会员分享,可在线阅读,更多相关《多线程读取文件.docx(13页珍藏版)》请在三一文库上搜索。

1、单线程读取10K个 文件的话会显的非常的慢,但是如果我们开启多个线程去读 取,会让读取的速度大大的加快,顺便说一下多线程的注意事项:synchronized 代码快中尽量简短,不要有类似socket.accept() inputstream.read() 这样 的阻塞式的方法,,这样会让程序减慢,如果synchronized代码快中的东西太多, 容易造成单线程的冏地,而且速度还不如单线程快。废话少说,我们来看看代码: 11 .生成10K个文件:package com.fileThread;import java.io.File;import java.io.FileOutputStream;i

2、mport java.io.OutputStream;public class GenFile /* param args* throws Exception* /public static void main(String口 args) throws Exception File file = null;Outputstream out = null;file = new File(c:/file);if(!file.exists()file.mkdirs();for (int i = 0; i 0) sum += Integer.parseInt(s);file = null; /sb =

3、 null;is = null;System.out.println(this.sum);System.out.println(System.currentTimeMillis() - time);public Integer getI() return i;public void setI(Integer i) this.i = i;public Integer getSum() return sum;public void setSum(Integer sum) this.sum = sum;所有的东西/都放到synchronazied代码快中的速度比较,这个类似单线程package co

4、m.fileThread;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;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 = new

5、 Thread(sum);Thread t3 = new Thread(sum);t1.start();t2.start();t3.start();/ 22562/ 14625class Sums implements Runnable private Integer i = 0;private Integer sum = 0;static long time;public void run() File file = null;InputStream is = null;StringBuffer sb = null;while (true) if (i = 0) time = System.

6、currentTimeMillis();if (i = 10000) break;.txt);/synchronized (this) /Thread.currentThread().getName();file = new File(c:/file/file + i +i+;try System.out.println(i + currentThread=is = new FilelnputStream(file); catch (FileNotFoundException e) )byte口 data = new byte2048;int len = 0;sb = new StringBu

7、ffer();try while (len = is.read(data) != -1) sb.append(new String(data, 0, len); catch (IOException e) String result = sb.toString();String arr = result.split(D+);for (String s : arr) if (s != null &s.trim().length() 0) sum += Integer.parseInt(s); file = null;sb = null;is = null;System.out.println(t

8、his.sum);System.out.println(System.currentTimeMillis() - time);public Integer getI() return i;public void setI(Integer i) this.i = i;public Integer getSum() return sum;public void setSum(Integer sum) this.sum = sum;单线程去读的速度: package com.fileThread;import java.io.File;/import java.io.FileInputStream;

9、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;long time = 0;int sum =0;File file = null;InputStream is = null;StringBuffer sb = null;while (true) if (i = 0)

10、 time = System.currentTimeMillis();if (i = 10000) break; /file = new File(c:/file/file + i + .txt);i+;try (is = new FilelnputStream(file); catch (FileNotFoundException e) )byte data = new byte2048;int len = 0;sb = new StringBuffer();try while (len = is.read(data) != -1) sb.append(new String(data, 0,

11、 len); catch (IOException e) String result = sb.toString();String arr = result.split(D+);for (String s : arr) if (s != null & s.trim().length() 0) sum += Integer.parseInt(s);file = null;sb = null;is = null; /System.out.println(sum);System.out.println(System.currentTimeMillis() - time);)通过比较,大家就可以发现多线程的好处了,可能大家多如何写多线程存在疑问, 这里我想说的是多想想,你的多线程是想做什么,就像 这个 例子,我的多线程 就是想 每个人线程分担 不同的文件读取,再算总和,所以我要控制的同步资源% 就是文件的名称和总和,所以我就需要把这两个属性同步上,这样就可以。如 何返回多线程执行完的结果呢,我个人认为这样做,是让其他线程在mian之前完成,这样就可以达到效果,可以用 thread.join() 方法,这样在执行 mian之 前就会先把多线程执行完,就可以的到结果。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1