Tuesday, September 11, 2012

Multi threading Interview Questions - 1

Multi-threading is a concept that even some of the experience developers also find difficult to understand. If you are appearing for investment bank, insurance or healthcare technology services company, the chances are that you will be tested thoroughly on the multi-threading concepts. Depending upon your experience, the questions may vary from basic to advanced levels. There are generally a set of written questions as well on this topic, which I will try to compile very soon. So, here is the list of basic questions on multi-threading.

Question 1: What is difference between a process and thread?
A process is a single execution of a program whereas a thread is a single execution sequence within a process. A process may contain multiple threads. A thread is sometimes referred to as a lightweight process. Threads in a JVM share the same heap space. Hence, threads can share same object. Threads have their own stack space, this is how one invocation of a method and its local variables are kept thread safe. Heap is not thread safe and hence must be synchronised for thread safety. 

Question 2: What are the different ways of creating Threads?
1) By implementing Runnable
2) By Extending Thread class
3) By using Executor Framework

Question 3: Which of the above method should be use?
Generally we should try to create threads by implementing Runnable as it will come in handy, when we need multiple inheritance.

Question 4: What are Thread states possible?
Thread can have the following states:
1) Runnable - Thread has started and is waiting for the Thread Scheduler to pick and execute.
2) Running - Thread is actively executing the code.
3) Waiting - Waiting for some external event like finish a file I/O to finish.
4) Sleeping - Thread are put to sleep for a certain period of time and will become runnable when it wakes from sleeping duration.
5) Blocked on I/O or Synchronised: Blocked by an external call to I/O or waiting to acquire lock.
6) Dead - Thread has finished executing code.
Question 5: What is difference between sleep and wait method?
Sleep method is used to stop the thread execution for a certain period of time, whereas wait method is used to make the thread wait for some external event or woke up by another thread by calling notify() or notifyAll().

Question 6: What is the difference between notify() and notifyAll()?
notify() and notifyAll() methods are used to pass the control to another thread that might be in the waiting state. When we call notify() it only means that the call will wake up the thread next in line. However, by calling notifyAll() we are actually passing the control to a larger number of threads which in turn may again compete to get control.

Question 7: What is difference between Thread.start() and Thread.run()?
Thread.start() actually does the job of calling Thread.run(). If we call the Thread.run() directly then it would be a simple method call from the main thread. It will not cause the thread to run independently.

Question 8: When is the InvalidMonitorStateException is thrown? why?
This exception is thrown when we try to call wait(), notify() or notifyAll() methods on an object in your program where you do not have the lock on the object. InvalidMonitorStateException extends RuntimeException and hence we do not need to catch it.

Question 9: What happens if the static method is defined as synchronized?
Executing such a method would mean that the thread has acquired a "Class" level lock, which means other threads cannot executed any other "static synchronized" method. This is different then the instance method synchronization in the way that two threads can execute the same synchronized instance method as long as they are executing it on the different instance of the object.