Java Interview Questions - Multithreading
Here are the most popular Java interview questions about multithreading.See all questions
What are Immutable Classes in Java? What are Immutable Classes useful for?
Immutable classes are those which cannot be changed once instantiated. The value of the object of an immutable class will always remain the same. Either the value cannot be changed or any change to the vale will result in a new immutable instance.
Boolean and other primitive wrapper classes are all Immutable. All the variables and the c
What happens when one thread modifies a HashMap while another thread is iterating the same map? What is the correct way to do this?
A HashMap is not a synchronized Map, meaning that it is not thread-safe. If a HashMap is modified by a thread while another thread is iterating it, a
ConcurrentModificationException will be thrown.
There are two possible ways to do this correctly:...
What is the use of the
volatile keyword in Java? What is the happens-before guarantee?
volatile keyword is used in multi-threaded programs. It indicates that a variable can be updated by multiple threads. This means that the value of the variable cannot be cached locally and must be directly accessed from main memory every time.
volatile variables are not serializable.
The happens-before guarantee for
volatile variables ca
Explain instruction reordering. In what context can instruction reordering create problems? How can it be fixed?
Instruction reordering is when the execution order of the code is changed by the JVM at compile time or run time. This is done to optimize the code. Instruction reordering guarantees program order execution. This means that two statements which are related will have a happens-before relation, but two unrelated statements can be executed out-of-orde...