Why threads enable concurrent execution in Operating Systems - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how using threads affects the time it takes to run tasks.
Specifically, how does splitting work into threads change the total execution time as tasks grow?
Analyze the time complexity of this simple threaded task execution.
for each task in tasks:
create a thread to run task
wait for all threads to finish
This code runs multiple tasks by creating a thread for each, then waits for all to complete.
Look at what repeats and takes time:
- Primary operation: Creating and running a thread for each task
- How many times: Once per task, so as many times as there are tasks
When tasks increase, threads run many tasks at the same time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 threads running mostly together |
| 100 | About 100 threads running mostly together |
| 1000 | About 1000 threads running mostly together |
Pattern observation: The total time depends on the longest task, not the number of tasks, because threads run tasks concurrently.
Time Complexity: O(t)
This means the total time grows with the longest single task time, not the total number of tasks.
[X] Wrong: "More tasks always mean more total time because each task adds time."
[OK] Correct: Threads let tasks run at the same time, so total time depends on the longest task, not the count.
Understanding how threads let tasks run together helps you explain real-world programs that do many things at once efficiently.
"What if tasks have to share a resource and wait for each other? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand what concurrent execution means
Concurrent execution means running multiple tasks at the same time or overlapping in time.Step 2: Identify how threads work within a program
Threads allow different parts of a program to run independently but share the same memory, enabling multiple tasks to happen simultaneously.Final Answer:
Because threads allow multiple tasks to run at the same time within a single program -> Option AQuick Check:
Threads = multiple tasks at once [OK]
- Thinking threads use separate memory spaces
- Believing threads prevent simultaneous tasks
- Assuming threads slow down programs
Solution
Step 1: Recall common thread creation syntax
Many languages use a Thread object with a start() method to begin execution.Step 2: Compare options to correct syntax
new Thread(task).start() matches the common pattern: creating a Thread with a task and calling start() to run it.Final Answer:
new Thread(task).start() -> Option DQuick Check:
Thread creation = new Thread(...).start() [OK]
- Using run() instead of start() to begin thread
- Writing incorrect keywords like 'create thread'
- Confusing thread creation with task execution
start thread A: print("Hello")
start thread B: print("World")What is a possible output?
Solution
Step 1: Understand thread execution order
Threads run independently and may execute in any order or overlap.Step 2: Analyze possible outputs
Since thread A and B print different words, output order can vary: "Hello World" or "World Hello".Final Answer:
Either 'Hello World' or 'World Hello' -> Option CQuick Check:
Thread output order varies = Either 'Hello World' or 'World Hello' [OK]
- Assuming threads always run in start order
- Expecting combined outputs like 'HelloHello'
- Ignoring concurrency effects on output order
Thread t = new Thread(); t.run();
Solution
Step 1: Identify how to start a thread properly
Calling run() directly runs the code in the current thread, not a new thread.Step 2: Correct method to start a thread
Using start() launches the thread to run concurrently.Final Answer:
It should call t.start() to run the thread concurrently -> Option AQuick Check:
Use start() to run thread concurrently [OK]
- Calling run() instead of start()
- Not providing a task to the thread
- Thinking threads need names to run
Solution
Step 1: Understand shared data risks in threads
When multiple threads access shared data, race conditions can cause errors.Step 2: Identify how to prevent race conditions
Synchronization (like locks) ensures only one thread updates the counter at a time, preventing conflicts.Final Answer:
Use synchronization methods to control access to the counter -> Option BQuick Check:
Synchronize shared data access = Use synchronization methods to control access to the counter [OK]
- Letting threads update shared data simultaneously
- Ignoring synchronization needs
- Avoiding threads instead of managing access
