Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan directory with multi-thread [closed]

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:

  1. How to scan with multi-thread? (scan sd card of Android phone)
  2. How to optimize the batch insert to DB?

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]);
        }
    }
}
like image 304
user2130265 Avatar asked Mar 04 '13 03:03

user2130265


1 Answers

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..

like image 128
zzk Avatar answered Oct 10 '22 00:10

zzk