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;

              }

  }