Concurrency in Problem Solving (OCR A-Level Computer Science): Revision Notes
📚 Revision Notes
Concurrency in Problem Solving
Overview
Concurrency in computational thinking refers to solving different parts of a problem simultaneously rather than sequentially. This approach improves efficiency and performance, especially in tasks where multiple operations can occur independently.
Understanding concurrency helps identify tasks that can run in parallel and those that depend on each other, leading to more efficient programme design and execution.
What is Concurrency?
- Definition: The ability to execute multiple tasks or processes simultaneously or overlap their execution.
- Purpose: To optimise resource usage and reduce total execution time.
- Examples:
- Loading a webpage while downloading images.
- Processing multiple transactions in a banking system concurrently.
Thinking Concurrently
- Involves analysing a problem to determine:
- Which tasks can run independently?
- Which tasks are dependent on the completion of others.
Identifying Concurrent Parts of a Problem
- Analyse the Problem: Break down the problem into smaller tasks.
- Determine Dependencies:
- Identify tasks that must be completed before others can start.
- Example: In a recipe, you cannot bake a cake until the batter is mixed.
- Identify Independent Tasks:
- Tasks that can be executed in parallel without affecting the correctness of the solution.
- Example: Washing dishes and cleaning the countertop can be done simultaneously.
Example Scenarios
infoNote
Scenario 1: Online Shopping System
- Tasks:
- Process user payment.
- Update product inventory.
- Send order confirmation email.
- Concurrency Analysis:
- Payment processing and inventory updates must be completed before the confirmation email is sent.
- However, updating inventory and preparing the email draught can occur concurrently.
infoNote
Scenario 2: Web Page Loading
- Tasks:
- Load the HTML structure.
- Download images.
- Load CSS styles.
- Load JavaScript for interactivity.
- Concurrency Analysis:
- Images, CSS, and JavaScript can be downloaded concurrently.
- However, applying CSS and JavaScript depends on the HTML being loaded first.
infoNote
Scenario 3: Video Processing
- Tasks:
- Extract audio.
- Process video frames.
- Combine audio and video.
- Concurrency Analysis:
- Audio extraction and video processing can be done concurrently.
- Combining them requires both tasks to be completed first.
Benefits of Concurrency
- Increased Efficiency: Tasks are completed faster by utilising system resources more effectively.
- Improved Resource Utilisation: Makes better use of multi-core processors and distributed systems.
- Reduced Latency: Users experience faster responses as tasks like loading content or processing requests can be done simultaneously.
Challenges of Concurrency
- Data Dependency Issues: If tasks share and modify the same data, conflicts can arise, leading to race conditions or inconsistent states.
- Example: Two tasks update the same database record concurrently.
- Deadlocks: When two or more tasks wait indefinitely for each other to complete, leading to a standstill.
- Increased Complexity: Designing and debugging concurrent programmes is more challenging due to the need for synchronisation and managing dependencies.
Tools and Techniques for Concurrency
- Threading:
- Using multiple threads to execute tasks concurrently.
- Example: In Python, the threading module allows concurrent execution of tasks.
- Asynchronous Programming:
- Tasks are initiated without waiting for their completion, improving programme responsiveness.
- Example: JavaScript's async/await for handling asynchronous operations.
- Parallel Processing:
- Divides a task into subtasks that are processed simultaneously on multiple processors.
- Example: Distributed systems like Hadoop for processing large data sets.
Note Summary
infoNote
Common Mistakes
- Incorrect Dependency Identification: Failing to recognise dependent tasks may lead to incorrect results.
- Poor Synchronisation: Not properly coordinating tasks can lead to data inconsistencies or programme crashes.
- Overusing Concurrency: Introducing concurrency where it is unnecessary can complicate the programme without significant performance benefits.
infoNote
Key Takeaways
- Concurrency allows multiple tasks to be executed simultaneously, improving efficiency and performance.
- Identifying independent and dependent tasks is crucial for designing concurrent solutions.
- While concurrency offers significant benefits, it introduces challenges like data dependency issues and increased complexity.
- Techniques such as threading, asynchronous programming, and parallel processing can be used to implement concurrency effectively.