java 多线程读取文件内容

package com.mlwy.util;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenchbj
 */
public class ReadUtil implements Serializable {

    private static final long serialVersionUID = -5668938962751597394L;

    private int theadNum = 8 /* 线程数 */,
            timeReadLine = 2048 /* 子线程一次循环读取字节长度 */,
            timeForTime = 10; /* 子线程一次循环读取数 */
    private StringBuffer sbf = new StringBuffer();

    /*public static void main(String[] args) {
        long start = System.currentTimeMillis();

        ReadUtil rUtil = new ReadUtil();
        rUtil.read(new File("/app/data/homebj/logs/mlwybj/info.log"), 0);

        System.out.println("获取字符总长度:" + rUtil.getSbf().length() + ". 总耗时:" +(System.currentTimeMillis() - start) + "毫秒!");
    }
*/
    /**
     * 假定:特殊场景不知道文件总长度! 情景下的   递归+多线程   模式。 可通过File得到总长度,此时可更好的进行子线程数的分配和控制。
     * @param file 文件
     * @param start 其实位置
     */
    public void read(File file, int start){
        List<ReadItem> list = new ArrayList<ReadItem>();

        for(int i = 0; i < theadNum; i++){
            list.add(new ReadItem(file, start + i*timeReadLine*timeForTime));	//创建多个子线程
            list.get(list.size()-1).start();
        }

        for(int i = 0; i < list.size(); i++){
            try {
                list.get(i).join();
                sbf.append(list.get(i).getSb());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(list.get(list.size() - 1).getLastNum() == timeReadLine){
            read(file, start + theadNum * timeReadLine * timeForTime);
        }
    }

    public StringBuffer getSbf() {
        return sbf;
    }

    /**
     * 子线程
     * @author James
     */
    public class ReadItem extends Thread{
        private BufferedReader reader;
        private int start, lastNum;
        private StringBuffer sb;

        public ReadItem(File file, int start) {
            try {
                this.reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.start = start;
            this.sb = new StringBuffer();
        }

        @Override
        public void run() {
            char[] buf = new char[timeReadLine];
            try {
                reader.skip(this.start);
                for (int i = 0; i < timeForTime && (lastNum = reader.read(buf)) != -1; i++ ) {
                    sb.append(new String(buf,0,lastNum));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public int getLastNum() {
            return lastNum;
        }

        public StringBuffer getSb() {
            return sb;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperChen12356

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值