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:

Threads – masters and slaves

Figure 10.3: Master/slave threads

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

Get Scala Functional Programming Patterns now with O’Reilly online learning.

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