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.