26 Lecture
CS410
Midterm & Final Term Short Notes
Threads and Synchronization
Threads and synchronization are essential in concurrent programming. Threads are independent paths of execution within a program, enabling multitasking. Synchronization ensures threads coordinate and share data safely, preventing conflicts and e
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
**Question 1: What is a thread in the context of programming?**
a) A function call
b) A sequence of instructions
c) A graphical user interface element
d) An input/output operation
**Solution: b) A sequence of instructions**
**Question 2: What is the purpose of thread synchronization?**
a) To increase the number of threads
b) To reduce the number of threads
c) To coordinate thread execution and data access
d) To stop all threads simultaneously
**Solution: c) To coordinate thread execution and data access**
**Question 3: What is a race condition in multithreading?**
a) A competition between threads for system resources
b) A condition where two or more threads access shared data concurrently, leading to unexpected results
c) A condition where a thread fails to start
d) A synchronization mechanism
**Solution: b) A condition where two or more threads access shared data concurrently, leading to unexpected results**
**Question 4: Which of the following is a thread synchronization primitive?**
a) Thread.sleep()
b) Thread.start()
c) Thread.join()
d) Thread.run()
**Solution: c) Thread.join()**
**Question 5: What is the purpose of the "synchronized" keyword in Java?**
a) It creates a new thread
b) It marks a method as deprecated
c) It prevents a method from being overridden
d) It ensures exclusive access to a block of code by only one thread at a time
**Solution: d) It ensures exclusive access to a block of code by only one thread at a time**
**Question 6: What can be used to prevent deadlock in multithreaded programs?**
a) Increasing the number of threads
b) Decreasing the number of threads
c) Using thread.sleep()
d) Implementing a proper order for acquiring locks
**Solution: d) Implementing a proper order for acquiring locks**
**Question 7: Which synchronization primitive allows multiple threads to read a shared resource simultaneously, but only one thread to write?**
a) Semaphore
b) Mutex
c) ReadWriteLock
d) CountDownLatch
**Solution: c) ReadWriteLock**
**Question 8: What is a critical section in the context of synchronization?**
a) A section of code that only runs on a single thread
b) A section of code that must be executed by multiple threads concurrently
c) A section of code that is ignored by all threads
d) A section of code where errors are expected
**Solution: b) A section of code that must be executed by multiple threads concurrently**
**Question 9: Which of the following is a potential drawback of excessive thread synchronization?**
a) Deadlocks
b) Race conditions
c) Improved performance
d) Concurrent execution
**Solution: a) Deadlocks**
**Question 10: What is a mutex?**
a) A type of thread
b) A synchronization primitive that allows multiple threads to access a resource simultaneously
c) A synchronization primitive that ensures only one thread can access a resource at a time
d) A thread scheduler
**Solution: c) A synchronization primitive that ensures only one thread can access a resource at a time**
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
**Question 1: What is a thread?**
**Answer:** A thread is the smallest unit of execution within a process. It represents an independent sequence of instructions that can run concurrently with other threads, sharing the same resources such as memory and files.
**Question 2: What is thread synchronization?**
**Answer:** Thread synchronization is the coordination of multiple threads to ensure orderly execution and proper data sharing. It prevents race conditions and ensures data integrity when multiple threads access shared resources simultaneously.
**Question 3: Explain the concept of a race condition.**
**Answer:** A race condition occurs when two or more threads access shared data concurrently, leading to unexpected and potentially incorrect behavior due to the unpredictable order of execution. This can result in data corruption or inconsistent results.
**Question 4: What is a critical section?**
**Answer:** A critical section is a portion of code that must be executed by only one thread at a time to prevent race conditions. Synchronization mechanisms are used to ensure that only one thread can access the critical section at any given moment.
**Question 5: What is the purpose of using locks in thread synchronization?**
**Answer:** Locks are used to control access to shared resources by allowing only one thread to acquire the lock and access the resource at a time. They prevent multiple threads from concurrently modifying the resource, ensuring data consistency.
**Question 6: Explain the difference between a mutex and a semaphore.**
**Answer:** A mutex is a synchronization primitive that ensures mutual exclusion by allowing only one thread to access a resource at a time. A semaphore, on the other hand, can allow a specified number of threads to access a resource concurrently, based on its count.
**Question 7: How does deadlock occur in multithreaded programming?**
**Answer:** Deadlock occurs when two or more threads are blocked, each waiting for a resource that is held by another thread in the set. This leads to a situation where no thread can proceed, resulting in a standstill.
**Question 8: What is thread contention, and how can it impact performance?**
**Answer:** Thread contention refers to multiple threads competing for the same resource, leading to delays and inefficiencies. Excessive thread contention can reduce performance as threads spend more time waiting for access to resources rather than doing useful work.
**Question 9: How does a Read-Write Lock differ from a regular lock (mutex)?**
**Answer:** A Read-Write Lock allows multiple threads to read a shared resource simultaneously while ensuring exclusive access for writing. This is more efficient for scenarios where reading is more frequent than writing, as it reduces contention.
**Question 10: Why is it important to carefully design the order in which locks are acquired to prevent deadlocks?**
**Answer:** The order in which locks are acquired is crucial to preventing deadlocks. If threads acquire locks in different orders, it can lead to a situation where each thread is waiting for a lock held by another, resulting in a deadlock where none of the threads can make progress. Proper lock ordering helps avoid such scenarios.