title: how to scan multiple directory using java multi threading
body: <p>The problem statement is, you have to list the name of the files from the given directory, you have given one directory structure which has some subdirectory and some file in them.  </p>
  
  <p>I did some part of the code but it is not working can you please help me what is the correct way of doing it.</p>
  
  <p><strong>code</strong></p>
  
  <pre><code>public class Test {
  public static void main(String[] args) {
      RunableExample run = new RunableExample();
      Thread th = new Thread(run, "thread1");
      String directoryName = "C:\\Users\\GUR35893\\Desktop\\CleanupMTM";
      File directory = new File(directoryName);
      File[] fList = directory.listFiles();
      RunableExample.MyList = new ArrayList&lt;File&gt;();
      for (File file : fList) {
          RunableExample.MyList.add(file);
      }
      try {
          th.start();
      } catch (Exception e) {
  
      }
    }
   }
  
   public class RunableExample implements Runnable {
  public static List&lt;File&gt; MyList;
  int count = 0;
  File filepath;
  
  public void run() {
      try {
          while (count &lt; MyList.size()) {
              System.out.println(Thread.currentThread().getName() + "&gt;&gt;&gt;&gt;"
                      + MyList.size() + " &gt;&gt;&gt;&gt;&gt; " + count);
              filepath = MyList.get(count);
  
              if (filepath != null &amp;&amp; filepath.isFile()) {
  
                  System.out.println(Thread.currentThread().getName() + " &gt;&gt;"
                          + filepath.getAbsolutePath());
              } else {
                  synchronized (this) {
                      if (filepath != null) {
                          // System.out.println("Else");
                          RunableExample run3 = new RunableExample();
                          Thread th3 = new Thread(run3, "thread" + count);
                          File[] fList = filepath.listFiles();
                          // System.out.println("Else1");
                          for (File file : fList) {
                              MyList.add(file);
                          }
                          th3.start();
                      }
                  }
              }
              count++;
          }
      } catch (Exception e) {
          e.printStackTrace();
          System.out.println(e);
  
      }
  
  }
  }
  </code></pre>