Interview Questions

Mention some guidelines to write thread safe code, most important point we must take care of in multithreading programs?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

32. Mention some guidelines to write thread safe code, most important point we must take care of in multithreading programs?

In multithreading environment it’s important very important to write thread safe code, thread unsafe code can cause a major threat to your application. I have posted many articles regarding thread safety. So overall this will be revision of what we have learned so far i.e. writing thread safe healthy code and avoiding any kind of deadlocks.


1. If method is exposed in multithreading environment and it’s not synchronized (thread unsafe) than it might lead us to race condition, we must try to use synchronized block and synchronized methods. Multiple threads may exist on same object but only one thread of that object can enter synchronized method at a time, though threads on different object can enter same method at same time.

2. Even static variables are not thread safe, they are used in static methods and if static methods are not synchronized then thread on same or different object can enter method concurrently. Multiple threads may exist on same or different objects of class but only one thread can enter static synchronized method at a time, we must consider making static methods as synchronized.

3. If possible, try to use volatile variables. If a field is declared volatile all threads see a consistent value for the variable. Volatile variables at times can be used as alternate to synchronized methods as well.

4. Final variables are thread safe because once assigned some reference of object they cannot point to reference of other object.

s is pointing to String object.
public class MyClass {
final String s=new String("a");
void method(){
s="b"; //compilation error, s cannot point to new reference.
}
}


If final is holding some primitive value it cannot point to other value.

public class MyClass {
final int i=0;
void method(){
i=0; //compilation error, i cannot point to new value.
}
}


5. Usage of local variables : If possible try to use local variables, local variables are thread safe, because every thread has its own stack, i.e. every thread has its own local variables and its pushes all the local variables on stack.

public class MyClass {
void method(){
int i=0; //Local variable, is thread safe.
}
}

6. We must avoid using deadlock prone deprecated thread methods such as destroy(), stop(), suspend() and resume().

7. Using thread safe collections : Rather than using ArrayList we must Vector and in place of using HashMap we must use ConcurrentHashMap or HashTable.

8. We must use VisualVM or jstack to detect problems such as deadlocks and time taken by threads to complete in multi threading programs.

9. Using ThreadLocal : ThreadLocal is a class which provides thread-local variables. Every thread has its own ThreadLocal value that makes ThreadLocal value threadsafe as well.

10. Rather than StringBuffer try using immutable classes such as String. Any change to String produces new String.

(Continued on next question...)

Other Interview Questions