Why use a recursive task?

The tasks could fork more subtasks themselves . To look at this aspect, let's extend the preceding egrep program to allow a recursive grep—given a directory, the program will recursively find all occurrences of the word in all the files in the directory tree.

We change the previous code suitably, as shown here: 

public class EgrepWord1 {  private final static ForkJoinPool forkJoinPool = new ForkJoinPool();  private static class WordFinder extends RecursiveTask<List<String>> {    final File file;    final String word;    private WordFinder(File file, String word) {      this.file = file;      this.word = word;    }

The task works on two kinds of files: if it is a directory, the task enters the directory and spawns off more subtasks to process ...

Get Concurrent Patterns and Best Practices now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.