多线程读取文件

package com.fileThread;


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

//生成10K个文件
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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值