How does synchronized modifier work?



How does synchronized modifier work?..

Answer / javamasque

Synchronized is used to make a resource thread safe. If an object or resources is accessed by more than one thread, then these should be under synchronization. Only method and block can be synchronized.

Synchronized method
• As an instance method is synchronized then, the object is locked for a period of time as a thread is accessing the method.
• As a class method (static) is synchronized then, whole class is locked for a period of time as a thread is accessing the class method.

Synchronized block
• A particular set of statements (lines of codes) inside method or block is synchronized with synchronized block.
• An object reference is passed inside synchronized block to make a particular set of statements inside instance method or block.

synchronized (this){
}
• If a particular set of synchronized statements inside instance method or block is accessed by a thread then whole object is locked.
• A class reference is passed inside synchronized block to make a particular set of statements inside static method or block.

Synchronized (Class.class){
}

Is This Answer Correct ?    4 Yes 0 No

Post New Answer

More Core Java Interview Questions

Does windows 10 need java?

0 Answers  


What is the multilevel inheritance. and also give the Example of it ?

5 Answers  


In the HashMap, we know the values but we dont know the key, then how can we get the key from HashMap ?????

4 Answers   Kanbay,


Can a constructor call another constructor?

0 Answers  


what is meaning of JIT?

4 Answers  






what Data encapsulation in java?

5 Answers  


Is java ee a framework?

0 Answers  


What is garbage collection? Can it be forced to run?

0 Answers   Global Logic,


Can we clone singleton class in java?

0 Answers  


What are the performance implications of interfaces over abstract classes?

1 Answers  


Can u write constructor in abstract.If yes den when it will be invoked.

4 Answers   SunGard,


What is a container in a gui?

0 Answers  


Categories