Friday, December 27, 2013

Commonly Questions About Multi Threading



1. What is synchronisation in respect to multi-threading in Java?

This is the power to control access to multiple threads to shared resources. Without proper synchronisation, one Java thread may modify a shared variable while another thread may still be using it. This would in turn lead to errors and glitches in a program.

2. Explain different ways of using threads?

Java threads can be implemented using runnable interfaces or by extending the thread class itself. Of the two methods, the runnable interface is considered to be more advantageous, when the programmer is going with multiple inheritance. 

3. Difference between Thread.start() & Thread.run() method?

The thread.start() method is a native method that runs the thread.run() method. Calling the thread.run() method will execute it in the same thread, which defeats the purpose of creating a new thread. 

4. Why do we need run() & start() method both? Can it be achieved by only the run method?

The run() and start() methods are both needed for the Java Virtual Machine (JVM) to create a separate thread that can not be differentiated from normal method calls. This is the task performed by the start() method, which has to be called explicitly. The other advantage of having these two methods is that the programmer can run any object as a thread if it is implementing a runnable interface. This solves the multiple inheritance problems that you may face with Java.

5. What is ThreadLocal class? How can it be used?

The following points about ThreadLocal variables should help:

- This variable gives a separate copy of its value to each thread that is using it.
- These variable are usually static fields in the classes that want to work with the thread.
- When multiple threads access the instance, each gets its own copy of the variable.
- The ThreadLocal variable is often used in the DAO pattern. In this, the DAO class can be kept as a singleton, but the database connection can be maintained for each thread separately. This is called Per Thread Singleton.

6. When InvalidMonitorStateException is thrown? Why?

The InvalidMonitorStateException is thrown when the user tries to call the wait(), notify() or notifyAll() methods for an Object. The Object needs to call the method(s) from a place in the program where you do not have a lock on the object. This means that you do not have a synchronised block or method of the object but are still trying to call the notify(), wait() or notifyAll() methods. They all throw the same IllegalMonitorStateException exception. The exception is a subclass of the RuntimeException.

7. What is the difference between sleep(), suspend() and wait()?

The sleep() method takes the currently running thread to the Not Runnable state for a specified amount of time. A sleeping thread can not be entered by another thread and if a thread is running a synchronised block or method when it is put to sleep, then no other thread will be able to call this block or method either. A sleeping thread can be woken up by a thread calling t.interrupt on it. Sleep() will always affect the current thread since it is a static method.

Suspend() on the other hand is a deprecated method. It sends a thread into suspended state, which means that it keeps all its monitors and can not be interrupted. It has been deprecated since it may cause deadlocks.

The wait() method also puts the current thread into the Not Runnable mode but it is invoked on a locked object and not a thread.

Here is the sequence of operations you can think

- A thread X is running a synchronised block with a lock on object A
- Another thread Y comes to execute the synchronized block and finds that it’s already acquired.
- Now Y calls A.wait() method for waiting on the lock to be released the X thread.
- X thread finishes all its synchronised block work.
- X thread calls A.notifyAll() to notify all waiting threads that its done using the lock.
- Since Y thread is first in the queue of waiting it acquires the lock and starts processing.

8. What happens when a static method is made synchronised?

When a thread enters a synchronised static method, the class itself will get locked by the thread monitor. This is because synchronised static methods have a lock on the class they are in. So, no other thread can enter the synchronised static methods in that class anymore. 

9. Can a thread call a non-synchronized instance method of an Object when a synchronised method is being executed?

Yes, non synchronised instance methods can be called always and no lock object check is performed for such methods. In fact, the method is called even when it is not declared if you are working with shared data.

That is why it is important to be careful while working with this. You declare a method as synchronised based on the critical section access. If a method does not access a critical section, then it doesn’t need to be a synchronised method.

10. What is a deadlock?

If two or more threads have been blocked forever, they are said to be in a deadlock and waiting for each other. This usually happens when the two threads, each of which has a lock on one resource, tries to get a lock on the others’ resource. 

The most common causes are:

- When two threads call Thread.join() on each other.
- When two threads use nested synchronised blocks to lock two objects and the blocks lock the same objects in different order.


No comments:

Post a Comment