Skip to content

Instantly share code, notes, and snippets.

@cmcenearney
Created August 8, 2013 00:47
Show Gist options
  • Save cmcenearney/6180437 to your computer and use it in GitHub Desktop.
Save cmcenearney/6180437 to your computer and use it in GitHub Desktop.
threads example
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.io.*;
/*
* Your assignment: The DirectoryCopier class will copy all the files in one
* directory to another directories in series (not in parallel). Use that code
* as a starting point and modify this class to copy files in parallel using threads.
*/
public class AsyncDirectoryCopier {
private static final int BUFFER_SIZE = 1024;
/**
* Copies one directory to another directory
* Only works with existing directories, and only copies the first level of files
* (Does not recursively copy directory structure)
*
* @param sourceDir The source directory file. Must be an existing directory.
* @param destinationDir The destination directory file. Must be an existing directory.
* @throws IOException An IOException raised while copying the files.
*/
public void copyDirectory(File sourceDir, File destinationDir) throws IOException {
if (!sourceDir.isDirectory() || !destinationDir.isDirectory()) {
throw new IllegalArgumentException("Must pass directories into copyDirectory function");
}
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
};
File[] filesToCopy = sourceDir.listFiles(fileFilter);
for (File sourceFile : filesToCopy) {
File destinationFile = new File(destinationDir, sourceFile.getName());
Runnable task = new CopyFileRunnable(sourceFile, destinationFile);
Thread worker = new Thread(task);
worker.start();
//copyFile(sourceFile, destinationFile);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment