Advanced Java Concurrency: Deep Dive into Locks and Synchronizers
Explore advanced concepts of Java concurrency focusing on locks and synchronizers.
Questions in this quiz
10 questions on Advanced Java Concurrency: Deep Dive into Locks and Synchronizers. Work through them here, then start the quiz to answer them and see your score.
1.Which of the following is true about the ReentrantLock class in Java?
- AIt allows multiple threads to hold the lock simultaneously.
- BIt does not support condition variables.
- CIt supports lock reentrancy, meaning a thread can acquire the same lock multiple times.
- DIt is a fair lock by default.
2.What is the primary advantage of using ReadWriteLock over ReentrantLock?
- AIt automatically handles thread starvation.
- BIt does not require explicit unlocking.
- CIt improves performance by allowing multiple writers.
- DIt allows multiple readers and one writer at the same time.
3.In Java, what is the purpose of the Semaphore class?
- ATo ensure only one thread can access a resource at a time.
- BTo automatically resolve deadlocks.
- CTo provide a lock that can be re-entered.
- DTo manage a fixed number of permits for access control.
4.How does the CountDownLatch class differ from the CyclicBarrier class?
- ACountDownLatch can be reused after reaching zero, while CyclicBarrier cannot.
- BCyclicBarrier does not allow a barrier action to be executed.
- CCountDownLatch is used for one-time events, while CyclicBarrier can be reset and reused.
- DCountDownLatch requires all threads to reach a barrier point before proceeding.
5.Which method in the Condition interface is used to block a thread until it is signaled?
- AsignalAll()
- Bawait()
- Clock()
- Dsignal()
6.What is the primary use of the StampedLock class introduced in Java 8?
- ATo automatically prevent deadlocks.
- BTo offer a more efficient alternative to ReadWriteLock with support for optimistic reads.
- CTo provide a lock-free mechanism for concurrency.
- DTo replace ReentrantLock for all concurrency needs.
7.Which of the following is a characteristic of the Phaser class in Java?
- AIt supports dynamic addition and removal of parties.
- BIt is a replacement for Semaphore.
- CIt can only be used once and then discarded.
- DIt does not support phased execution.
8.What is the main difference between a fair and an unfair lock?
- AA fair lock allows threads to acquire locks in a random order.
- BA fair lock ensures that threads acquire locks in the order they requested them.
- CAn unfair lock prevents any thread from acquiring a lock.
- DAn unfair lock guarantees that the longest-waiting thread acquires the lock next.
9.Why might using a ReentrantLock be preferred over synchronized blocks in Java?
- AReentrantLock always provides better performance.
- BReentrantLock does not support condition variables.
- CReentrantLock offers more control over lock acquisition and fairness policies.
- DReentrantLock is less flexible and offers fewer features.
10.What is the role of the ForkJoinPool class in Java concurrency?
- ATo replace the ExecutorService for all concurrent tasks.
- BTo manage a pool of threads for executing ForkJoinTasks.
- CTo automatically balance load across multiple CPUs.
- DTo provide a simple way to execute Runnable tasks.