Reputation: 3682
I have a Java application that is designed to run on removable devices. One feature of it is that it is designed to list all of the folders on the drive, but obviously the more files and folders there are the more time it will take (regardless of how powerful the computer is) and with removal devices getting bigger and bigger these days, I think it is important to gain as much speed in this process as possible.
I know that the best/quickest (in terms of listing, not coding) way would be to create an index file, but I think that misses the point of what my application is all about. So, I thought maybe threads would help because in theory, even two would double the speed of listing, but I don't know if this is possible, and if so how?
Any answers would be appreciated. Here is the recursive method that I currently have:
private static void recursiveNodeCreation(DefaultMutableTreeNode node, String root){
if(root==null) root = RUNNING_DRIVE = ".";
try{
// Get a list of files/folders in the root directory
File rootDir = new File(root);
File[] files = rootDir.listFiles();
// Loop through the files/folders
for(File current : files){
if(Files.isReadable(current.toPath()) && !current.isHidden() && current.isDirectory()){
DefaultMutableTreeNode folder = new DefaultMutableTreeNode(new FolderOnDrive(current.getName(), current.getPath()));
recursiveNodeCreation(folder, current.getPath());
node.add(folder);
}
}
}catch(Exception e){e.printStackTrace();}
}
(RUNNING_DRIVE
holds the drive letter of the drive that the application is running on. I just added = "."
to make it quicker whilst testing. As you may have guessed, I'm listing all the folders in a JTree.)
Upvotes: 0
Views: 175
Reputation: 116878
Threading an application will improve the speed of the application if multiple operations can be performed in parallel. For example, if you are reading and writing from a single network connection, it makes sense to fork a thread so that you can handle other connections at the same time.
In your case, however, I suspect that you are being limited by your removable device's ability to service IO requests. Every device (including memory) has a maximum number of requests and a maximum amount of data that it can handle in a time period -- regardless of the number of threads. Memory sticks and the like are notoriously slow which is why many cameras have memory inside themselves to they can take a number of pictures quickly while writing to the SD card over time. Adding additional threads will most likely not improve the overall application performance and may actually slow it down because the IO requests will be working on different parts of the storage drive and competing with each other.
One way to have your application seem more performant is to have one thread do the actual reading of the device in the background. Then your UI can show the initial results to the user (like the top directory) and fill in more information as the background thread reads in from the device.
To test to see if you are IO bound, you should run your code on a faster hard drive to compare it's directory recursion speed with the removable device.
Upvotes: 2
Reputation: 10372
IO is really slow. This article describes how to tune File IO for performance, but since your program isn't reading the contents of any file, I doubt you can speed it up. The bottleneck of your program isn't how fast you can list the Files, its how fast you can get the list of the Files from the device.
You can add as many threads as you want and the bottleneck will still be the IO.
Upvotes: 1