1. When does ReentrantReadWriteLock help most?
Read-write locks allow many readers to proceed in parallel, blocking only when a writer acquires the write lock. This improves throughput in read-heavy workloads.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
Java · Question Set
Concurrency, Executors & Locks (Advanced) interview questions for placements and exams.
Questions
14
Included in this set
Subject
Java
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for Java.
Read-write locks allow many readers to proceed in parallel, blocking only when a writer acquires the write lock. This improves throughput in read-heavy workloads.
For complete preparation, combine this set with full subject-wise practice for Java. You can also explore other subjects and sets from the links below.
One: Impose a strict global lock ordering and acquire locks in that order everywhere. Two: Use tryLock with timeout and back off if you can’t acquire all locks. Other options: minimize lock scope, prefer immutable data, or replace coarse locks with higher-level concurrency utilities.
ExecutorService abstracts thread creation. You submit tasks, not threads; it reuses a pool, limits concurrency, offers futures for results, and supports graceful shutdown. This improves throughput and stability.
For CPU-bound tasks, set the pool size near the number of cores to minimize context switches and maximize CPU utilization. Cached pools can oversubscribe threads and degrade performance.
CompletableFuture supports async pipelines (thenApply, thenCompose), timeouts, combining results, and completion actions without blocking. It works with default or custom executors.
java.util.concurrent.atomic uses lock-free algorithms built on CAS instructions. They retry on contention instead of blocking, reducing context switching.
ReentrantLock provides advanced features: timed/interruptible acquisition, optional fairness, and separate Lock/Condition objects. Performance depends on the workload; it’s not universally faster.
A ThreadFactory standardizes thread characteristics across a pool, aiding debugging, monitoring, and safe error handling for uncaught exceptions.
Semaphores manage permits. Threads acquire a permit before proceeding and release it afterward, capping concurrency to available resources.
Phaser allows parties to register and deregister at runtime and supports multiple synchronization phases with custom phase-advance logic, making it a superset of many barrier patterns.
volatile guarantees visibility and ordering for a single variable: writes by one thread become visible to others, and reads/writes are not reordered across that variable. It does NOT make compound actions atomic (like ++), nor does it protect invariants across multiple variables. Use locks or atomics for atomicity and compound state changes.
Call shutdown to reject new tasks and let running tasks finish. Await for a bounded time. If tasks still run, call shutdownNow to interrupt and drain the queue. This avoids leaks and dangling threads.
ForkJoinPool keeps per-worker deques; when a worker runs out of local tasks, it steals from the tail of another's deque, reducing contention and balancing work for divide-and-conquer algorithms.
Blocking queues coordinate producers and consumers. put waits for space; take waits for data. offer/add/remove have different, non-blocking behaviors and may fail or throw.