Threads and Concurrency Quiz - Languages Java
Free practice quiz: Threads and Concurrency (Languages Java). 55 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 55 questions freeSample questions
What is the primary mechanism by which Java achieves concurrency?
- Processes
- Threads
- Coroutines
- Asynchronous programming models
Show answer
Answer: B. Threads
Java's concurrency model is built around the concept of threads, which are lightweight units of execution that can run concurrently within a single process.
Which exception is thrown by `Thread.sleep()`?
- IllegalThreadStateException
- InterruptedException
- ThreadDeath
- NullPointerException
Show answer
Answer: B. InterruptedException
`Thread.sleep()` can be interrupted by another thread calling the `interrupt()` method on the sleeping thread. If this happens, `Thread.sleep()` throws an `InterruptedException`.
What is `ReentrantLock` in Java?
- A simple synchronization mechanism that is less flexible than `synchronized` blocks.
- A more flexible and powerful locking mechanism than `synchronized` keywords, allowing reentrant locking, fairness, and timed waits.
- A mechanism for creating daemon threads.
- A method to make threads sleep.
Show answer
Answer: B. A more flexible and powerful locking mechanism than `synchronized` keywords, allowing reentrant locking, fairness, and timed waits.
`ReentrantLock` implements the `Lock` interface and provides explicit locking and unlocking. It offers features like reentrancy (a thread can acquire the same lock multiple times), fairness policies (optional), and timed lock acquisition, which are not available with the basic `synchronized` keyword.
What is the purpose of the `synchronized` block `synchronized(object)`?
- To synchronize on the `Class` object.
- To synchronize on a specific instance of an object.
- To synchronize on the current thread's stack.
- To make the method static.
Show answer
Answer: B. To synchronize on a specific instance of an object.
When you use `synchronized(object)`, the thread acquires the intrinsic lock associated with the `object` instance. This allows for fine-grained synchronization, where different blocks of code might synchronize on different objects, enabling more concurrent access than synchronizing an entire method or static method.
What is a `Phaser` in Java's concurrency utilities?
- A synchronization mechanism for a variable number of parties that may arrive and depart dynamically at barrier points.
- A way to create a thread pool with a fixed number of threads.
- A lock that can be acquired by multiple threads simultaneously.
- A mechanism for signaling between threads.
Show answer
Answer: A. A synchronization mechanism for a variable number of parties that may arrive and depart dynamically at barrier points.
`Phaser` is a more flexible synchronization barrier than `CyclicBarrier` because the number of parties (threads) can change dynamically. Parties register with the `Phaser`, wait at a barrier point, and then can optionally deregister. It's useful for coordinating threads in complex, dynamic workflows.