Concurrency patterns
18 questions in 6 subtopics.
Producer-consumer
Implement producer-consumer with aBlockingQueue where one producer reads lines from a file and four consumers process them. How do the consumers stop cleanly when the file is finished?A service uses Executors.newFixedThreadPool(20) to send events to a slow downstream. During a downstream slowdown the service runs out of heap. Explain the cause and the fix.Two components of a system communicate as producer and consumer. One proposal uses an in-process bounded queue, another uses Kafka. How do you decide?Thread pool
Explain howThreadPoolExecutor decides between creating a thread, queueing a task and rejecting it. Why does a pool with core 10, max 50 and an unbounded queue never grow beyond 10 threads?A service receives 500 requests per second, and each request makes a blocking call to a downstream that takes 200 ms on average. How do you size the thread pool, and what changes with virtual threads?Write the standards for thread pools and executors across your services, including virtual threads adoption.Future and promise
Call two remote services in parallel withCompletableFuture, combine their results, and fail the whole operation if it takes longer than 2 seconds. Which executor do you use?What is the difference between a future and a promise? How does CompletableFuture play both roles, and how can an API hide the write side from callers?A REST endpoint that builds a response from a CompletableFuture chain sometimes hangs forever, and no exception is logged. The chain uses thenApply and thenCompose, and one stage manually completes a CompletableFuture from a callback API. What are the likely causes?Immutable object
Make this class immutable and explain each change:public class Booking {
private Date start;
private List<String> guests;
public Booking(Date start, List<String> guests) {
this.start = start;
this.guests = guests;
}
public Date getStart() {
return start;
}
public List<String> getGuests() {
return guests;
}
}
Actor model
Explain the actor model. How would you implement a simple actor in plain Java, and what do frameworks such as Akka or Apache Pekko add?A real-time bidding system keeps per-campaign budgets in memory and receives 200,000 bid events per second. One architect proposes actors, another proposes lock-based services with a shared database. How do you evaluate the options?A widely used actor framework changed its licence in 2022, and a community fork followed. What does this teach about betting on concurrency frameworks that shape your application code for many years?Reactor pattern
A Spring WebFlux endpoint calls a blocking JPA repository inside a map operator. Under load, throughput collapses and latency rises for all endpoints. Explain and fix.Explain the reactor pattern and how Netty implements it. Why must code never block on an event loop thread?Java server code moved from thread-per-request to reactive event loops for scalability, and now virtual threads bring back thread-per-request with blocking code. What concurrency model should your company standardise on for the next decade, and how do you handle existing reactive services?No question matches these choices.