I have a directory scan program with single thread. When scanning a file, I have to read attribute information and insert it to database.
I have 2 questions. In order to improve the performance:
Below is the code listing:
void scan() {
File file = new File("/mnt/sdcard");
fun(file);
}
void fun(File file) {
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
// read attribute information and insert to db
return;
} else {
File[] arr = file.listFiles();
for (int i = 0; i < arr.length; i++) {
fun(arr[i]);
}
}
}
I don't think using multithread is going to help here. Scanning directory is IO bounded. Even if you use multiple thread, they are all going to wait for the IO operation to finish in a working thread. So at anytime there is only one thread scanning.
It will help unless the IO operation on your directory can be parallelized, e.g. multiple disks..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With