O'Reilly logo

Scala Functional Programming Patterns 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

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

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