Threads – masters and slaves
One more concept at play here is of masters and slaves (also known as workers). The master spawns off a bunch of workers that are connected via a queue.
Then, a worker thread recurs in the directory. Once it finds a file with a .txt
extension, it hands over the file to the egrep worker thread, as shown in the following figure. The egrep worker searches for the pattern in the file and prints out the matching lines:
The master thread recursively traverses the directory, looking for files with the .txt
extension. Here is an example of the Java code. We've used the excellent Apache commons io
library for the file operations.
Here is how the Java code looks:
public class FilesFinder extends DirectoryWalker implements Runnable { // 1 private final String directory; private final BlockingQueue<File> inputQueue; // 2 public FilesFinder(final String directory, final BlockingQueue<File> inputQueue) { ...