0% found this document useful (0 votes)
3 views3 pages

Java Multithreading 40 Interview Questions Solutions

The document presents a comprehensive list of 40 interview questions and answers related to Java multithreading. Key topics include thread lifecycle, synchronization, locking mechanisms, and concurrency concepts. It also covers practical aspects like thread pools, CompletableFuture, and common pitfalls in multithreading.

Uploaded by

Rohan Bangera
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Multithreading 40 Interview Questions Solutions

The document presents a comprehensive list of 40 interview questions and answers related to Java multithreading. Key topics include thread lifecycle, synchronization, locking mechanisms, and concurrency concepts. It also covers practical aspects like thread pools, CompletableFuture, and common pitfalls in multithreading.

Uploaded by

Rohan Bangera
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Multithreading – Top 40 Interview Questions

with Detailed Answers


Q: Difference between start() and run()
A: start() creates a new thread and invokes run() internally. run() called directly executes in the
current thread without creating a new thread.

Q: Java Thread Lifecycle


A: NEW → RUNNABLE → RUNNING → WAITING/BLOCKED → TERMINATED. JVM controls
state transitions.

Q: Why a thread cannot be restarted?


A: Once a thread reaches TERMINATED state, its stack and resources are destroyed by JVM.

Q: Thread vs Runnable
A: Thread is a class, Runnable is a functional interface. Runnable allows better design and
inheritance.

Q: How synchronized works internally?


A: It uses an object monitor. A thread must acquire the monitor lock before entering synchronized
block.

Q: What is reentrant locking?


A: A thread can acquire the same lock multiple times. synchronized and ReentrantLock are
reentrant.

Q: Object-level vs Class-level lock


A: Object-level locks apply to instance methods; class-level locks apply to static synchronized
methods.

Q: Deadlock, livelock, starvation


A: Deadlock: threads wait forever. Livelock: threads keep reacting. Starvation: thread never gets
CPU.

Q: Coarse vs Fine-grained locking


A: Coarse locking locks large sections; fine-grained locks small critical sections improving
concurrency.

Q: volatile guarantees
A: Visibility and ordering, not atomicity.

Q: Visibility vs Atomicity vs Ordering


A: Visibility ensures latest value, atomicity ensures indivisible operation, ordering prevents
reordering.
Q: Happens-before
A: A rule that guarantees visibility of memory operations between threads.

Q: Why volatile not atomic


A: Compound operations like i++ involve multiple steps.

Q: Double-checked locking
A: Fixed using volatile to prevent instruction reordering.

Q: wait/notify working
A: wait releases lock and pauses thread; notify wakes waiting thread.

Q: Why wait inside synchronized


A: Because wait releases the monitor which it must own.

Q: wait vs sleep
A: wait releases lock, sleep does not.

Q: Spurious wakeup
A: Thread wakes up without notify; always use wait in loop.

Q: notify vs notifyAll
A: notifyAll avoids missed signals and deadlocks.

Q: Why not create threads manually


A: Expensive, unmanaged, poor scalability.

Q: Thread pool types


A: Fixed, Cached, Single, Scheduled, ForkJoin.

Q: submit vs execute
A: submit returns Future, execute does not.

Q: ScheduledExecutorService
A: Uses DelayQueue to schedule tasks.

Q: Future vs CompletableFuture
A: CompletableFuture supports async chaining.

Q: ForkJoinPool
A: Uses work-stealing algorithm.

Q: Parallel vs Normal Stream


A: Parallel uses ForkJoinPool.
Q: Concurrency vs Parallelism
A: Concurrency = dealing with multiple tasks, Parallelism = executing simultaneously.

Q: CAS
A: Compare-And-Set updates value atomically.

Q: Atomic vs synchronized
A: Atomic uses CAS, synchronized uses locking.

Q: ABA problem
A: Value changes A→B→A unnoticed; solved using versioning.

Q: ConcurrentHashMap vs synchronizedMap
A: CHM allows concurrent reads/writes with bucket-level locking.

Q: Why no null in CHM


A: Avoid ambiguity during concurrent access.

Q: Resize safety in CHM


A: Uses CAS and node forwarding.

Q: CopyOnWriteArrayList
A: Creates new copy on write; expensive for writes.

Q: False sharing
A: Multiple variables share same cache line.

Q: Lock states
A: Biased → Lightweight → Heavyweight.

Q: Lock escalation
A: JVM upgrades lock under contention.

Q: Memory barrier
A: Prevents instruction reordering.

Q: ThreadLocal
A: Provides thread-confined variables; can cause memory leaks.

Q: CompletionService
A: Returns completed tasks faster.

You might also like