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 - Second Edition

Book Description

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

About This Book

  • Make the most of Scala by understanding its philosophy and harnessing the power of multicores
  • Get acquainted with cutting-edge technologies in the field of concurrency, through practical, real-world applications
  • Get this step-by-step guide packed with pragmatic examples

Who This Book Is For

If you are a Scala programmer with no prior knowledge about concurrent programming, or seeking to broaden your existing knowledge about concurrency, this book is for you. Basic knowledge of the Scala programming language will be helpful. Also if you have a solid knowledge in another programming language, such as Java, you should find this book easily accessible.

What You Will Learn

  • Get to grips with the fundamentals of concurrent programming on modern multiprocessor systems.
  • 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
  • Design safe, scalable, and easy-to-comprehend in-memory transactional data models
  • Transparently create distributed applications that scale across multiple machines
  • Integrate different concurrency frameworks together in large applications
  • Develop and implement scalable and easy-to-understand concurrent applications in Scala 2.12

In Detail

Scala is a modern, multiparadigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Scala smoothly integrates the features of object-oriented and functional languages.

In this second edition, you will find updated coverage of the Scala 2.12 platform. The Scala 2.12 series targets Java 8 and requires it for execution. The book 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.

The book then walks you through different high-level concurrency abstractions, each tailored toward a specific class of programming tasks, while touching on the latest advancements of async programming capabilities of Scala. It also covers some useful patterns and idioms to use with the techniques described. Finally, the book presents an overview of when to use which concurrency library and demonstrates how they all work together, and then presents new exciting approaches to building concurrent and distributed systems.

Style and approach

The book provides a step-by-step introduction to concurrent programming. It focuses on easy-to-understand examples that are pragmatic and applicable to real-world applications. Different topics are approached in a bottom-up fashion, gradually going from the simplest foundations to the most advanced features.

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

Table of Contents

  1. Learning Concurrent Programming in Scala - Second Edition
    1. Learning Concurrent Programming in Scala - Second Edition
    2. Credits
    3. Foreword
    4. About the Author
    5. Acknowledgements
    6. About the Reviewers
    7. www.PacktPub.com
      1. Why subscribe?
    8. Customer Feedback
    9. Preface
      1. What this book covers
      2. What you need for this book
        1. Installing the JDK
        2. Installing and using SBT
        3. Using Eclipse, IntelliJ IDEA, or another IDE
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. 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. Overview of new features in Scala 2.12
      5. Summary
      6. 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. Custom concurrent data structures
        1. Implementing a lock-free concurrent pool
        2. 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 with 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. Summary
      7. 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 lifecycle
      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. 10. Reactors
      1. The need for reactors
      2. Getting started with Reactors
      3. The "Hello World" program
      4. Event streams
        1. Lifecycle of an event stream
        2. Functional composition of event streams
      5. Reactors
        1. Defining and configuring reactors
        2. Using channels
      6. Schedulers
      7. Reactor lifecycle
      8. Reactor system services
        1. The logging service
        2. The clock service
        3. The channels service
        4. Custom services
      9. Protocols
        1. Custom server-client protocol
        2. Standard server-client protocol
          1. Using an existing connector
          2. Creating a new connector
          3. Creating a protocol-specific reactor prototype
          4. Spawning a protocol-specific reactor directly
        3. Router protocol
        4. Two-way protocol
      10. Summary
      11. Exercises