多线程拷贝文件
来源:互联网 发布:华为研发 知乎 编辑:程序博客网 时间:2024/06/04 17:13
以下是一个用多线程拷贝大文件的例子,供大家参考:
package com.jkqiang.thread;
import java.io.*;
/**利用多线程拷贝文件的类*/
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) {
e.printStackTrace();
} catch (IOException e) {
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.jkqiang.thread;
/**利用多线程拷贝文件的主线程类*/
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.jkqiang.thread;
/**利用多线程拷贝文件的测试类 */
public class TestMultiCopy {
public static void main(String[] args) {
MainCopy mc = new MainCopy("D://a.exe", "D://b.exe", 4);
mc.start();
}
}
- 多线程拷贝文件
- 多线程拷贝文件
- 多线程拷贝文件
- 多线程实现文件拷贝
- linux 多线程拷贝文件
- 利用多线程拷贝文件
- 6.多线程文件拷贝
- 多线程拷贝文件
- 多线程文件拷贝
- 多线程断点文件拷贝
- 多线程实现文件拷贝
- C# 多线程目录拷贝 文件拷贝
- 多线程文件拷贝整体架构
- 多线程处理文件(拷贝)
- C++ 多线程拷贝文件(demo)
- NSFileHandle拷贝大文件+多线程
- pthread_create 用法多线程文件拷贝。
- 8.多线程文件拷贝(2)
- 通过Jquery中的选择器选择相同的className统一处理表单(可以同时处理多个表单在一个网页中)
- 简单工厂模式和策略模式
- restlet_JAX-RS 的一个列子
- 利用sed处理文档替换空格的命令
- Android Layout.xml布局属性
- 多线程拷贝文件
- android onTouchListener事件
- JS 正则表达式
- 声明式事务原理
- 抽象基类
- 比较看好 iBeacon
- Android实现类似支付宝上的长按出现删除和置顶操作
- 一入淘宝深似海 卖家你觉得呢?
- mongodb简介、安装、启停