Java Multi-Threading Interview Questions-3

Q21. What is thread stack and Heap space as per Java Memory model?

Ans: Java memory model divides reserved memory in two separate area known as thread stack and Heap Space.

Thread Stack: It is specific to each thread. If you have 100 threads running in your application. You will be having 100 thread stacks.  Thread stack keeps information in “call stack”. Thread stack also keeps all local variables for each method. Local variable in thread stack will not be visible to another thread hence, thread safe.

Call Stack: Call stack keep the information about all the method it has executed till this point. For instance there are 5 method named as method1(), method2().. method5().. and you are currently on method3(). It will keep information on call stack as below. However, local variable are GCed as soon as thread completes the method execution.

method3()

method2() : local variable of method2 as well

method1() : local variable of method2 as well

 

Heap Space: It contains all objects created in your java application. It can be created by any thread. All objects will shared same memory space. Objects could be local variable or member variable all will be stored in heap space.

Q22. Static class variable stored in Heap space or in Thread stack?

Ans: Static class variables are stored on Heap space, with the class definition.

Q23.  How java memory model mapped to Hardware memory on multiple CPU?

Ans: Let’s first understand Hardware memory architecture: There are mainly three sections of memory on any computer (Remember your college time computer architecture syllabus).

·         RAM: Main Memory (It is shared across CPUs). It is the biggest memory area for JVM.

·         CPU Cache: Each CPU has its own CPU cache. Accessing CPU cache is faster than main memory but slower than CPU registers.

·         CPU Registers: Each CPU has its own Registers as part of CPU memory. Most of the calculations happen on CPU registers and it will be much faster.

Each CPU is capable of running single thread at any moment. So if we have multithreaded application (each thread will be running on separate CPU, assume we have 10 threads and 2 CPUs, then only two threads at a time can execute concurrently out of 10 threads.)

Q24. What is the Role of RAM, CPU cache and Registers in java process?

Ans: Whenever, any calculations/process needs to be executed by a thread. It will read value from main memory and load into CPU cache. And from CPU cache to registers.

Once calculation is completed resulting variable returned from registers to cache and flushed from cache to main memory.

From CPU Cache to main memory flushing happens only when CPU needs more memory to do some calculations or stored new variable

Q25. How Thread stack and Heap Space mapped to this Hardware memory model.

Ans: So at hardware level there is no difference between Thread Stack and Heap Space.  It is JVM responsibility to keep them separate. Thread Stack will share have their variables stored in any of the Hardware memory area e.g. Main Memory, CPU Cache and Registers. Similarly Heap Space can also share any of the memory area.

Q26. What all problems needs to be taken care because of these memory model differences between Hardware and JVM?

Ans: There are mainly below problems needs to be taken care

·         Race conditions (Visibility of variables across threads) :  Lets assume we are having a one variable named counter as below

int counter=0;

counter = counter+1; 

Now two threads TreadA and ThreadB , both are executing same code segment(Critical Section). Now ThreadA read the value of counter as counter=0 and load into CPU cache. At the same time another thread ThreadB also read this variable as counter=0 and load and keeps it in its own CPU cache. Both the thread will now update value by 1 independently on their own CPU cache. And their changes are not visible to each other Thread. Which cause wrong final value. Because both will be writing this value as 1 (But it should be 2, because it is incremented twice). This problem can be avoided by declaring counter variable as a volatile or this critical section needs to be surrounded by Synchronized block. 

Q27. How Synchronized block will help in such scenario?

Ans: If we surround Critical section of the code by Synchronized block, it will guarantee following

·         Only one thread at a time can access critical section of the code

·         All the variables of Critical section will always be read from main memory

·         Thread as soon as leave synchronized block, it will flush all the variables to main memory.

Because of all these guarantees, Race condition will never occur.

Q28. What happens, when you write code inside synchronize block?

Ans : If you add synchronized block around critical section of the code, only one thread at a time can execute this code section. Because before entering synchronized block thread (ThreadA) has to first get a lock on the object (also known as monitor). And only one thread can get a lock, if other thread (ThreadB) wants the lock than it has to wait for ThreadA to release the lock.

Q29. Which all the places, we can apply synchronized keyword?

Ans : We can apply synchronized block following places in code

·         Method level (Both static and non-static)

·         Code block (Inside both static and non-static method)

Q30. Give example of both method level as well as block level synchronization?

Ans:

·         Method level (Non static): In this case thread will have to take a lock on this object (current instance) from of which this foo method needs to be executed.

public synchronized void foo(int counter){

      this.counter += counter;

  }

·         Method level (Static) : In this case thread will have to take a lock on Class object (Definition of Class stored in Heap Space) from of which this foo method needs to be executed.

public static synchronized void foo(int counter){

      this.counter += counter;

  }

·         Block Level (non-static method): In this case thread will have to take a lock on this object (current instance) from of which this foo method needs to be executed.

public  void add(int value){

synchronized(this){

      count += value;

              }

  }

·         Block level (Inside static method)

public static void add(int value){

synchronized(this){

      count += value;

              }

  } 



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