Package wwww

Source Code of wwww.FolderProcessor

package wwww;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;

public class FolderProcessor extends RecursiveTask<List<String>> {

  private static final long serialVersionUID = -590727688578356028L;

  private String path;
  private String extension;

  public static void main(String[] args) {
    ForkJoinPool pool = new ForkJoinPool(12);
    // 创建3个FolderProcessor任务。用不同的文件夹路径初始化每个任务。

    FolderProcessor system = new FolderProcessor("C:\\", "txt");
    FolderProcessor apps = new FolderProcessor("D:\\", "txt");
    FolderProcessor documents = new FolderProcessor("E:\\", "txt");
    // 在池中使用execute()方法执行这3个任务。
    pool.execute(system);
    pool.execute(apps);
    pool.execute(documents);
    // 将关于池每秒的状态信息写入到控制台,直到这3个任务完成它们的执行。
    do {
      System.out.printf("******************************************\n");
      System.out.printf("Main: Parallelism: %d\n", pool.getParallelism());
      System.out.printf("Main: Active Threads: %d\n", pool.getActiveThreadCount());
      System.out.printf("Main: Task Count: %d\n", pool.getQueuedTaskCount());
      System.out.printf("Main: Steal Count: %d\n", pool.getStealCount());
      System.out.printf("******************************************\n");
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    } while ((!system.isDone()) || (!apps.isDone())
        || (!documents.isDone()));
    // 使用shutdown()方法关闭ForkJoinPool。
    pool.shutdown();
    // 将每个任务产生的结果数量写入到控制台。

    List<String> results;
    results = system.join();
    System.out.printf("System: %d files found.\n", results.size());
    for (String string : results) {
      System.out.println(string);
    }
    results = apps.join();
    System.out.printf("Apps: %d files found.\n", results.size());
    for (String string : results) {
      System.out.println(string);
    }
    results = documents.join();
    System.out.printf("Documents: %d files found.\n", results.size());
    for (String string : results) {
      System.out.println(string);
    }

  }

  public FolderProcessor(String path, String extension) {
    this.path = path;
    this.extension = extension;
  }

  @Override
  protected List<String> compute() {
    List<String> list = new ArrayList<String>();
    List<FolderProcessor> tasks = new ArrayList<FolderProcessor>();
    File file = new File(path);
    File content[] = file.listFiles();
    if (content != null) {
      for (int i = 0; i < content.length; i++) {
        if (content[i].isDirectory()) {
          FolderProcessor task = new FolderProcessor(
              content[i].getAbsolutePath(), extension);
          task.fork();
          tasks.add(task);
        } else {
          if (checkFile(content[i].getName())) {
            list.add(content[i].getAbsolutePath());
          }
        }
      }
      /*if (tasks.size() > 50) {
        System.out.printf("%s: %d tasks ran.\n",
            file.getAbsolutePath(), tasks.size());
      }*/

      addResultsFromTasks(list, tasks);
    }
    return list;
  }

  private void addResultsFromTasks(List<String> list,
      List<FolderProcessor> tasks) {
    for (FolderProcessor item : tasks) {
      list.addAll(item.join());
    }
  }

  private boolean checkFile(String name) {
    return name.endsWith(extension);
  }

}
TOP

Related Classes of wwww.FolderProcessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.