Java Multi-Threading Interview Questions-2

Q11. What is a Race Condition?

Ans: When multiple thread execute Critical section of code segment, and thread execution order can lead to different results can be considered as a Race Condition. Lets assume we have below code segment

Class HadoopExam 

{

Int counter=0;

Int foo(){

return counter= counter+1; //Critical Section

}

}

Now there are two threads ThreadA and ThreadB. Both are executing above code. ThreadA read value of counter (which is 0 as of now) and will be updating it by one. However, as soon as ThreadA reads counter value as 0, at the same time ThreadB also read counter value which is also 0 (because ThreadA has not written back). Now finally when ThreadA and ThreadB both writes the Value. It will become 1 instead of 2 . Which is a wrong results, because counter is increased twice from 0 and result is still 1. This can be avoided by executing thread in proper order. This problem is known as a Race Condition. Race condition occurs only when Thread update the resource values, only reading will not create Race condition.

Q12.  How can you avoid Race Condition?

Ans: As we can see in above example “counter=counter+1” is not an Atomic operation. To avoid Race condition we need to make operation atomic. This can be done using Synchronized block or method. So only one thread at a time can execute Critical Section of the code. Hence, ThreadA is executing “counter=counter+1” then ThreadB has to wait.

Q13. What is the Impact of Synchronizing “Critical Section” of code?

Ans: In above example we have two threads ThreadA and ThreadB, and by Synchronizing we are allowing only one thread to execute and other ThreadB needs to wait, which will impact the overall performance of multithreaded application. Hence, it is always advisable to break critical sections in multiple part and synchronized accordingly. So it will not impact overall throughput of multithreaded application.

If Critical Section of code is larger than, we have to be very careful while breaking multiple Synchronized block, else it could lead another problem

Q14.  What do you mean by Thread Safe code?

Ans: Code which does not have any side effect, if multiple thread calls same code in different order is called Thread safe code. And code must not have any Race Condition, Deadlock ans Starvation.

Q15. What is local variables and why they are Thread Safe?

Ans: Variables you defined in a method is called local variable. See below example

Int foo(){

Int localCounter=10;

return localCounter = localCounter +1; //Local Variable increment

}

In above code “localCounter” is a local variable.

Thread safe: Local primitive variable are always thread safe because, they are always stored as part of thread’s stack (And thread stack will never be shared across threads). So it is always used by one thread. Hence, it is thread safe in above code “localCounter” is created in thread stack and once thread is out of method block, this variable will be Garbage collected.

Q16. Is Local Object thread safe (Important)?

Ans: As local Objects are not same as local primitive values. Local objects references are always stored in Thread’s local stack but actual instance of Object will be stored in Shared Heap(memory area shared among thread).

If we use this local object in the same method block and never shared with another thread than it is a thread safe. Even, you pass same object reference in another method and no alteration is done by any other thread on this object than still it is a Thread safe object.

However, if you pass primitive local variable to another thread it is thread safe, because it always pass a copy of the variable. But if you pass Objects it will pass a copy of reference, which will point to same object and can alter the object (And code will not be thread safe)

Q17. What is Object Member variable?

Ans: Object member variables are fields which are created as a member of class and not a method. See below example.

Class HadoopExam 

{

Int counter=0;

 Int foo(){

return counter= counter+1; //Critical Section

}

}

Here, counter is member variable of a Class HadoopExam.

Q18. Is Member variable are thread safer?

Ans: Object member variable similar to any other objects will be stored in heap (shared memory area). Hence, if we have single instance of HadoopExam class and two different thread TreadA and ThreadB tries to modify member variable counter than this code is not thread safe. Because same copy of the variable will be updated by two different thread. Which can cause Race condition.

If you have two different instances of HadoopExam class and each Thread works on different than there would not be any issue. Because each thread is working on different copy of member variable. (What happen if member variable is Static and String constants?)

Q19. What is Immutable objects and are they thread safe?

Ans: Any objects which is once created and cannot be modified after creation will be called Immutable objects. See below code

public class HadoopExam{

  private int counter = 0;

   public HadoopExam(int value){

    this.value = value;

  }

 public int getCounter(){

    return this.counter;

  }

} 

Above class is defined that, you can create object as below.

HadoopExam instance = new HadoopExam(20);

Now, once instance is created there is no way by which we can change instance.counter value. Hence, this class is called Immutable.

Now, we have instance which cannot be modified once created. So as many number of threads use it, no problem. Because nobody(no thread) can modify this object. Hence, immutable object are always thread safe

Q20. What is Java Memory Model?

Ans: Java Memory model defines, how JVM works with the computer main memory also known as RAM. Whenever you launch a Java application, it reserves some memory for RAM for your application also based on JVM arguments it can ask more memory during your application run if needed.

JVM itself you can assume as an OS, which will interact with your computer OS and it has its own way to manage memory reserved by your Java application, which is known as Java Memory Model.

Java memory model specifies how different thread see the shared variable in main memory. So it is about visibility of values across threads of shared variable.



http://www.hadoopexam.com/cloudera_certification/cca175/cca_175_hadoop_cloudera_spark_certification_questions_dumps_practice_test.html