多线程拷贝文件
看过请留个言,转载请注明出处,尊重作者劳动成果,谢谢!
以下是一个用多线程拷贝大文件的例子,供大家参考:
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();
}
}