Java Synchronization Interview Question

Java Multithreading Interview Questions Part-4 | In the previous Java Multithreading Interview Questions Part – 3 we have seen interview questions related to Thread Priority, yield(), join() & sleep() Method. Now, in this post, we will see the Java Synchronization Interview Question.

Q1) When should we develop synchronization, and why?

If multiple threads are operating simultaneously on the same Java object/class then there will be a chance to get data inconsistency problems. To overcome this problem we should use the synchronization concept.

Q2) Why is the object data modified in an inconsistent way from multiple threads?

Threads are executing concurrently. In this case, when the first thread execution is paused, the second thread modifies object data in the middle of the first thread execution, after resuming the first thread it uses the second thread modified values, but not its actual values. This is called a data inconsistent modification problem and it leads to wrong results.

Q3) Explain different application areas where synchronization is used?
Bank application, every web application, ticket booking system, and e.t.c.

Q4) How can we develop synchronization in Java?

In Java synchronization is implemented by using synchronized keyword. We can apply synchronized keyword either to methods and/or to local blocks. So, in Java synchronization is developed:-

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses
  1. By using synchronized methods.
  2. By using synchronized blocks.

Q5) What is the synchronized keyword and where can we apply it?
The synchronized is a modifier applicable for methods and blocks but not for class, objects, and variables.

Q6) What is the advantage of Synchronized keyword?
We can resolve data inconsistency problems.

Q7) Disadvantage of synchronized keyword?
It increases the waiting time of threads and gives performance issues.

Q8) What is race condition?*

If multiple threads are operating simultaneously on the same Java object then there will be a chance of data inconsistency problems. This data inconsistency problem in Java is also known as a race condition. To resolve race conditions we should use a synchronized keyword/modifier.

Q9) What is an object-level lock and when is it required?

Every object in Java has a unique lock that is nothing but an object-level lock. Whenever a thread wants to execute an instance synchronized method or block then it needs to acquire the object level lock.

Q10) What is class level lock and when is it required?

Every class in Java has a unique lock which is nothing but a class-level lock. Whenever a thread wants to execute a static synchronized block or method then the thread needs to acquire the class level lock.

Q11) Difference between the class-level lock and object-level lock?

If a thread wants to execute a static synchronized method/block then the class level lock is required. Whereas if a thread wants to execute an instance synchronized method/block then object level lock is required.

Q12) What will happen when the synchronized method is called?
When a synchronized method is called, the object will be locked while executing the method.

Q13) What is the meaning of an object being locked and unlocked?

Locking an object means marking the current object is not allowed to another thread for calling synchronized method/block. Unlocking an object means marking this object as available to other threads to access its functions.

Q14) When a thread is called a monitor?

Monitor means a thread that holds the lock of an object is called monitor. Note that an object has only one monitor that means an object can be locked by one thread at a time.

Q15) While a thread is executing a synchronized method on the given object, are the other threads allowed to execute any other synchronized methods simultaneously on the same object?

No, once an object is locked by a thread then other threads can’t execute any synchronized method/block simultaneously on that object.

Q16) When an object is locked can another thread use it to execute a non-synchronized method?

Yes, because the lock/unlock concept is applicable only for the synchronized method/block but not for the non-synchronized method.

Q17) If threads are using different objects, will the synchronized method execution be concurrent or sequential?

Since objects are different therefore the synchronization concept is not applicable. Hence execution will be concurrent i.e. they will execute simultaneously not one by one.

Q18) What is the difference between declaring instance method and the static method as synchronized?

If we declare an instance method as synchronized its current object is locked so that in this method instance variables of this object are modified sequentially by multiple threads. If we declare a static method as synchronized then its class’s java.lang.Object is locked so that in this method static variables are modified sequentially by multiple threads.

In a static method by using a synchronized block we can only lock an argument object but not the current object because “this” keyword doesn’t exist in the static method.

Q19) What is a synchronized block?
A block surrounded by a synchronized keyword is called a synchronized block.

Q20) What is the need for a synchronized block? 

Assume we have an m1() method with 10k lines of code (linking method) among these lines only 10 lines require synchronization. In that case, if we declare the m1() method as the synchronized method then it is the worst kind of practice in Java programming. It will increase the waiting time of the thread and gives very poor performance. Therefore if only a few lines of code are required for synchronization then it is never recommended to declare the whole method as synchronized rather we should use the synchronized block for those lines of code.

Q21) What is the difference between synchronized methods and blocks?

If a method is declared as synchronized, that method’s complete logic is executed in sequence from multiple threads by using the same object. For different objects, it is executed concurrently from multiple threads. If we declare a block as synchronized, only the statements written inside that block are executed sequentially but not the complete method logic.

Using synchronized methods we can only lock the current object of the method. Using a synchronized block we can lock the current object, argument object of the method, or any class. 

Q22) Can we define multiple synchronized blocks in a method?
Yes, we can define multiple synchronized blocks in a method.

Q23) What is the advantage of the synchronized block over synchronized method?
Waiting time of thread decreased and performance improved.

Q24) How to declare a synchronized block to get the lock of the current object?
synchronized(this)

Q25) How to declare a synchronized block to get the class level lock?
synchronized(ClassName.class)

Q26) Is it possible, a thread can acquire multiple locks simultaneously?
Yes, a thread can acquire multiple locks simultaneously.

class X {
   // x.m1() called
   public synchronized void m1() { // "x" locked
      Y y = new Y();
      synchronized(y) { // "y" locked
         Z z = new Z();
         synchronized(z) { // "z" locked
           // currently thread holds 
           // "x", "y", and "z" objects lock
         }
      }
   }
}

Now, we want to call the m1 method. To call the m1() method,
X x = new X();
x.m1();

When a thread needs to execute a synchronized method, the compulsory thread has to acquire a lock of “x” object. To enter into 1st synchronized block “y” object lock is required. Similarly, to enter into the 2nd synchronized block “z” object lock is required. Therefore inside the 2nd synchronized block thread will have a lock of “x”, “y”, and “z” objects.

Q27) By how many threads an object can be locked at a time?
Only one thread can lock an object at a time.

Q28) When should we develop multiple synchronized blocks instead of declaring complete methods as synchronized?

In the below two cases we develop multiple synchronized blocks,

  1. For executing parts of method logic sequentially for doing object modification.
  2. For executing some part of method logic by locking current object and other part of method by locking argument object.

Q29) What is a synchronized statement?

As per Java specification there is no such type of terminology but the interview person uses this synchronized statement. According to them:- the statement present in a synchronized method or synchronized block is called a synchronized statement.

Q30) What is the meaning of thread-safe & non-thread-safe objects?

If an object is not accessible for multiple threads concurrently for modifying values, or one thread modification on the object is not affected to another thread, then that object is called a thread-safe object.

Q31) How can we develop thread-safe objects?

We can develop thread-safe objects in two ways,

  • By declaring all mutator methods of this object as synchronized. Or,
  • By creating objects as immutable.
Join PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *