Book Image

Learning Concurrent Programming in Scala

By : Aleksandar Prokopec
5 (1)
Book Image

Learning Concurrent Programming in Scala

5 (1)
By: Aleksandar Prokopec

Overview of this book

Table of Contents (18 chapters)
Learning Concurrent Programming in Scala
Credits
Foreword
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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 result = new mutable...