`
kaobian
  • 浏览: 208617 次
  • 性别: Icon_minigender_1
  • 来自: 哈尔滨
社区版块
存档分类
最新评论

多线程读取文件

阅读更多
单线程读取10K个 文件的话会显的非常的慢,但是如果我们开启多个线程去读取,会让读取的速度大大的加快,顺便说一下多线程的注意事项:synchronized 代码快中 尽量简短,不要有 类似socket.accept() inputstream.read() 这样的阻塞式的方法,这样会让程序减慢,如果synchronized代码快中的东西太多,容易造成单线程的囧地,而且速度还不如单线程快。

废话少说,我们来看看代码:
1.生成10K个 文件:
package com.fileThread;

import java.io.File;
import java.io.FileOutputStream;
import 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 < 10000; i++) {
            file = new File("c:/file/file" + i + ".txt");
            out = new FileOutputStream(file);
            out.write("nihao1hh1hh1".getBytes());
            out.close();
        }
        file = null;
        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 {
        Sum sum = new Sum();
        Thread t1 = new Thread(sum);
        Thread t2 = new Thread(sum);
        Thread t3 = new Thread(sum);
        t1.start();
        t2.start();
        t3.start();
        // t1.run();
        // t2.run();
        // t3.run();
        // System.out.println(sum.getSum()+"==");
    }
}

class Sum 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.currentTimeMillis();
            }
            if (i == 10000) {
                break;
            }
            synchronized (this) {
                file = new File("c:/file/file" + i + ".txt");
                // System.out.println(i + "currentThread==" +
                // Thread.currentThread().getName()); 

              i++;


            }
           
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
            }
            byte[] data = new byte[2048];
            int len = 0;
            sb = new StringBuffer();
            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+");
            synchronized (this) {
                for (String s : arr) {
                    if (s != null && s.trim().length() > 0) {
                        sum += Integer.parseInt(s);
                    }
                }
            }
        }
        file = null;
        sb = 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 com.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 Thread(sum);
        Thread t3 = new Thread(sum);
        t1.start();
        t2.start();
        t3.start();
        // 22562
        // 14625
    }
}

class 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.currentTimeMillis();
            }
            if (i == 10000) {
                break;
            }
            synchronized (this) {
                file = new File("c:/file/file" + i + ".txt");
//                System.out.println(i + "currentThread=="
//                        + Thread.currentThread().getName());
                i++;
                try {
                    is = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                }
                byte[] data = new byte[2048];
                int len = 0;
                sb = new StringBuffer();
                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(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;
    }

}

单线程去读的速度:

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 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) {
                time = System.currentTimeMillis();
            }
            if (i == 10000) {
                break;
            }
            file = new File("c:/file/file" + i + ".txt");
            i++;
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
            }
            byte[] data = new byte[2048];
            int len = 0;
            sb = new StringBuffer();
            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(sum);
        System.out.println(System.currentTimeMillis() - time);
    }
}



通过比较,大家就可以发现多线程的好处了,可能大家多如何写多线程存在疑问,这里我想说的是多想想,你的多线程是想做什么,就像 这个 例子,我的多线程 就是想 每个人线程分担 不同的文件读取,再算总和,所以我要控制的同步资源就是文件的名称和总和,所以我就需要把这两个 属性 同步上,这样就可以。如何返回多线程执行完的结果呢,我个人认为这样做,是让其他线程在mian 之前完成,这样就可以达到效果,可以用thread.join()方法,这样在执行mian 之前就会先把多线程执行完,就可以的到结果。
4
7
分享到:
评论
10 楼 bq_test1 2015-01-20  
我是这么理解的,你的synchorized方法还没执行完,i已经变成了10000,此时外面的线程在  if (i == 10000) { 
                break; 
            }  这句话下面等待执行,把10000跳过去了,然后后面的也不会中断了。
但是第一个类ReadFile 却可以正常结束执行,很诡异。建议把while里的有关i的数据操作都放到synchorized里面。
9 楼 bq_test1 2015-01-20  
ReadFileAll 这个类的run方法多线程执行会阻塞,没有跳出while循环,楼主测一下。
8 楼 bq_test1 2015-01-20  
你好,我想请教个问题。
单线程读取大文件和多线程读取大文件是否会对内存的使用有什么不同?比如单线程更容易造成内存溢出,而多线程不容易?
7 楼 lynnkong 2011-07-18  
i++;

这个可以用AtomicInteger类的addAndGet代替。
6 楼 kaobian 2011-07-18  
Dark-Mraz 写道
kaobian 写道
Dark-Mraz 写道
仔细看了看,楼主的每个Runnable对象拥有独立的计数器,并非共享。那多线程的用意何在?

这个你说错了,这三个是共享同一个计数器的,你可以跑一下程序看看


看错了,以为你扔了三个不同的Runnable对象给3个线程。原来是同一个

呵呵,很感谢你的评语,谢谢你的关注 
5 楼 Dark-Mraz 2011-07-18  
kaobian 写道
Dark-Mraz 写道
仔细看了看,楼主的每个Runnable对象拥有独立的计数器,并非共享。那多线程的用意何在?

这个你说错了,这三个是共享同一个计数器的,你可以跑一下程序看看


看错了,以为你扔了三个不同的Runnable对象给3个线程。原来是同一个
4 楼 kaobian 2011-07-18  
Dark-Mraz 写道
仔细看了看,楼主的每个Runnable对象拥有独立的计数器,并非共享。那多线程的用意何在?

这个你说错了,这三个是共享同一个计数器的,你可以跑一下程序看看
3 楼 kaobian 2011-07-18  
Dark-Mraz 写道
多线程读有些问题

 synchronized (this) {
                file = new File("c:/file/file" + i + ".txt");
                // System.out.println(i + "currentThread==" +
                // Thread.currentThread().getName());
            }
            i++;


i++在同步块之外,无法保证计数器的线程安全。

假设当前线程释放锁,即将执行i++但还未执行时,其他线程获得对象锁执行了new File
则导致文件被重复读取,且下一个文件被跳过未被读取。

呵呵你说的对,这个是应该放到同步代码块中的。
2 楼 Dark-Mraz 2011-07-18  
仔细看了看,楼主的每个Runnable对象拥有独立的计数器,并非共享。那多线程的用意何在?
1 楼 Dark-Mraz 2011-07-18  
多线程读有些问题

 synchronized (this) {
                file = new File("c:/file/file" + i + ".txt");
                // System.out.println(i + "currentThread==" +
                // Thread.currentThread().getName());
            }
            i++;


i++在同步块之外,无法保证计数器的线程安全。

假设当前线程释放锁,即将执行i++但还未执行时,其他线程获得对象锁执行了new File
则导致文件被重复读取,且下一个文件被跳过未被读取。

相关推荐

Global site tag (gtag.js) - Google Analytics