O'Reilly logo

Concurrent Patterns and Best Practices by Atul S. Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required