Concurrency
-
What are the different ways to create a thread?
- Extends
Thread
class. - Implements
Runnable
interface. ExecutorService
- Extends
-
What are the states of a thread?
- NEW: Thread created but not started (start() not called).
- RUNNABLE: Thread is executing or ready to execute.
- BLOCKED: Waiting for a monitor lock (e.g., in a synchronized block).
- WAITING: Waiting indefinitely for another thread (e.g., wait(), join()).
- TIMED_WAITING: Waiting with a timeout (e.g., sleep(), wait(timeout)).
- TERMINATED: Thread has completed execution.
-
What are the ways to create a thread pool?
- Using
Executors
Factory Methods:ExecutorService pool = Executors.newFixedThreadPool(4); // Fixed-size pool
ExecutorService single = Executors.newSingleThreadExecutor(); // Single thread
ExecutorService cached = Executors.newCachedThreadPool(); // Dynamic pool - Using
ThreadPoolExecutor
:ThreadPoolExecutor pool = new ThreadPoolExecutor(
2, // core pool size
4, // max pool size
60, TimeUnit.SECONDS, // keep-alive time
new LinkedBlockingQueue<>() // work queue
);
- Using
-
What are the states of a thread pool?
-
What is the difference between the
submit()
andexecute()
methods in a thread pool?
- need example
-
How can you ensure thread safety in a Java program?
-
Use synchronized methods and blocks.
-
Use concurrent collections.
-
Use atomic variables for lock-free operations.
-
Use
ReentrantLock
for fine-grained control. -
Use
volatile
for shared variables. -
Use immutable object.
-
What is the principle behind synchronized lock upgrading in multithreading?
-
What is a deadlock?
- A deadlock occurs when two or more threads are blocked forever, each waiting for a resource that another holds, forming a circular dependency.
- Four conditions.
- What is the difference between deadlock and starvation?
- Deadlock: Threads are blocked forever.
- Starvation: Threads unable to access resource because of low priority or unfair scheduling.
-
How can deadlocks be prevented?
-
Acquire all required locks at once.
synchronized (lock1) {
synchronized (lock2) { /* work */ }
} -
Always lock in the same order.
-
Use
tryLock()
with ReentrantLock to avoid indefinite waiting. -
What is ThreadLocal? What are its use cases?
- ThreadLocal provides thread-local variables.
- Storing per-thread context (e.g., user ID, transaction ID).
-
Explain the underlying implementation of
synchronized
. -
What is the difference between
synchronized
andvolatile
? -
What are the communication methods between threads?
wait()
,notify()
,notifyAll()
- Volatile keyword
- Locks and Conditions?
-
What are the communication methods between processes?
-
What is thread safety? Is
Vector
a thread-safe class? -
What is a
FutureTask
? -
What is the difference between the heap and the stack in Java?
- Heap:
- Share across threads
- Store objects and class instances
- Stack
- Thread-specific
- Store method local variables
- Tied to method scope
- What is a thread pool, and why is it used?
- Reusable threads managed by
ExecutorService
- What is the difference between livelock and deadlock in Java?
- Livelock: Threads actively change states but cannot progress.
- Deadlock: Threads blocked forever waiting for resources.