Java Interview Questions

Java Multithreading and Concurrency Fundamentals

"Can you explain Java multithreading and concurrency fundamentals?"

Why Interviewers Ask This

Concurrency is fundamental to building high-performance Java applications. This question tests whether you can write thread-safe code, understand synchronization mechanisms, and properly use thread pools — essential skills for any backend engineer working on distributed systems.

How to Answer

  1. 1

    Start with thread creation: Thread class, Runnable interface, Callable + Future for return values, and thread pools (ExecutorService) as the preferred approach.

  2. 2

    Explain thread states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.

  3. 3

    Cover synchronization: synchronized keyword (method/block level), volatile for visibility, Lock interface (ReentrantLock) for more control.

  4. 4

    Discuss the Java Memory Model: main memory vs. working memory, happens-before relationship, visibility and ordering guarantees.

  5. 5

    Describe thread pools: ThreadPoolExecutor parameters (corePoolSize, maxPoolSize, keepAliveTime, workQueue, handler), why Alibaba coding guidelines recommend using ThreadPoolExecutor directly rather than Executors factory methods.

Sample Answer

Java provides multiple ways to create threads: extending Thread, implementing Runnable, or using Callable with Future for tasks that return results. However, the best practice is to use thread pools via ExecutorService, which reuses threads and controls concurrency. ThreadPoolExecutor is configured with core pool size, maximum pool size, keep-alive time, a work queue (LinkedBlockingQueue, SynchronousQueue, etc.), and a rejection policy (AbortPolicy, CallerRunsPolicy, etc.). Thread safety is achieved through several mechanisms. The synchronized keyword provides mutual exclusion using monitor locks — it can be applied to methods or code blocks. volatile ensures visibility across threads by forcing reads/writes to main memory, but does not provide atomicity for compound operations like i++. For more flexibility, ReentrantLock offers features like tryLock with timeout, fair locking, and multiple Condition objects. The Java Memory Model defines happens-before relationships that guarantee visibility: unlock happens-before subsequent lock, volatile write happens-before volatile read, and thread start happens-before any action in the started thread. The java.util.concurrent package provides higher-level utilities: AtomicInteger for lock-free atomic operations using CAS, CountDownLatch for waiting on multiple threads, CyclicBarrier for synchronizing threads at a common point, and ConcurrentHashMap for thread-safe map operations. Understanding these tools and choosing the right one for each scenario is what separates junior developers from experienced concurrent programmers.

Pro Tips

  • Always mention why Executors.newFixedThreadPool() can be dangerous — unbounded queue can cause OOM.

  • Be ready to explain the difference between synchronized and ReentrantLock with concrete scenarios.

  • Know CAS (Compare-And-Swap) and its ABA problem — this is a common follow-up.

  • Use HireMe AI during interviews to get instant reminders on thread pool parameters and rejection policies.

Frequently Asked Questions

What is the difference between synchronized and volatile?

synchronized provides both mutual exclusion and visibility. volatile only provides visibility — it ensures all threads see the latest value, but does not make compound operations atomic.

Why not use Executors.newFixedThreadPool()?

It uses an unbounded LinkedBlockingQueue, which can accumulate unlimited tasks and cause OutOfMemoryError. Always use ThreadPoolExecutor with an explicit bounded queue.

What is a deadlock and how do you prevent it?

A deadlock occurs when two threads each hold a lock the other needs. Prevent it by: always acquiring locks in the same order, using tryLock with timeout, or using java.util.concurrent utilities instead of manual locking.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free