7. The Concurrent Programming Paradigm
7.1. Overview
Why and when do we need concurrency?
When it is a natural fit for the problem domain
multiple autonomous behaviors/simulations
user interfaces: timed events, background activities
When the technical solution domain requires it
more efficient use of available resources: asynchronous computing
graphical user interfaces: queuing of low-level input events
multi-core systems
network services/distributed systems
Key considerations:
physical (parallelism) versus logical concurrency
speedup and when to expect it
data parallelism versus task parallelism
7.2. Activity terminology and concerns
process: own memory
thread: shared memory and “thread-local” state
foreground versus background
CPU-bound versus IO-bound
run-to-completion versus coordination
progress reporting
cancelation
7.3. Thread safety
nondeterminism
extent of nondeterminism: see subsection below
race conditions
root cause of thread safety problems
7.3.1. Understanding the extent of nondeterminism
Consider this small example of two concurrent increment operations:
/*f1*/ final int local1 = shared; /*f2*/ final int local2 = shared;
/*s1*/ shared = local1 + 1; /*s2*/ shared = local2 + 1;
When analyzing race conditions, we might be tempted to enumerate the different possible interleavings. While it seems reasonable for this example, this quickly becomes impractical because of the combinatorial explosion for larger number of threads with more steps. (Please see the CDER chapter for more details.)
To appreciate this combinatorial explosion, let’s count the possible interleavings for the case of \(k\) threads with \(n\) steps each. We recall the binomial coefficient \(i\) choose \(j\) defined as
In our case, there are \(kn\) steps, of which the first thread chooses \(n\); there are \(\binom{kn}{n}\) possibilities for this. This leaves \((k-1)n\) steps, of which the second thread chooses \(n\), and so on. At the end, there are \(n\) steps left, which are the only choice for the last thread. The total number of choices is the product of choices for each thread:
Here the second factor in each denominator cancels out against the numerator of the next top-level factor and the second factor in the last denominator is \(1\), leaving
As the number of threads and/or their number of steps grow beyond two, the number interleavings gets very large.
Therefore, we cannot attempt to comprehend, let alone enumerate, all possible interleavings. Instead, we need to think in terms of constraints, e.g., f1 always happens before s1, and f2 always happens before s2.
Once we make each thread atomic, however, the number of interleavings shrinks dramatically to \(k!\).
7.5. (Conflicting) design forces
correctness/(thread-)safety
liveness/deadlock
fairness/starvation
performance
throughput
latency
jitter
7.6. Specific concurrency mechanisms
Language constructs, patterns, building blocks:
threads (familiar from 313/413)
monitors: synchronized/locks, wait/notify
fully synchronized object (pattern/building blocks)
Android (also familiar from 313/413)
-
atomic variables
thread-safe collections
FIFO locks
…
reactive streams including Akka streams
7.7. References: concurrent and asynchronous computing
Läufer and Thiruvathukal, CDER book chapter
Goetz et al., JCIP
Doug Lea, CPJ
Thiruvathukal and Christopher, HPJPC