This page presents a possible solution to point 4. of
CP Lab 2.
The code for the parallel version of Search is given as
ParSearch.java.
The solution has been obtained by making the following changes to the
original Search.java program.
The algorithm of the search() method has been moved to
the run() method of a new Thread-derived class called
Searcher.
The Searcher class is parameterized with the text to
search, and indices of the start and end of the segment to search.
(This avoids copying the original string. We can safely assume
that concurrent reading of elements in the common string can be done
atomically.)
The result of each search is stored in a Vector field local to
each thread.
The requested number of threads are created and passed properly
overlapping sections of the text.
The threads are run concurrently and their termination awaited.
Finally the result vectors are scanned sequentially in order to
produce the overall result.