O'Reilly logo

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

Learning Concurrent Programming in Scala

Book Description

Learn the art of building intricate, modern, scalable concurrent applications using Scala

In Detail

This book will give you an insight into the best practices necessary to build concurrent programs in Scala using modern, high-level concurrency libraries. It starts by introducing you to the foundations of concurrent programming on the JVM, outlining the basics of the Java Memory Model, and then shows some of the classic building blocks of concurrency, such as the atomic variables, thread pools, and concurrent data structures, along with the caveats of traditional concurrency. It then walks you through different high-level concurrency abstractions, each tailored toward a specific class of programming tasks. Finally, the book presents an overview of when to use which concurrency library and demonstrates how they all work together.

What You Will Learn

  • Get to grips with the fundamentals of concurrent programming on modern multiprocessor systems, with a particular focus on the JVM concurrency model
  • Build high-performance concurrent systems from simple, low-level concurrency primitives
  • Express asynchrony in concurrent computations with futures and promises
  • Seamlessly accelerate sequential programs by using data-parallel collections
  • Implement reactive and event-based programs declaratively with Rx-style event streams
  • Design safe, scalable, and easy-to-comprehend in-memory transactional data models
  • Transparently create distributed applications that scale across multiple machines
  • Choose the correct concurrency abstraction and integrate different concurrency frameworks together in large applications

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Learning Concurrent Programming in Scala
    1. Table of Contents
    2. Learning Concurrent Programming in Scala
    3. Credits
    4. Foreword
    5. About the Author
    6. Acknowledgments
    7. About the Reviewers
    8. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    9. Preface
      1. How this book is organized
      2. What this book covers
      3. What you need for this book
        1. Installing the JDK
        2. Installing and using SBT
        3. Using Eclipse, IntelliJ IDEA, or another IDE
      4. Who this book is for
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    10. 1. Introduction
      1. Concurrent programming
        1. A brief overview of traditional concurrency
        2. Modern concurrency paradigms
      2. The advantages of Scala
      3. Preliminaries
        1. Execution of a Scala program
        2. A Scala primer
      4. Summary
      5. Exercises
    11. 2. Concurrency on the JVM and the Java Memory Model
      1. Processes and Threads
        1. Creating and starting threads
        2. Atomic execution
        3. Reordering
      2. Monitors and synchronization
        1. Deadlocks
        2. Guarded blocks
        3. Interrupting threads and the graceful shutdown
      3. Volatile variables
      4. The Java Memory Model
        1. Immutable objects and final fields
      5. Summary
      6. Exercises
    12. 3. Traditional Building Blocks of Concurrency
      1. The Executor and ExecutionContext objects
      2. Atomic primitives
        1. Atomic variables
        2. Lock-free programming
        3. Implementing locks explicitly
        4. The ABA problem
      3. Lazy values
      4. Concurrent collections
        1. Concurrent queues
        2. Concurrent sets and maps
        3. Concurrent traversals
      5. Creating and handling processes
      6. Summary
      7. Exercises
    13. 4. Asynchronous Programming with Futures and Promises
      1. Futures
        1. Starting future computations
        2. Future callbacks
        3. Futures and exceptions
        4. Using the Try type
        5. Fatal exceptions
        6. Functional composition on futures
      2. Promises
        1. Converting callback-based APIs
        2. Extending the future API
        3. Cancellation of asynchronous computations
      3. Futures and blocking
        1. Awaiting futures
        2. Blocking in asynchronous computations
      4. The Scala Async library
      5. Alternative Future frameworks
      6. Summary
      7. Exercises
    14. 5. Data-Parallel Collections
      1. Scala collections in a nutshell
      2. Using parallel collections
        1. Parallel collection class hierarchy
        2. Configuring the parallelism level
        3. Measuring the performance on the JVM
      3. Caveats of parallel collections
        1. Non-parallelizable collections
        2. Non-parallelizable operations
        3. Side effects in parallel operations
        4. Nondeterministic parallel operations
        5. Commutative and associative operators
      4. Using parallel and concurrent collections together
        1. Weakly consistent iterators
      5. Implementing custom parallel collections
        1. Splitters
        2. Combiners
      6. Alternative data-parallel frameworks
        1. Collections hierarchy in ScalaBlitz
      7. Summary
      8. Exercises
    15. 6. Concurrent Programming with Reactive Extensions
      1. Creating Observable objects
        1. Observables and exceptions
        2. The Observable contract
        3. Implementing custom Observable objects
        4. Creating Observables from futures
        5. Subscriptions
      2. Composing Observable objects
        1. Nested observables
        2. Failure handling in observables
      3. Rx schedulers
        1. Using custom schedulers for UI applications
      4. Subjects and top-down reactive programming
      5. Summary
      6. Exercises
    16. 7. Software Transactional Memory
      1. The trouble with atomic variables
      2. Using Software Transactional Memory
        1. Transactional references
        2. Using the atomic statement
      3. Composing transactions
        1. The interaction between transactions and side effects
        2. Single-operation transactions
        3. Nesting transactions
        4. Transactions and exceptions
      4. Retrying transactions
        1. Retrying with timeouts
      5. Transactional collections
        1. Transaction-local variables
        2. Transactional arrays
        3. Transactional maps
      6. Summary
      7. Exercises
    17. 8. Actors
      1. Working with actors
        1. Creating actor systems and actors
        2. Managing unhandled messages
        3. Actor behavior and state
        4. Akka actor hierarchy
        5. Identifying actors
        6. The actor life cycle
      2. Communication between actors
        1. The ask pattern
        2. The forward pattern
        3. Stopping actors
      3. Actor supervision
      4. Remote actors
      5. Summary
      6. Exercises
    18. 9. Concurrency in Practice
      1. Choosing the right tools for the job
      2. Putting it all together – a remote file browser
        1. Modeling the filesystem
        2. The server interface
        3. Client navigation API
        4. The client user interface
        5. Implementing the client logic
        6. Improving the remote file browser
      3. Debugging concurrent programs
        1. Deadlocks and lack of progress
        2. Debugging incorrect program outputs
        3. Performance debugging
      4. Summary
      5. Exercises
    19. Index