Top 20 Interview Questions On Java Multithreading
Top 20 Interview Questions On Java Multithreading
Top 20 Interview Questions On Java Multithreading
MULTITHREADING
1. What is Multithreading in Java?
Answer: A thread can be created by extending the Thread class and overriding the run()
method or by implementing the Runnable interface and passing it to a Thread object.
Answer: Thread is a class, while Runnable is an interface. When you extend Thread, you
cannot extend any other class, but by implementing Runnable, you can still extend other
classes.
Answer: Thread safety can be ensured using synchronization, locks (like ReentrantLock),
atomic classes from java.util.concurrent.atomic, and thread-safe collections from
java.util.concurrent.
Answer: A synchronized block restricts the access of a particular block of code to only one
thread at a time, ensuring that shared resources are accessed in a thread-safe manner.
Answer: A synchronized method locks the entire method for the current thread, whereas a
synchronized block locks only a specific portion of the code within the method, allowing more
granular control over synchronization.
Answer: A deadlock occurs when two or more threads are blocked forever, waiting for each
other to release a resource. It usually happens when two threads have a circular dependency
on resources.
Answer: Deadlock can be avoided by following practices like avoiding nested locks, acquiring
locks in a consistent order, and using try-lock mechanisms like ReentrantLock.tryLock().
11. What is volatile keyword in Java?
Answer: The volatile keyword ensures that a variable's value is always read from the main
memory, rather than being cached in a thread's local memory. It guarantees visibility of changes
to variables across threads.
Answer: notify() wakes up a single thread that is waiting on the object's monitor, while
notifyAll() wakes up all the threads that are waiting on the object's monitor. The choice
between them depends on whether you want to notify one or all waiting threads.
Answer: Callable is similar to Runnable, but it can return a result and throw a checked
exception. It is used with ExecutorService to execute tasks that return values.
Answer: A thread pool is a pool of worker threads that are reused to execute multiple tasks. It
improves performance by reducing the overhead of creating and destroying threads.
16. What is the Future interface in Java?
Answer: execute() is used to submit a task for execution without expecting any result, while
submit() can be used to submit a task and obtain a Future object representing the result of
the task.
Answer: Concurrency is about managing multiple tasks at the same time, allowing them to
make progress independently. Parallelism involves executing multiple tasks simultaneously,
often on multiple processors or cores.
Answer: A race condition occurs when the outcome of a program depends on the sequence or
timing of uncontrollable events, like the interleaving of thread execution. It can be prevented
using synchronization, locks, or atomic variables to ensure that shared resources are accessed
safely.