Java Concurrency: Exploring Locks and Synchronizers
Assess your understanding of Java concurrency with a focus on locks and synchronizers.
Questions in this quiz
12 questions on Java Concurrency: Exploring Locks and Synchronizers. Work through them here, then start the quiz to answer them and see your score.
1.Which of the following is a key advantage of using ReentrantLock over synchronized blocks?
- AReentrantLock allows specifying a fairness policy.
- BReentrantLock does not require explicit unlocking.
- CReentrantLock is faster than synchronized blocks.
- DReentrantLock is part of the java.util.concurrent.atomic package.
2.What is the primary purpose of the Semaphore class in Java?
- ATo manage a pool of resources.
- BTo ensure a single thread execution.
- CTo provide a lock-free mechanism.
- DTo replace synchronized blocks.
3.What does the CountDownLatch class do in Java concurrency?
- AEnsures only one thread can access a resource at a time.
- BProvides a lock-free mechanism for thread synchronization.
- CManages a pool of resources.
- DAllows threads to wait until a set of operations complete.
4.In which scenario would you prefer using a ReadWriteLock over a ReentrantLock?
- AWhen reads are frequent and writes are infrequent.
- BWhen only one thread accesses the resource.
- CWhen fairness is not a concern.
- DWhen multiple threads read and write frequently.
5.How does the CyclicBarrier differ from CountDownLatch?
- ACyclicBarrier is not reusable.
- BCyclicBarrier can be reused after the barrier is broken.
- CCyclicBarrier allows only one thread to proceed.
- DCyclicBarrier is used for managing resource pools.
6.What is the main advantage of using the Lock interface over synchronized blocks?
- ALocks can be more flexible and provide more options.
- BLocks are part of the java.lang package.
- CLocks do not require explicit unlocking.
- DLocks are always faster than synchronized blocks.
7.Which method in the Condition interface is used to make a thread wait until it is signaled?
- Aawait()
- Block()
- Csignal()
- Dunlock()
8.What is the purpose of the Phaser class in Java concurrency?
- ATo manage a pool of resources.
- BTo synchronize threads at a common barrier point.
- CTo ensure a single thread execution.
- DTo provide a lock-free mechanism.
9.Which of the following is true about the StampedLock class?
- AIt is a replacement for ReentrantLock.
- BIt supports optimistic reads.
- CIt does not support write locks.
- DIt is part of the java.util.concurrent.atomic package.
10.What is the main difference between a Semaphore and a Lock?
- ASemaphore is part of the java.lang package.
- BSemaphore can manage multiple permits.
- CSemaphore is faster than Lock.
- DSemaphore does not require explicit unlocking.
11.Which method in the ReentrantLock class can be used to check if the lock is held by any thread?
- AtryLock()
- BisLocked()
- ChasQueuedThreads()
- DisHeldByCurrentThread()
12.What is the purpose of the signalAll() method in the Condition interface?
- ATo wake up all waiting threads.
- BTo lock all threads.
- CTo make all threads wait.
- DTo unlock all threads.