10 Concurrent Programs

 

In this chapter you will learn how to

  • declaratively design concurrent program flows
  • use lightweight virtual threads (fibers)
  • safely store and access data from different threads
  • process stream of events asynchronously

We know the past but cannot control it.

We control the future but cannot know it.

— Claud Shannon

10.1 Threads... threads everywhere

So far in the book we’ve been focusing on sequential programs: each program consisted of a sequence of expressions which was evaluated one by one using a single execution thread, usually connected to a single core:

We won’t be focusing on cores (or CPUs) in this chapter. We will focus on having multiple threads. Note that multiple threads can still run on a single core. Operating system switches between different threads to make sure everyone gets a chance to progress.

10_04

10.2 Declarative concurrency

10.3 Sequential vs concurrent

10.4 Coffee Break: Sequential thinking

10.5 Coffee Break Explained: Sequential thinking

10.6 The need for batching

10.7 Batching implementation

10.8 The concurrent world

10.9 The concurrent state

10.10 Imperative concurrency

10.11 Atomic references

10.12 Introducing Ref

10.13 Updating Ref values

10.14 Using Ref values

10.15 Making it all concurrent

10.16 parSequence in action

10.17 Practicing concurrent IOs

10.18 Modeling concurrency

10.19 Coding using Refs and fibers

10.20 IOs that run infinitely

10.21 Coffee Break: Concurrent thinking

10.22 Coffee Break Explained: Concurrent thinking

10.23 The need for asynchronicity

10.24 Preparing for asynchronous access

10.25 Designing functional asynchronous programs

10.26 Managing fibers manually

10.27 Coding functional asynchronous programs

10.28 Summary