Synchronization and monitors
19 questions in 5 subtopics.
Synchronized methods and blocks
What lock does each of these use, and what guarantees does synchronized give beyond mutual exclusion? (1) a synchronized instance method, (2) asynchronized static method, (3) synchronized (lockObject) { ... }
class Counter {
private static int total;
public synchronized void add() {
total++;
}
}
synchronized (lock) {
lock = new Object();
...
}
private final Boolean flag = false;
synchronized (flag) { ... }
synchronized ("orders") { ... }In a code review you seepublic synchronized Quote getQuote(String symbol) {
if (!cache.containsKey(symbol)) cache.put(symbol, pricingClient.fetch(symbol));
return cache.get(symbol);
}
Intrinsic locks and reentrancy
What does it mean that intrinsic locks are reentrant, and what would go wrong if they were not?How does HotSpot implement synchronized at a high level, from an uncontended lock to a contended one, and what changed in recent JDKs?Java gave every object a monitor in 1.0. In hindsight, what did that design cost, and what does it mean for the future of the language, for example value-based classes and the value objects proposed by Project Valhalla?Wait, notify and notifyAll
Write a bounded buffer with synchronized, wait andnotifyAll supporting put and take, and explain why each rule in your code is necessary.This consumer occasionally throws NoSuchElementException:synchronized (queue) {
if (queue.isEmpty()) queue.wait();
return queue.remove();
}
notify() safe to use instead of notifyAll(), and what failure does a wrong notify() cause?You are designing a concurrency curriculum for 500 Java engineers. Should wait and notify still be taught, and what should the curriculum focus on to prevent incidents over the coming years?Lock coarsening and biased locking removal
What was biased locking, why was it disabled by default in JDK 15 (JEP 374) and later removed, and who might notice?Describe lock elision and lock coarsening done by the HotSpot JIT, and give an example of code where they apply.You are leading a fleet upgrade from Java 11 to Java 25. Some older services rely heavily on Vector, Hashtable,StringBuffer and synchronized utility classes. How would you assess and manage the performance risk from biased locking removal and other locking changes?Biased locking was a HotSpot optimisation many teams implicitly relied on, and it was removed after years. What does this teach about depending on JVM implementation details, and what guidance would you give engineering leaders?Guarded blocks
What is a guarded block, and why is it better than polling a flag in a loop with sleep?Implement a one-shot gate class with methodsopen() and awaitOpen() using intrinsic locks, then compare it with using CountDownLatch.Your codebase has dozens of hand-written wait/notify coordination classes written over the years, and several caused hard-to-reproduce stalls. Would you ban raw wait/notify in application code? How would you handle the existing code?No question matches these choices.