Which of the following best explains why threads allow a program to perform multiple tasks at the same time?
Think about how sharing memory helps tasks communicate and run together.
Threads share the same memory space, which allows them to run parts of a program concurrently and communicate easily. This enables multiple tasks to progress at the same time without waiting for others to finish.
Which feature of threads directly supports concurrent execution within a single process?
Consider what resources threads share that help them work together.
Threads share the same process resources such as memory and open files, which allows them to work concurrently within the same program efficiently.
Consider two threads incrementing the same counter variable 3 times each without synchronization. What is a possible final value of the counter?
counter = 0 # Thread 1 increments counter 3 times # Thread 2 increments counter 3 times # No locks or synchronization used
Think about what happens when two threads update the same variable without coordination.
Without synchronization, increments can overlap causing some increments to be lost. So the final counter can be anywhere between 3 (only one thread's increments counted) and 6 (all increments counted).
Which explanation best describes how threads help a CPU stay busy and improve performance?
Consider how switching between tasks helps when some tasks wait for input or resources.
Threads let the CPU switch between tasks quickly, so when one thread waits (like for input), the CPU can work on another thread. This reduces idle time and improves overall performance.
Why can using multiple threads in a program sometimes cause unexpected errors?
Think about what happens when multiple threads access the same data at the same time.
Since threads share memory, if two or more threads try to change the same data simultaneously without proper controls, it can cause errors like corrupted data or crashes.