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

List the three steps for creating an object for a class?

0 Answers  


What is the difference between method overriding and overloading?

0 Answers  


What do you understand by weak reference?

0 Answers  


Is there is any error if you have multiple main methods in the same class?

10 Answers   Infosys, Wipro,


Is set thread safe java?

0 Answers  






Explain 5 features introduced in jdk 1.7?

0 Answers  


what is actual real time using of oops concepts in projects(Interface,polymorphism.abstraction.........)

2 Answers  


Given: 1. package test; 2. 3. class Target { 4. public String name = “hello”; 5. } What can directly access and change the value of the variable name? 1 any class 2 only the Target class 3 any class in the test package 4 any class that extends Target

7 Answers   Infosys, WW,


What is the life cycle of an Applet ?

1 Answers  


What do you understand by java virtual machine?

0 Answers  


Linked List reverese program

2 Answers   Bally Technologies,


What is the difference between an interface and an abstract class?

0 Answers   Cyient,


Categories