Using parallel and concurrent collections together
We have already seen that parallel collection operations are not allowed to access mutable states without the use of synchronization. This includes modifying sequential Scala collections from within a parallel operation. Recall that we used a mutable variable in the section on side effects to count the size of the intersection. In the following example, we will download the URL and HTML specifications, convert them to sets of words, and try to find an intersection of their words. In the intersection method, we use a HashSet collection and update it in parallel. Collections in the scala.collection.mutable package are not thread-safe. The following example nondeterministically drops elements, corrupts the buffer state, or throws exceptions:
object ConcurrentWrong extends App {
import ParHtmlSearch.getHtmlSpec
import ch4.FuturesCallbacks.getUrlSpec
def intersection(a: GenSet[String], b: GenSet[String]) = {
val...