Advanced Java Concurrency: Mastering Locks, Semaphores, and Barriers
Test your advanced knowledge of Java concurrency mechanisms including locks, semaphores, and barriers.
Questions in this quiz
10 questions on Advanced Java Concurrency: Mastering Locks, Semaphores, and Barriers. Work through them here, then start the quiz to answer them and see your score.
1.In Java, what is the main advantage of using a ReentrantLock over a synchronized block?
- AReentrantLock is faster than synchronized blocks.
- BReentrantLock can be used in a try-with-resources statement.
- CReentrantLock automatically handles thread interruptions.
- DReentrantLock provides more control over lock acquisition and release.
2.Which of the following is true about the Semaphore class in Java?
- ASemaphores can only be used to manage access to a single resource.
- BSemaphores are a type of lock that guarantees thread safety.
- CSemaphores can be used to implement resource pools.
- DSemaphores are always fair by default.
3.What is the primary purpose of the CyclicBarrier class in Java?
- ATo limit the number of threads that can access a resource simultaneously.
- BTo provide a mechanism for thread-safe data sharing.
- CTo allow a set of threads to all wait for each other to reach a common barrier point.
- DTo ensure that threads execute in a specific order.
4.How does the ReadWriteLock interface improve concurrency in Java applications?
- ABy allowing multiple threads to read simultaneously while only one thread can write.
- BBy ensuring that read and write operations are executed in FIFO order.
- CBy providing a lock that can be upgraded from read to write.
- DBy allowing multiple threads to write simultaneously.
5.What happens when a thread calls await() on a CountDownLatch object?
- AThe thread waits until the latch count reaches zero.
- BThe thread immediately proceeds without waiting.
- CThe thread waits indefinitely until interrupted.
- DThe thread waits until another thread signals it to proceed.
6.Which method would you use to attempt to acquire a lock without blocking in Java?
- Alock()
- BtryLock()
- Cunlock()
- DlockInterruptibly()
7.What is the main difference between Semaphore and CountDownLatch in Java?
- ASemaphore is used for counting down events, while CountDownLatch is used for controlling access to resources.
- BSemaphore is thread-safe, while CountDownLatch is not.
- CSemaphore can be reused after reaching zero, while CountDownLatch cannot.
- DSemaphore requires a callback method, while CountDownLatch does not.
8.In Java, what is the purpose of the lockInterruptibly() method in the ReentrantLock class?
- ATo acquire a lock only if it is available at the time of invocation.
- BTo acquire a lock and allow the thread to be interrupted while waiting.
- CTo check if the current thread holds the lock.
- DTo release a lock held by the current thread.
9.Which of the following scenarios is best suited for using a Phaser in Java?
- AWhen you need to limit access to a resource.
- BWhen you need to ensure a single thread executes a critical section.
- CWhen you need to dynamically adjust the number of parties involved in a task.
- DWhen you need to manage a fixed number of parties.
10.What is a potential drawback of using a fair lock in Java concurrency?
- AFair locks can lead to increased context switching and reduced throughput.
- BFair locks are not supported in Java.
- CFair locks are not thread-safe.
- DFair locks do not guarantee thread ordering.