Java multithreading copies the instance code of the file
- 2020-04-01 01:35:28
- OfStack
package com.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FileCoper {
private static final String _ORIGIN_FILE_MODE = "r";
private static final String _TARGET_FILE_MODE = "rw";
private static long time1 = 0l;
private String originFileName;
private String targetFileName;
private RandomAccessFile originFile;
private RandomAccessFile targetFile;
private int threadCount;
private static int totalThreadCount = 0;
private static int executedCount = 0;
public FileCoper() {
this.threadCount = 1;
totalThreadCount = this.threadCount;
}
public FileCoper(String originFile, String targetFile) {
try {
this.originFileName = originFile;
this.targetFileName = targetFile;
this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);
this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);
this.threadCount = 1;
totalThreadCount = this.threadCount;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FileCoper(String originFile, String targetFile, int threadCount) {
try {
this.originFileName = originFile;
this.targetFileName = targetFile;
this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);
this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);
this.threadCount = 1;
totalThreadCount = this.threadCount;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void init(String originFile, String targetFile) throws Exception {
this.originFileName = originFile;
this.targetFileName = targetFile;
this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);
this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);
this.threadCount = 1;
totalThreadCount = this.threadCount;
}
public void init(String originFile, String targetFile, int threadCount) throws Exception {
this.originFileName = originFile;
this.targetFileName = targetFile;
this.originFile = new RandomAccessFile((originFile), FileCoper._ORIGIN_FILE_MODE);
this.targetFile = new RandomAccessFile((targetFile), FileCoper._TARGET_FILE_MODE);
this.threadCount = threadCount;
totalThreadCount = this.threadCount;
}
public void init(RandomAccessFile originFile, RandomAccessFile targetFile) throws Exception {
this.originFile = originFile;
this.targetFile = targetFile;
this.threadCount = 1;
totalThreadCount = this.threadCount;
}
public void init(RandomAccessFile originFile, RandomAccessFile targetFile, int threadCount) throws Exception {
this.originFile = originFile;
this.targetFile = targetFile;
this.threadCount = threadCount;
totalThreadCount = this.threadCount;
}
public static synchronized void finish() {
FileCoper.executedCount ++;
System.out.println(" The total threads" " + FileCoper.totalThreadCount + " 】 , Have finished [ " + FileCoper.executedCount + " Copy of a thread!! ");
if (FileCoper.totalThreadCount == FileCoper.executedCount){
long time2 = System.currentTimeMillis();
System.out.println(" Time spent: "+(time2-time1));
System.out.println(" All the " + FileCoper.totalThreadCount + " Thread copy completed!! ");
}
}
public void start() throws Exception {
if (this.originFile.length() == 0)
return;
if (this.threadCount == 0)
this.threadCount = 1;
//Setting the target file size & NBSP;
this.targetFile.setLength(this.originFile.length());
this.targetFile.seek(0);
this.originFile.seek(0);
time1 = System.currentTimeMillis();
System.out.println(this.originFile.length());
//Divide the file into blocks, each with a pair of values: the starting and ending position of the current block in the file & PI;
long[][] splits = new long[this.threadCount][2];
long originFileLength = this.originFile.length();
int startPos = 0;
for (int i = 0; i < this.threadCount; i++) {
splits[i][0] = 0;
splits[i][1] = 0;
if (i == 0) {
splits[i][0] = 0;
splits[i][1] = originFileLength / this.threadCount;
} else if (i == this.threadCount - 1) {
//Note: you can't add 1 here. If you add 1, multiple files will appear messy & NBSP;
// splits[i][0] = startPos + 1;
splits[i][0] = startPos;
splits[i][1] = originFileLength;
} else {
//Note: you can't add 1 here. If you add 1, multiple files will appear messy & NBSP;
// splits[i][0] = startPos + 1;
splits[i][0] = startPos;
splits[i][1] = startPos + originFileLength / this.threadCount;
}
startPos += originFileLength / this.threadCount;
// System.out.println(splits[i][0] + " " + splits[i][1]);
Coper fc = new Coper("thread-" + i);
fc.init(this.originFile, this.targetFile, splits[i][0], splits[i][1]);
fc.setOriginFileName(this.originFileName);
fc.setTargetFileName(this.targetFileName);
fc.start();
}
}
public void startNew() throws Exception {
if (this.originFile.length() == 0)
return;
//Setting the target file size & NBSP;
this.targetFile.setLength(this.originFile.length());
this.targetFile.seek(0);
this.originFile.seek(0);
long startPosition;
long endPosition;
long block = this.originFile.length() / 1029;
if (block <= 1)
this.threadCount = 1;
for (int i = 0; i < this.threadCount; i++) {
//Define the length of each transition & NBSP;
startPosition = i * 1029 * (block / this.threadCount);
endPosition = (i + 1) * 1029 * (block / this.threadCount);
if (i == (this.threadCount - 1))
endPosition = this.originFile.length();
Coper fc = new Coper("thread-" + i);
fc.init(this.originFile, this.targetFile, startPosition, endPosition);
fc.setOriginFileName(this.originFileName);
fc.setTargetFileName(this.targetFileName);
fc.start();
}
}
private class Coper extends Thread {
private String originFileName;
private String targetFileName;
private RandomAccessFile originFile;
private RandomAccessFile targetFile;
private String threadId;
private long startPosition;
private long endPosition;
private long blockCapacity;
public void setOriginFileName(String originFileName) {
this.originFileName = originFileName;
}
public void setTargetFileName(String targetFileName) {
this.targetFileName = targetFileName;
}
public Coper(String threadId) {
this.threadId = threadId;
}
public void init(RandomAccessFile originFile, RandomAccessFile targetFile, long startPosition, long endPosition) throws Exception {
this.originFile = originFile;
this.targetFile = targetFile;
this.startPosition = startPosition;
this.endPosition = endPosition;
this.blockCapacity = this.endPosition - this.startPosition;
}
public void run() {
//System.out.println(this.threadid + "start, start copying files [" +
//File block in this. OriginFileName + "] [" + this.startposition + "," +& ° ;& ° ;
//This. EndPosition + "] to the target file [" + this.targetfilename + "]..." );
synchronized (this.originFile) {
try {
//Records the number of bytes of the current copy & PI;
int copyCount = 0;
//Reference offset & NBSP; of data copy;
long offSet = this.startPosition;
byte[] b = new byte[16 * 1024 * 1024];
//Dynamically sets the number of bytes buffer & PI for a read;
long blockSize = 0;
while (copyCount < this.blockCapacity) {
this.originFile.seek(offSet);
if (this.blockCapacity - copyCount > 16 * 1024 * 1024)
blockSize = 16 * 1024 * 1024;
else
blockSize = this.blockCapacity - copyCount;
if (blockSize > this.blockCapacity - copyCount)
blockSize = this.blockCapacity - copyCount;
int count = this.originFile.read(b, 0, (int) blockSize);
synchronized (this.targetFile) {
try {
if (copyCount == 0)
this.targetFile.seek(offSet);
else
this.targetFile.seek(offSet + 1);
this.targetFile.write(b, 0, count);
} catch (IOException e) {
e.printStackTrace();
}
}
//Increase the number of bytes copied & NBSP;
copyCount += count;
//Copy is actually [offset moved down & NBSP;& NBSP;
offSet += count;
}
} catch (IOException e) {
e.printStackTrace();
}
}
//System.out.println(this.threadid + "copy file [" + this.originfilename & NBSP;
//+ "] in the file block [" + this.startposition + "," + this.endposition +& NBSP;& NBSP;
//"] to the target file [" + this.targetfilename + "] done!" );
//Notifies the main thread that the current thread has completed the replication
FileCoper.finish();
}
}
public static void main(String[] args) throws Exception {
FileCoper fc = new FileCoper();
fc.init("e:/InitialData_zhihuan.sql", "e:/InitialData_zhihuan2.sql", 30);
//fc.init("d:/ValueAdd_11.txt", "d:/ValueAdd_111.txt", 100);
//Fc. Init (" D: tools music do your lover. Mp3 ", "D: / do you love _5. Mp3", 10);
//Fc. Init (E: "the darkest. RMVB", "d: / the darkest hit _1. RMVB", 100);
/* //Read in the keyboard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Source file
String originFile;
//File target
String targetFile;
System.out.println(" [source file, target file, number of threads] ");
System.out.print(" Source file to be copied: ");
originFile = br.readLine();
System.out.print(" Copy the file to the target file: ");
targetFile = br.readLine();
System.out.print(" Number of slitted threads: ");
int threadCount = Integer.parseInt(br.readLine());
fc.init(originFile, targetFile, threadCount);*/
// fc.startNew();
long time1 = System.currentTimeMillis();
fc.start();
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
}