Interview questions
What's the difference between synchronized and volatile?
How I'd answer:
"synchronized provides mutual exclusion — only one thread at a time holds the monitor for that object — and visibility: releasing the lock happens-before the next
acquire, so the next thread sees all writes. volatile provides only visibility and ordering for a single variable: every read sees the latest write, and it can't be
cached or reordered around. It doesn't make compound operations atomic, so volatile int count; count++ is still a race. I use volatile for a stop flag or to publish
an immutable object, and synchronized, locks or atomics when there's a read-modify-write or an invariant across several fields. I've seen the plain-flag version
actually hang: without volatile, the JIT hoisted the read out of the loop."
Combines: Ch 3, 4.
How does ConcurrentHashMap achieve thread safety? Is if (!map.containsKey(k)) map.put(k, v) safe on it?
How I'd answer:
"Since Java 8, ConcurrentHashMap uses a HashMap-like table where reads are lock-free, inserting into an empty bucket uses a CAS, and updates to a non-empty bucket lock
only that bucket's first node, so writes to different buckets proceed in parallel. Resizing is done cooperatively by multiple threads, and it doesn't allow nulls so a
null from get always means absent. But individual thread-safe calls don't compose: containsKey followed by put is check-then-act, and another thread can insert in
between. I'd use putIfAbsent, computeIfAbsent, merge or compute, which are atomic per key. I measured a get-then-put counter losing most of its updates on CHM,
while merge was exact."
Combines: Ch 8; Collections, From the Inside Ch 4.
How would you size a thread pool?
How I'd answer: "It depends on the workload. For CPU-bound tasks, about the number of cores — more threads just add context switching. For I/O-bound tasks, roughly cores times one plus the ratio of wait time to compute time, because threads spend most of their time blocked; with 8 cores and tasks waiting 100 times longer than they compute, that's hundreds of threads. I'd also bound the queue and choose a rejection policy so overload produces backpressure, like a 503, rather than unbounded memory growth. And I'd measure — throughput and latency under load — rather than trust the formula. On Java 21, for I/O-bound request handling, I'd use virtual threads instead and cap access to scarce resources like database connections with a semaphore or the connection pool size."
Combines: Ch 2, 9, 11.
Write a producer–consumer. How do you stop the consumer?
How I'd answer:
"I'd use a bounded ArrayBlockingQueue. Producers call put, which blocks when the queue is full — that gives backpressure — and consumers loop on take, which blocks
when it's empty. To stop, the producer puts a poison pill, one per consumer, and a consumer that takes it exits its loop; alternatively, the owner interrupts the
consumer threads or shuts down the executor running them. If asked to do it without the library, I'd use a lock with wait and notifyAll, always waiting inside a
while loop that re-checks the condition, because of spurious wakeups."
Combines: Ch 7.
What are virtual threads and when should you use them?
How I'd answer:
"Virtual threads, final in Java 21, are lightweight threads scheduled by the JVM rather than the OS. Many virtual threads run on a small pool of carrier platform threads;
when one blocks on I/O or sleeps, the JVM unmounts it and runs another on that carrier. So you can have one thread per request — even hundreds of thousands — and keep
simple blocking code. In a test, 10,000 tasks each waiting 100 ms took about 5.2 seconds on a 200-thread pool and about 190 ms with virtual threads. They're for
I/O-bound work; they don't speed up CPU-bound work. Don't pool them — create one per task — limit scarce resources with semaphores, watch ThreadLocal usage, and in Java
21 avoid blocking inside synchronized because it pins the carrier; that was fixed in Java 24."
Combines: Ch 11; Operating Systems Ch 11.