javaマルチスレッドが同時にファイルを読み込みます。

4449 ワード

回転:http://zhidao.baidu.com/question/164435216.html?fr=qrl&cid=870&index=5
import java.io.*;
class DownThread extends Thread {
    //      (     )   
    private final int BUFF_LEN = 32;
    //        
    private long start;
    //        
    private long end;
    //          
    private InputStream is;
    //          raf 
    private RandomAccessFile raf;

    //   ,     ,         、   
    public DownThread(long start, long end, InputStream is, RandomAccessFile raf) {
        //              
        System.out.println(start + "---->" + end);
        this.start = start;
        this.end = end;
        this.is = is;
        this.raf = raf;
    }

    public void run() {
        try {
            is.skip(start);
            raf.seek(start);
            //               (  )
            byte[] buff = new byte[BUFF_LEN];
            //            
            long contentLen = end - start;
            //                     
            long times = contentLen / BUFF_LEN + 4;
            //        
            int hasRead = 0;
            for (int i = 0; i < times; i++) {
                hasRead = is.read(buff);
                //          0,     !
                if (hasRead < 0) {
                    break;
                }
                raf.write(buff, 0, hasRead);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //  finally            、   
        finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

public class MutilDown {
    public static void main(String[] args) {
        final int DOWN_THREAD_NUM = 4;
        final String OUT_FILE_NAME = "d:/copy    .rmvb";
        InputStream[] isArr = new InputStream[DOWN_THREAD_NUM];
        RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM];
        try {

            isArr[0] = new FileInputStream("d:/    .rmvb");
            long fileLen = getFileLength(new File("d:/    .rmvb"));
            System.out.println("     " + fileLen);
            //           RandomAccessFile   
            outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw");
            //               
            for (int i = 0; i < fileLen; i++) {
                outArr[0].write(0);
            }
            //           
            long numPerThred = fileLen / DOWN_THREAD_NUM;
            //            
            long left = fileLen % DOWN_THREAD_NUM;
            for (int i = 0; i < DOWN_THREAD_NUM; i++) {
                //            、  RandomAccessFile  ,
                //                  。
                if (i != 0) {

                    isArr[i] = new FileInputStream("d:/    .rmvb");
                    //           RandomAccessFile  
                    outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw");
                }
                if (i == DOWN_THREAD_NUM - 1) {
                    //          numPerThred+left   
                    new DownThread(i * numPerThred, (i + 1) * numPerThred
                            + left, isArr[i], outArr[i]).start();
                } else {
                    //           numPerThred   
                    new DownThread(i * numPerThred, (i + 1) * numPerThred,
                            isArr[i], outArr[i]).start();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static long getFileLength(File file) {
        long length = 0;
        //       
        long size = file.length();
        length = size;
        return length;
    }
}