CS2005 - Lecture 10 - Threads Flashcards
(27 cards)
What is a thread in the context of process management?
A thread is the basic unit of CPU utilization; It includes a thread ID, program counter, register set, and stack; Threads share code, data, and OS resources with other threads of the same process
What is the main benefit of using threads in applications?
Threads enable simultaneous execution of tasks, improving responsiveness and CPU utilization; They are lightweight and more economical than creating new processes
What are the differences between concurrency and parallelism?
Concurrency: Tasks appear to run in parallel through time-slicing; not necessarily at the same time; Parallelism: Tasks actually run at the same time on multiple processors or cores
How does sequential execution differ from concurrent and parallel execution?
Sequential: One task executes at a time, others must wait; Concurrent: Multiple tasks are interleaved in execution; Parallel: Multiple tasks execute simultaneously on separate CPUs/cores
What is the lifecycle of a Java thread?
NEW → RUNNABLE → RUNNING → may go to WAITING, TIMED_WAITING, BLOCKED, or TERMINATED
What happens when a thread enters the WAITING state?
It waits indefinitely for another thread to perform an action (e.g., wait() called)
What is Amdahl’s Law and how does it relate to multicore performance?
Amdahl’s Law estimates the potential speedup of a program using multiple cores; Formula: speedup ≤ 1 / (S + (1 - S)/N) where S = serial fraction, N = number of cores
What are the advantages of multithreaded server architecture?
Efficient handling of multiple client requests using threads instead of processes; Lower overhead and quicker context switching
How can threads be created in Java?
By extending the Thread class and overriding run(); By implementing the Runnable interface
Why is multithreading beneficial in multicore systems?
It allows actual parallel execution of threads, improving performance and scalability
What does the RUNNABLE state mean in Java thread lifecycle?
The thread is ready to run and waiting for CPU time from the thread scheduler
What are the key benefits of threads over processes?
Faster creation and context switching; Shared memory space; Improved responsiveness; Better scalability on multicore systems
How does Java put a thread into TIMED_WAITING state?
Using methods like sleep(millis) or wait(millis)
What is the difference between BLOCKED and WAITING states in Java?
BLOCKED: Waiting for a lock; WAITING: Waiting indefinitely for an event or action
What are common use cases for threads in desktop applications?
UI responsiveness (e.g., UI thread + background threads); Networking (e.g., downloading while rendering UI); Parallel computations
What methods are commonly used to control thread behavior in Java?
start(), run(), sleep(), wait(), notify(), interrupt()
What happens when a thread finishes execution?
It transitions to the TERMINATED state
Why is thread creation more efficient than process creation?
Thread creation uses shared memory and resources of the process, avoiding expensive memory allocation and setup steps
In what scenario would concurrency not result in performance gain?
On single-core systems where tasks are context-switched rather than executed in parallel; overhead may even reduce performance
Why is synchronization important in concurrent and parallel systems?
To prevent race conditions, ensure data integrity, and avoid deadlocks when multiple threads access shared data
What are some risks or drawbacks of using multithreading?
Deadlocks, race conditions, difficult debugging, increased complexity, and nondeterministic behavior
Compare RUNNABLE vs. RUNNING in Java threads.
RUNNABLE: Eligible to run but waiting for CPU; RUNNING: Currently executing on a CPU
Describe an application that benefits from both concurrency and parallelism.
A video game: concurrent threads for input, rendering, and audio; parallel execution on multicore systems improves performance
How does multithreading improve responsiveness in GUI applications?
Background threads handle long-running tasks (e.g., file download), allowing the UI thread to stay interactive