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());