Top 20 Java Concurrency Interview Questions and Answers

Top 20 Java Concurrency Interview Questions and Answers Work through these 20 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.

  1. When should you use CopyOnWriteArrayList?

    Use for subscriber lists in pub/sub where adds are rare and iteration is constant. Avoid for high-frequency add/remove — use ConcurrentLinkedQueue or synchronized collections....

    Full answer

  2. What are text blocks and how do they improve Java code?

    String query = """ SELECT u.id, u.name FROM users u WHERE u.active = true """; — readable without \n concatenation. Use formatted for parameters carefully — prefer...

    Full answer

  3. Explain structured concurrency and why it matters.

    In a request handler, open StructuredTaskScope, fork fetchUser and fetchOrders, join both — if orders fail, user fetch cancelled automatically. Safer than manual Future...

    Full answer

  4. AtomicReference vs synchronized — when to choose each?

    Use AtomicReference for lock-free config pointer swap — compareAndSet(old, new). Use synchronized when updating multiple related fields atomically. AtomicInteger for counters....

    Full answer

  5. Compare BlockingQueue implementations for producer-consumer workloads.

    For order processing, use ArrayBlockingQueue with capacity matching SLA — slow consumer blocks producer instead of OOM. ThreadPoolExecutor with SynchronousQueue +...

    Full answer

  6. IdentityHashMap vs HashMap — when would you use IdentityHashMap?

    Use when identity matters not value — tracking object visitation in deep graph walk. Business keys (userId, email) belong in HashMap with proper equals/hashCode....

    Full answer

  7. What are ScopedValues and how do they replace ThreadLocal?

    Replace logging MDC ThreadLocal with ScopedValue.where(REQUEST_ID, id).run(() -> handle()). Value immutable — update by nesting new scope. Frameworks (Spring, Micrometer)...

    Full answer

  8. What is ForkJoinPool common pool starvation and how do you avoid it?

    Never block on common pool — no DB/HTTP in parallelStream without custom pool. Use Executors.newFixedThreadPool for blocking batch jobs. CompletableFuture.supplyAsync(task,...

    Full answer

  9. How do you enable virtual threads in Spring Boot 3.2+?

    application.yml: spring.threads.virtual.enabled=true on Java 21. Verify integration tests pass — especially security, transactions, MDC logging. Replace custom fixed thread...

    Full answer

  10. How do JPA Specifications (Criteria API) help dynamic queries?

    UserSearchSpec.withStatus(status).and(withNameLike(name)) — repository.findAll(spec, pageable). Criteria API verbose — wrap in static factory methods. Alternative: Querydsl or...

    Full answer

  11. How implement multi-tenancy in Spring Security context?

    JWT includes tenant_id — converter sets TenantContextHolder. Base entity @Column tenantId validated on save. Repository methods always include tenant predicate — or Hibernate...

    Full answer

  12. HashMap vs ConcurrentHashMap — when and why?

    HashMap for single-threaded or external synchronization. ConcurrentHashMap for shared concurrent maps — finer-grained locking than Hashtable. Cannot store null keys/values in...

    Full answer

  13. Explain immutability and why String is immutable.

    Immutable objects cannot change after build — thread-safe without synchronization. String immutability allows intern pool, caching hashCode, and security when used as...

    Full answer

  14. volatile vs synchronized — when to use each?

    volatile for single-writer flags (shutdown, initialized) where no read-modify-write. synchronized or java.util.concurrent locks when multiple threads update shared state. For...

    Full answer

  15. How does Stream API parallel stream work?

    parallelStream() splits work across common pool threads. Good for CPU-heavy map on large in-memory collections. Bad for small lists, IO inside stream, or ordered stateful...

    Full answer

  16. What is an ExecutorService and why use it?

    Instead of new Thread per task, submit Runnable to fixed pool sized to CPU/IO profile. Handle Future results and exceptions. On shutdown hook call shutdown() and...

    Full answer

  17. What are virtual threads in Java 21 and when should you use them?

    Virtual threads excel when code blocks on IO — HTTP clients, JDBC, file reads — allowing one platform thread to serve thousands of concurrent requests. For CPU-heavy parallel...

    Full answer

  18. Why is ThreadLocal problematic with virtual threads?

    Libraries storing per-request state in ThreadLocal allocate one entry per virtual thread — catastrophic memory with 100k concurrent requests. Prefer ScopedValue for request...

    Full answer

  19. How do CompletableFuture and virtual threads compare for concurrency?

    Use virtual threads when business logic is naturally blocking — call DB, call HTTP, return. Use CompletableFuture when composing multiple independent async results with...

    Full answer

  20. Explain the happens-before relationship in the Java Memory Model.

    Thread A writes x=1 without sync; Thread B reads x may see 0 forever. volatile x or synchronized block creates happens-before. Classic DCL singleton needs volatile instance...

    Full answer

Top Java Concurrency Interview Questions Every Senior Backend Engineer Should Know · Java Concurrency Interview Questions and Answers · Top 50 Java Concurrency Interview Questions and Answers (2026 Edition) · Top 40 Java Concurrency Interview Questions | Must-Prepare before interview.

All interview question lists · Interview guides · Home · Support the platform