package club.calong.lawyer;
// 团 队: 风讯网络
// 后 端: Dopa
// 开发时间: 2021/7/25 15:06
import java.io.*;
import java.time.Duration;
import java.time.LocalDateTime;
public class ApplicationTest extends Thread {
long begin;
long end;
RandomAccessFile sourceFile;
RandomAccessFile targetFile;
public ApplicationTest(long begin, long end, File file , String targetFilePath) throws IOException {
this.begin = begin;
this.end = end;
this.sourceFile = new RandomAccessFile(file, "rw");
this.targetFile = new RandomAccessFile(targetFilePath, "rw");
sourceFile.seek(begin);
targetFile.seek(begin);
}
@Override
public void run() {
byte[] bytes = new byte[1];
try {
while (begin<end&&sourceFile.read(bytes) != -1) {
begin += 1;
targetFile.write(bytes,0,1);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
targetFile.close();
sourceFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Start {
public static void main(String[] args) throws IOException, InterruptedException {
File file=new File("C:\\TEST\\test.sql");
int index = Runtime.getRuntime().availableProcessors()-4;
long divisor = file.length() / index;
LocalDateTime startTime=LocalDateTime.now();
for (int i = 0; i < index+1; i++) {
System.out.println((divisor * i) + "-----" + (divisor * (i + 1)));
Thread thread= new ApplicationTest(divisor*i,divisor*(i+1),file, "C:\\TEST\\test44.sql");
thread.setName("copy"+i);
thread.join();
thread.start();
}
LocalDateTime endTime=LocalDateTime.now();
System.out.println(startTime);
System.out.println(endTime);
Duration duration=Duration.between(startTime, endTime);
System.out.println(duration.toMillis());
System.out.println(duration.toNanos());
}
}
多线程复制同一份文件
最新推荐文章于 2024-07-20 23:59:39 发布