Concurrency problems and diagnosis
26 questions in 7 subtopics.
Deadlock
What are the four conditions that must all hold for a deadlock, and which one is usually easiest to break in application code?Thread 1 runssynchronized (a) {
synchronized (b) { ... }
}
synchronized (b) {
synchronized (a) { ... }
}
FutureTask.get, and the pool's queue contains subtasks submitted by those same threads. Explain.Each request in a service opens a transaction with one connection and then calls a helper that requests a second connection from the same Hikari pool of size 10. Under load, everything hangs until connection timeouts. Explain and fix.Service A calls service B synchronously, and B, while handling some requests, calls back to A. Both use bounded thread pools. During a traffic spike, both services stop responding until timeouts fire. Explain and propose architectural fixes.As systems moved from one large JVM to many services, deadlocks shifted from lock cycles inside a process to waits across services: call cycles, exhausted pools and retries. What long-term architecture principles would you set to prevent these?Livelock and starvation
Define livelock and starvation with a Java example of each, and how to fix them.A service has one shared pool for request work and a periodic cleanup job. Under steady high load, the cleanup task submitted to the pool runs hours late and memory grows. Diagnose and fix.Retry storms, where clients retry failed calls and keep an overloaded service from recovering, are a form of livelock at system scale. What long-term standards would you set for retries across your organisation?Race conditions
This Spring singleton bean is called by many request threads. Find the race and fix it.@Service
class RateCache {
private final Map<String, Rate> cache = new HashMap<>();
Rate get(String c) {
Rate r = cache.get(c);
if (r == null) {
r = load(c);
cache.put(c, r);
}
return r;
}
}
NumberFormatException intermittently. The code usesprivate static final SimpleDateFormat FMT = new SimpleDateFormat("yyyy-MM-dd");
if (!order.isConfirmed()) {
sendEmail();
order.setConfirmed(true);
save(order);
}
Thread dumps
How do you take a thread dump of a running JVM, and what do BLOCKED, WAITING (parking) and locked lines tell you?How do thread dumps work for applications using virtual threads, and what changes in how you read them?During a latency incident a thread dump shows 180 of 200 Tomcat threads like this: "http-nio-8080-exec-57" WAITING (parking) atjdk.internal.misc.Unsafe.park ... at java.util.concurrent.SynchronousQueue.poll ... at com.zaxxer.hikari.pool.HikariPool.getConnection. What is happening and what do you check next?Design a fleet-wide approach so that, when a latency or error alert fires, engineers have useful thread and lock information without logging into hosts.ThreadLocal leaks
A service sets a large per-request object into aThreadLocal in an interceptor, and memory slowly grows even though requests are short. A heap dump shows those objects referenced from Thread objects of the request pool. What is wrong?After several hot redeploys of a web application in a shared Tomcat, the server fails with OutOfMemoryError: Metaspace. Tomcat logs warnings that the web application created a ThreadLocal and failed to remove it. Explain the leak.Contention profiling
What tools and JFR events would you use to profile contention in a JVM, and what does each tell you?A service shows p99 latency spikes while CPU usage stays at 30%. You suspect lock contention. Describe how you would confirm and locate it.Contention problems in your organisation are usually found only during incidents. What would you put in place so they are found earlier?Leadership asks whether to invest in fleet-wide continuous profiling, including lock and wall-clock profiles, or to rely on engineers taking thread dumps and profiles during incidents. How would you weigh this decision over several years?Testing concurrent code
Write a test that tries to detect a lost-update bug in aCounter.increment() method by running many threads at once. Explain why a passing test does not prove the code is correct.What tools and techniques would you use to test concurrent Java code beyond plain unit tests, and what is each good for?Testing cannot prove the absence of concurrency bugs. As a senior technical leader, how would you set an organisation-wide strategy for concurrency correctness?No question matches these choices.