Scrivio Interview library

Synchronization and monitors

19 questions in 5 subtopics.

Synchronized methods and blocks

ConceptFoundations6 min read
What lock does each of these use, and what guarantees does synchronized give beyond mutual exclusion? (1) a synchronized instance method, (2) a
synchronized static method, (3) synchronized (lockObject) { ... }
CodingFoundations6 min read
Is this counter thread-safe when two Counter instances are used from different threads?
class Counter {
private static int total;
public synchronized void add() {
total++;
}
}
DebuggingFoundations6 min read
Review these lock choices and explain the problem with each: (a)
synchronized (lock) {
lock = new Object();
...
}
(b)
private final Boolean flag = false;
synchronized (flag) { ... }
(c) synchronized ("orders") { ... }
ScenarioIntermediate6 min read
In a code review you see
public synchronized Quote getQuote(String symbol) {
if (!cache.containsKey(symbol)) cache.put(symbol, pricingClient.fetch(symbol));
return cache.get(symbol);
}
in a singleton bean. Load tests show throughput capped at a few requests per second when the cache is cold. What feedback do you give?
DesignAdvanced6 min read
Classes in your shared libraries declare public synchronized methods, and some client code synchronizes on library objects for its own purposes, causing unexpected blocking and one deadlock. Write the locking guidelines for public library classes.

Intrinsic locks and reentrancy

ConceptFoundations6 min read
What does it mean that intrinsic locks are reentrant, and what would go wrong if they were not?
InternalsIntermediate6 min read
How does HotSpot implement synchronized at a high level, from an uncontended lock to a contended one, and what changed in recent JDKs?
ConceptPrincipal6 min read
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

CodingFoundations6 min read
Write a bounded buffer with synchronized, wait and notifyAll supporting put and take, and explain why each rule in your code is necessary.
DebuggingFoundations5 min read
This consumer occasionally throws NoSuchElementException:
synchronized (queue) {
if (queue.isEmpty()) queue.wait();
return queue.remove();
}
Explain.
ConceptIntermediate6 min read
When is notify() safe to use instead of notifyAll(), and what failure does a wrong notify() cause?
LeadershipPrincipal6 min read
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

ConceptIntermediate6 min read
What was biased locking, why was it disabled by default in JDK 15 (JEP 374) and later removed, and who might notice?
InternalsIntermediate6 min read
Describe lock elision and lock coarsening done by the HotSpot JIT, and give an example of code where they apply.
ScenarioAdvanced7 min read
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?
LeadershipPrincipal6 min read
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

ConceptFoundations7 min read
What is a guarded block, and why is it better than polling a flag in a loop with sleep?
CodingIntermediate7 min read
Implement a one-shot gate class with methods open() and awaitOpen() using intrinsic locks, then compare it with using CountDownLatch.
LeadershipAdvanced7 min read
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?