Java Threads Interview Questions Answers
Java Threads Interview Questions Answers
A thread is a lightweight sub-process, the smallest unit of processing. Java allows multiple threads
to run concurrently within a program, sharing resources but executing independently. Each thread is
Runnable: An interface in Java that is used to define the code that constitutes the thread's task.
Difference: You can either extend Thread (but then cannot extend any other class due to single
inheritance) or implement Runnable and pass it to a Thread object. Using Runnable is more flexible.
Two ways:
1. Extending Thread: You create a subclass of Thread and override the run() method.
2. Implementing Runnable: You implement the Runnable interface and pass the object to a Thread
object.
Timed Waiting: Thread is waiting for another thread for a specified waiting time.
Daemon threads run in the background and do not prevent the JVM from exiting once all user
threads finish execution. Examples include garbage collector threads. You can set a thread as
inconsistent data. Without it, multiple threads could interfere with each other, causing issues like
race conditions.
- Static synchronization: Used when the synchronized method is static, locking the class object
A synchronized method locks the entire method for a single thread, while a synchronized block locks
only a portion of the code within a method, allowing more flexibility and better performance.
Deadlock occurs when two or more threads block each other by holding resources the other needs.
Prevention:
10. What is a volatile keyword in Java and how is it used in the context of threads?
volatile ensures that the value of a variable is always read from the main memory and not from the
volatile ensures visibility of changes to variables across threads but does not guarantee atomicity,
while synchronized ensures both visibility and atomicity by locking the critical section.
ThreadLocal provides each thread with its own copy of a variable, ensuring that no two threads
share the same instance. It's useful when you need a variable per thread without synchronization.
The Java Memory Model defines how threads interact through memory and governs the visibility of
changes to variables across threads. It ensures that memory writes by one thread are visible to
- wait(): Causes the current thread to wait until another thread calls notify() or notifyAll().
ReentrantLock is a lock that allows the same thread to acquire the lock multiple times without
deadlock. It offers more control than the synchronized keyword (e.g., fairness policy, lock polling
with tryLock()).
CountDownLatch is a synchronization aid that allows one or more threads to wait until a set of
operations being performed by other threads complete. It is useful when you need to start multiple
CyclicBarrier allows a group of threads to wait for each other to reach a common barrier point.
Unlike CountDownLatch, it can be reused once the barrier is reached, hence 'cyclic'.
The Executor framework in Java provides a mechanism to manage thread execution via thread
pools. It simplifies concurrent task execution by separating task submission from the mechanics of
ForkJoinPool is a special thread pool in Java that efficiently manages and executes tasks in parallel
by breaking them into smaller subtasks (forking) and merging results (joining). It is used for
Callable is similar to Runnable, but it can return a result and throw exceptions.
Future represents the result of an asynchronous computation and provides methods to check if the
Thread.yield() is a hint to the thread scheduler to give the CPU to other threads. The current thread
goes back to a 'runnable' state, and the thread scheduler decides whether to continue running it or
sleep() pauses the thread for a specific duration without releasing any locks.
wait() causes the current thread to release the lock and wait until another thread calls notify() or
notifyAll().
ScheduledExecutorService allows you to schedule tasks to run after a fixed delay or periodically. It's
part of the ExecutorService interface and offers a high-level way to manage task scheduling without
Uncaught exceptions in threads can be handled using the UncaughtExceptionHandler. You can set