CS2005 - Lecture 10 - Threads Flashcards

(26 cards)

1
Q

What is a thread in the context of process management?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the main benefit of using threads in applications?

A

Threads enable simultaneous execution of tasks, improving responsiveness and CPU utilization
They are lightweight and more economical than creating new processes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the differences between concurrency and parallelism?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does sequential execution differ from concurrent and parallel execution?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the lifecycle of a Java thread?

A

NEW → RUNNABLE → RUNNING → may go to WAITING, TIMED_WAITING, BLOCKED, or TERMINATED

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens when a thread enters the WAITING state?

A

It waits indefinitely for another thread to perform an action (e.g., wait() called)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the advantages of multithreaded server architecture?

A

Efficient handling of multiple client requests using threads instead of processes; Lower overhead and quicker context switching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can threads be created in Java?

A

By extending the Thread class and overriding run()
By implementing the Runnable interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is multithreading beneficial in multicore systems?

A

It allows actual parallel execution of threads, improving performance and scalability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the RUNNABLE state mean in Java thread lifecycle?

A

The thread is ready to run and waiting for CPU time from the thread scheduler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the key benefits of threads over processes?

A

Faster creation and context switching; Shared memory space; Improved responsiveness; Better scalability on multicore systems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Java put a thread into TIMED_WAITING state?

A

Using methods like sleep(millis) or wait(millis)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between BLOCKED and WAITING states in Java?

A

BLOCKED: Waiting for a lock
WAITING: Waiting indefinitely for an event or action

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are common use cases for threads in desktop applications?

A

UI responsiveness (e.g., UI thread + background threads); Networking (e.g., downloading while rendering UI); Parallel computations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What methods are commonly used to control thread behavior in Java?

A

start(), run(), sleep(), wait(), notify(), interrupt()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens when a thread finishes execution?

A

It transitions to the TERMINATED state

17
Q

Why is thread creation more efficient than process creation?

A

Thread creation uses shared memory and resources of the process, avoiding expensive memory allocation and setup steps

18
Q

In what scenario would concurrency not result in performance gain?

A

On single-core systems where tasks are context-switched rather than executed in parallel; overhead may even reduce performance

19
Q

Why is synchronization important in concurrent and parallel systems?

A

To prevent race conditions, ensure data integrity, and avoid deadlocks when multiple threads access shared data

20
Q

What are some risks or drawbacks of using multithreading?

A

Deadlocks, race conditions, difficult debugging, increased complexity, and nondeterministic behavior

21
Q

Compare RUNNABLE vs. RUNNING in Java threads.

A

RUNNABLE: Eligible to run but waiting for CPU
RUNNING: Currently executing on a CPU

22
Q

Describe an application that benefits from both concurrency and parallelism.

A

A video game: concurrent threads for input, rendering, and audio; parallel execution on multicore systems improves performance

23
Q

How does multithreading improve responsiveness in GUI applications?

A

Background threads handle long-running tasks (e.g., file download), allowing the UI thread to stay interactive

24
Q

When would you prefer implementing Runnable over extending Thread?

A

When you need to inherit from another class (Java doesn’t support multiple inheritance), or want to decouple task logic from threading

25
What happens if you call run() directly instead of start() on a thread?
It will execute like a normal method call in the current thread, not in a separate thread
26
How does Java handle thread scheduling?
It's managed by the JVM and OS; generally based on thread priority and time-slicing