From "The Pragmatic Programmer"
🎧 Listen to Summary
Free 10-min PreviewFundamental Concepts of Concurrency and Parallelism & Temporal Coupling
Key Insight
Concurrency describes when the execution of two or more pieces of code act as if they run simultaneously, typically implemented via software constructs such as fibers, threads, and processes. Parallelism, in contrast, occurs when code truly runs at the same time, necessitating hardware capabilities like multiple CPU cores, multiple CPUs, or networked computers. Concurrency is an essential requirement for most decent-sized systems, enabling applications to handle the real world's asynchronous nature, where user interactions, data fetching, and external service calls often occur simultaneously. Forcing these operations into a serial sequence results in sluggish systems and fails to leverage available hardware power.
A significant challenge in concurrent systems is 'temporal coupling,' a form of dependency that imposes an unrequired execution sequence on operations, hindering flexibility. Examples include rigid method call orders like 'A before B,' or sequential calls to multiple backend services when parallel execution is possible. To enhance flexibility and system responsiveness, it is crucial to identify and decouple these time-based dependencies. Workflow analysis, often using activity diagrams, is a key technique to discover what activities can happen concurrently and what must maintain a strict order, thereby maximizing opportunities for parallelism.
Activity diagrams consist of rounded boxes representing actions connected by arrows for sequencing, and thick 'synchronization bars' indicating points where multiple preceding actions must complete before subsequent actions can begin. This notation helps visualize and exploit concurrency. For example, in a robotic piña colada maker, an activity diagram reveals that initial tasks (e.g., opening blender, opening mix, measuring rum) can occur concurrently, as can later tasks (e.g., adding mix, pouring rum, adding ice). Such diagrams highlight opportunities for concurrency in activities that consume significant 'real world' time but not CPU cycles, such as querying databases, accessing external services, or waiting for user input, allowing the CPU to perform other productive work.
📚 Continue Your Learning Journey — No Payment Required
Access the complete The Pragmatic Programmer summary with audio narration, key takeaways, and actionable insights from Andrew Hunt, David Thomas.