Bird
Raised Fist0
Operating Systemsknowledge~20 mins

Thread pools in Operating Systems - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Thread Pool Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main advantage of using a thread pool?

Thread pools are used in many systems to manage multiple tasks efficiently. What is the primary benefit of using a thread pool instead of creating a new thread for each task?

AIt reduces the overhead of creating and destroying threads repeatedly.
BIt guarantees that tasks will run in the order they are received.
CIt allows threads to run without any synchronization mechanisms.
DIt ensures that only one task runs at a time.
Attempts:
2 left
💡 Hint

Think about what happens when threads are created and destroyed frequently.

📋 Factual
intermediate
2:00remaining
How does a thread pool manage tasks when all threads are busy?

In a thread pool, what happens to new tasks when all threads are currently executing other tasks?

ANew tasks are rejected and lost.
BNew tasks are placed in a queue to wait until a thread becomes free.
CNew threads are created immediately to handle the tasks.
DThe system pauses all running threads to make room for new tasks.
Attempts:
2 left
💡 Hint

Consider how a thread pool controls the number of active threads.

🔍 Analysis
advanced
2:00remaining
Identify the problem caused by an unbounded task queue in a thread pool

What issue can arise if a thread pool uses an unbounded queue to hold waiting tasks?

AThe system may run out of memory due to too many queued tasks.
BTasks will be executed out of order.
CThreads will be created beyond the pool size limit.
DTasks will be rejected immediately when the queue is full.
Attempts:
2 left
💡 Hint

Think about what happens if tasks keep arriving faster than they can be processed.

Comparison
advanced
2:00remaining
Difference between fixed-size and cached thread pools

Which statement correctly describes the difference between a fixed-size thread pool and a cached thread pool?

ACached pools reject tasks when all threads are busy; fixed-size pools queue tasks indefinitely.
BFixed-size pools create threads on demand; cached pools have a fixed number of threads.
CFixed-size pools have a set number of threads; cached pools create new threads as needed and reuse idle ones.
DBoth pools create unlimited threads but differ in task queue size.
Attempts:
2 left
💡 Hint

Consider how each pool manages thread creation and reuse.

Reasoning
expert
3:00remaining
Why might a thread pool cause deadlocks in certain applications?

Consider an application using a thread pool where tasks submit other tasks to the same pool and wait for their completion. Why can this design cause deadlocks?

ABecause the thread pool automatically cancels tasks that wait for others.
BBecause thread pools do not support nested task submission.
CBecause tasks submitted later always have lower priority and never run.
DBecause all threads may be blocked waiting for subtasks, no thread is free to run those subtasks.
Attempts:
2 left
💡 Hint

Think about what happens if threads are waiting for other threads that cannot start.

Practice

(1/5)
1. What is the main purpose of a thread pool in an operating system?
easy
A. To create a new thread for every task without limit
B. To store data permanently on disk
C. To reuse a fixed number of threads to run multiple tasks efficiently
D. To manage memory allocation for processes

Solution

  1. Step 1: Understand thread pool concept

    A thread pool manages a fixed number of threads to handle many tasks efficiently without creating new threads each time.
  2. Step 2: Compare options with thread pool purpose

    Only To reuse a fixed number of threads to run multiple tasks efficiently correctly describes reusing threads for multiple tasks. Other options describe unrelated concepts.
  3. Final Answer:

    To reuse a fixed number of threads to run multiple tasks efficiently -> Option C
  4. Quick Check:

    Thread pool purpose = reuse threads [OK]
Hint: Thread pools reuse threads, not create new ones each time [OK]
Common Mistakes:
  • Thinking thread pools create unlimited threads
  • Confusing thread pools with memory management
  • Assuming thread pools store data permanently
2. Which of the following is the correct way to describe how tasks are handled in a thread pool?
easy
A. Tasks run only one at a time sequentially
B. Tasks are executed immediately without waiting
C. Tasks are discarded if no thread is free
D. Tasks wait in a queue if all threads are busy

Solution

  1. Step 1: Recall thread pool task management

    When all threads are busy, new tasks wait in a queue until a thread becomes free.
  2. Step 2: Evaluate options against this behavior

    Tasks wait in a queue if all threads are busy correctly states tasks wait in a queue. Other options are incorrect because tasks are not discarded or run sequentially only.
  3. Final Answer:

    Tasks wait in a queue if all threads are busy -> Option D
  4. Quick Check:

    Task queueing = waiting tasks [OK]
Hint: Tasks queue up when threads are busy, not discarded [OK]
Common Mistakes:
  • Believing tasks run immediately always
  • Thinking tasks get dropped if no thread is free
  • Assuming tasks run strictly one by one
3. Consider a thread pool with 3 threads. If 5 tasks are submitted at once, how many tasks will be running simultaneously?
medium
A. 3
B. 2
C. 5
D. 0

Solution

  1. Step 1: Understand thread pool capacity

    The thread pool has 3 threads, so it can run up to 3 tasks at the same time.
  2. Step 2: Analyze task submission

    When 5 tasks are submitted, 3 run immediately (one per thread), and 2 wait in the queue.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Threads limit running tasks = 3 [OK]
Hint: Running tasks = number of threads in pool [OK]
Common Mistakes:
  • Assuming all tasks run simultaneously regardless of thread count
  • Confusing queued tasks with running tasks
  • Thinking no tasks run if more than threads
4. A developer notices that tasks submitted to a thread pool never start running and remain queued indefinitely. What is the most likely cause?
medium
A. The thread pool size is set to zero
B. Tasks are too short to run
C. The queue is empty
D. The CPU is overloaded

Solution

  1. Step 1: Analyze thread pool size effect

    If the thread pool size is zero, no threads exist to run tasks, so tasks remain queued forever.
  2. Step 2: Evaluate other options

    Tasks being short or queue empty do not cause indefinite waiting. CPU overload may slow but not block all tasks.
  3. Final Answer:

    The thread pool size is set to zero -> Option A
  4. Quick Check:

    Zero threads means no task execution [OK]
Hint: Zero threads means no tasks run, causing indefinite queue [OK]
Common Mistakes:
  • Assuming short tasks cause waiting
  • Thinking empty queue causes waiting
  • Blaming CPU overload for all tasks stuck
5. You need to design a thread pool for a server that handles 100 simultaneous client requests. Which approach best balances resource use and performance?
hard
A. Create a thread pool with 100 threads to handle all requests at once
B. Create a thread pool with a fixed smaller number of threads (e.g., 10) and queue extra requests
C. Create a new thread for each request without pooling
D. Use a single thread to handle all requests sequentially

Solution

  1. Step 1: Consider resource limits

    Creating 100 threads can exhaust system resources and reduce performance.
  2. Step 2: Evaluate thread pool with queuing

    A fixed smaller thread pool (like 10 threads) efficiently reuses threads and queues extra requests, balancing load and resources.
  3. Step 3: Reject other options

    Creating new threads per request wastes resources; single thread causes slow sequential handling.
  4. Final Answer:

    Create a thread pool with a fixed smaller number of threads (e.g., 10) and queue extra requests -> Option B
  5. Quick Check:

    Fixed small pool + queue = balanced performance [OK]
Hint: Use fewer threads than requests, queue extras for efficiency [OK]
Common Mistakes:
  • Making thread pool size equal to requests
  • Creating new thread per request wastes resources
  • Using single thread causes slow processing