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
Get Scala Functional Programming Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.