MultiThreading and Concurrency Interview Questions

Multi threading, synchronization has been one of the faviourite topic during interview. The new concurrency API (java.util.concurrent) provided by java is also hot these days. This page specially focus on interview questions from these topics.

Overview : Most Imp Questions Java Basic Interview Questions
OOPS Concepts Interview Questions Exception Handling Questions
MultiThreading & Concurrency API Java API : String, File I/O
Collection Framework Questions Advanced Questions

There are 3 ways to create Thread in Java:
By extending Thread class.
By Implementing Runnable interface.
By Implementing Callable interface.
Synchronization is the process of accessing shared resources by multiple threads in such a way that no resource can be accessed by multiple thread at one particular moment of time.
In Java synchronization is achieved through synchronized keyword. Java 5 onwards there are other classes like Lock to achieve the same.
Object.wait() Thread.sleep()
Wait method is defined inside Object class. Sleep method is defined inside Thread class and is static method.
You call wait on an object and the code must be inside a synchronized context (method or block). You call sleep on Thread class and code may or may not be in synchronized context.
Wait releases lock of the object on which its waiting. sleep method don't release lock of the Thread.
Waiting thread can be woken-up by notify or notifyAll methods. Sleep-ing thread is woken-up automatically once the time is complete.
java.util.concurrent.locks.Lock class provides thread synchronization operation like synchronized keyword (implicit locking). Lock is more flexible and sophisticated than synchronized block/method. Biggest benefit of Lock object over implicit locking is its ability to backout of an attempt to acquire a lock (immediately or after specified duration). This helps in avoiding deadlock. lock(), unlock() and tryLock() are most common methods. Lock is an interface, ReentrantLock is the implementing subclass.
The concept of reentrant lock, in simple terms, is that if a thread has acquired lock on a monitor object once, the same thread can reacquire lock on same monitor multiple times before releasing it. The synchronized keyword is also reentrant. If a thread enter a synchronized method, it can enter any other method synchoronized on same monitor object.
ReadWriteLock allows multiple threads to read a shared resource simultaneously while only one can write at a time. ReadWriteLock is again an interface like Lock.
This is achieved by maintaining a pair of locks, one for read only operation and one for writing. The write lock is exclusive whereas read lock can be held by multiple reader threads until there is a writer. readLock() and writeLock() are methods of it.
Blocking queue is a queue that additional has additional operation to block if you are adding to the full queue or remove from an empty queue. It is like a producer consumer implementation, if the bucket is full producer wait and if the buicket is empty consumer wait.
synchronized block Lock object
A synchronized block doesn't guarentee the sequence in which threads waiting to enter it are granted access. Lock access order can be controlled.
Synchronized block must be limited to a single method. You can call lock() and unlock() on a Lock in different methods.
synchronized block don't have option to backout. tryLock() backout if lock is not available immediately. Also using parameterised lock() method you can wait till a specified time before backout.
The scoping mechanism of synchronized method/block makes easier to use them. Since lock() and unlock() methods can be called from anywhere its difficult to keep track of it and hence more error prone.
Race condition is a scenario when 2 threads access a shared resource. While one thread is reading/changing value of the resource other threads acquire it and changes the value. In simple terms you can understand it like : You call a cinema and ask check for seats availability and you get to know tickets are available. After one hour you reach cinema hall and find all tickets are sold, because somebody bought the tickets between two events done by you - check availability and act to buy them. This is race condition. Here you are other people are racing to buy the ticket. There are commonly 2 types of race condition:
'Check and Act' race condition.
'read-modify-update' race condition.
Deadlocak describes a situation where two or more threads are blocked forever, waiting for each other. For example lets take T1 and T2 are two threads. T1 has acquired lock on a monitor L1 while T2 has acquired lock on monitor L2. Now if suppose T1 is waiting for lock L2 and T2 is waiting for lock L1, both T1 and T2 will keep waiting infinitely and this becomes a deadlock.
How you detect deadlock: Deadlock usually gets ditected because your application (or some threads) becomes unresponsive. This situation is also called hang.
What causes deadlock: Improper synchronization where you don't follow proper sequence while acquiring locks.
How you avoid deadlock: You can avoid deadlock by proper synchronization. Say Thread T1 is acquiring two locks L1 and L2. Now if any other thread also need these two locks, make sure each thread acquire them in same order.
You can't tell your program that a particular thread has hung. So in such scenario you may opt to kill one or all e threads and analyse the thread dump to avoid such case in future. In normal scenario, however, you can tell a thread to stop by one of below approach:
1. You can periodically check for a flag. This is most commonly used approach.
2. You can have thread to be interruptible.
java.uti.concurrent.Executor is an interface. It has execute() method. This is used to execute a Runnable object. Another way to create thread.


<< Previous Page : 1 2 3 4 5 6 7 8 Next >>