CopyMaxFileThread
public class CopyMaxFileThread extends Thread {
@Override
public void run() {
try {
RandomAccessFile in = new RandomAccessFile("人生.mp3", "rw");
byte[] bytes = new byte[100];
int len = 0;
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Desktop\\", "aa.mp3"));
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.close();
in.close();
System.out.println("复制大文件的线程执行完了");
} catch (IOException e) {
e.printStackTrace();
}
}
}
CopyMinFileCallable
public class CopyMinFileCallable implements Callable<Long> {
@Override
public Long call() throws Exception{
long start = System.currentTimeMillis();
try {
RandomAccessFile in = new RandomAccessFile("mv.jpg", "rw");
byte[] bytes = new byte[1];
int len = 0;
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Desktop\\", "mv.jpg"));
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.close();
in.close();
System.out.println("复制小文件线程执行完了");
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
return end-start;
}
}
CopyMinFileRunable
public class CopyMinFileRunable implements Runnable {
@Override
public void run() {
try {
RandomAccessFile in = new RandomAccessFile("mv.jpg", "rw");
byte[] bytes = new byte[1024 * 8];
int len = 0;
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Desktop\\", "mv.jpg"));
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.close();
in.close();
System.out.println("复制小文件线程执行完了");
} catch (IOException e) {
e.printStackTrace();
}
}
}
MyTest
public class MyTest {
public static void main(String[] args) throws IOException {
CopyMaxFileThread copyMaxFileThread = new CopyMaxFileThread();
copyMaxFileThread.start();
CopyMinFileRunable copyMinFileRunable = new CopyMinFileRunable();
Thread th2 = new Thread(copyMinFileRunable);
th2.start();
}
}
MyTest2
public class MyTest2 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CopyMinFileCallable copyMinFileCallable = new CopyMinFileCallable();
FutureTask<Long> longFutureTask = new FutureTask<>(copyMinFileCallable);
Thread thread = new Thread(longFutureTask);
thread.start();
Long aLong = longFutureTask.get();
System.out.println("耗时:"+aLong+"毫秒");
}
}