• 多线程拷贝文件


     

    看过请留个言,转载请注明出处,尊重作者劳动成果,谢谢!

     

    以下是一个用多线程拷贝大文件的例子,供大家参考:

    package com.wepull.thread;

     

    import java.io.*;

     

    /**

     * @author leno

     * @version V1.0 利用多线程拷贝文件的类

     */

    public class MultiThreadCopy extends Thread {

     

        private File srcFile;// 源文件

        private File tgeFile;// 目标文件

        private int index;// 线程索引号(从0开始)

        private RandomAccessFile rafFrom;// 输入流对象

        private RandomAccessFile rafTo;// 输出流对象

        private static final int MAX_BUF = 10 * 1024 * 1024;// 缓冲区大小

        private int threadNum;// 线程个数

        private long lengthPerThread;// 每个线程需要拷贝的文件长度

        private String threadID;// 自定义线程标识符

        private boolean done = false;

     

        /**

         * @param source

         *            源文件路径

         * @param target

         *            目标文件路径

         * @param index

         *            线程索引号(从0开始)

         * @param threadNum

         *            线程个数

         */

        public MultiThreadCopy(String source, String target, int index,

                int threadNum) {

            this.srcFile = new File(source);

            this.tgeFile = new File(target);

            this.index = index;

            this.threadNum = threadNum;

            this.lengthPerThread = srcFile.length() / threadNum;

            try {

                this.rafFrom = new RandomAccessFile(srcFile, "r");

                this.rafTo = new RandomAccessFile(tgeFile, "rw");

                long pos = lengthPerThread * index;

                rafFrom.seek(pos);

                rafTo.seek(pos);

                this.threadID = "" + index + "线程";

                System.out.println(this.threadID + "开始定位拷贝!");

            } catch (FileNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

     

        }

     

        @Override

        public void run() {

            byte[] buf = new byte[MAX_BUF];

            int len = 0;

            long sum = 0;

            try {

                while ((len = rafFrom.read(buf)) != -1 && sum <= lengthPerThread) {

                    rafTo.write(buf, 0, len);

                    sum += len;

                    System.out.println(this.threadID + "拷贝" + len + "字节");

                }

                done = true;

                System.out.println(this.threadID + "拷贝结束!");

            } catch (IOException e) {

                e.printStackTrace();

            } finally {

                try {

                    rafFrom.close();

                    rafTo.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

     

        public boolean isDone() {

            return done;

        }

     

    }

     

    package com.wepull.thread;

     

    /**

     * @author leno

     * @version V1.0 利用多线程拷贝文件的主线程类

     */

    public class MainCopy extends Thread {

     

        private String srcFile;// 源文件

        private String tgeFile;// 目标文件

        private int threadNum;// 线程个数

     

        public MainCopy(String srcFile, String tgeFile, int threadNum) {

            super();

            this.srcFile = srcFile;

            this.tgeFile = tgeFile;

            this.threadNum = threadNum;

        }

     

        public void run() {

            MultiThreadCopy[] mtcs = new MultiThreadCopy[threadNum];

            long t1 = System.currentTimeMillis();

            for (int i = 0; i < mtcs.length; i++) {

                mtcs[i] = new MultiThreadCopy(srcFile, tgeFile, i, mtcs.length);

                mtcs[i].start();

            }

            boolean stop = false;

            // 循环扫描每个线程对象的isDone(),如果所有线程都已经完成,则整个文件拷贝结束

            while (!stop)

                outer: {

                    try {

                        Thread.sleep(500);

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                    for (int i = 0; i < mtcs.length; i++) {

     

                        if (mtcs[i].isDone() == false) {

                            break outer;

                        }

                    }

                    stop = true;

                }

            long t2 = System.currentTimeMillis();

            System.out.println("整个文件拷贝完毕!大约耗时" + (t2 - t1) + "ms!");

        }

     

    }

     

    package com.wepull.thread;

     

    /**

     * @author leno

     * @version V1.0 利用多线程拷贝文件的测试类

     */

    public class TestMultiCopy {

     

        public static void main(String[] args) {

            MainCopy mc = new MainCopy("D://a.exe", "D://b.exe", 4);

            mc.start();

        }

    }

     

  • 相关阅读:
    网站中使用了Excel组件问题 Microsoft.ACE.OLEDB.14.0' provider is not registered on the local machine
    键值对在架构设计里的应用
    轻轻松松 用U盘安装WIN7
    U盘装WIN7:微软官方工具《Windows 7 USB DVD Download Tool》U盘装wind7(更新官方整合SP1的WIN7 ISO)
    微软一站式示例代码库(中文版)20110924版本, 新添加ASP.NET, Windows Base, Silverlight, WinForm等20个Sample
    最薄笔记本苹果MacBook Air安装雪豹+Win7双系统的驱动解决方法
    使用受保护的配置加密配置信息
    自动加密web.config配置节批处理
    Asp.net MVC3学习
    周老师科研站
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9663827.html
Copyright © 2020-2023  润新知