Answer Posted / 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 View All Answers
What is not thread safe?
What do you mean by synchronized non access modifier?
Can we sort list in java?
What does singleton mean in java?
What are the various access specifiers for java classes?
How many bytes is a unicode character?
What methods are used in Servlet?Applet communication?
What is JVM and is it platform independent?
Can we pass null as argument in java?
What does the ‘static’ keyword mean? Is it possible to override private or static method in java?
What is string syntax?
What is the major advantage of external iteration over internal iteration?
What is a function in java?
Explain illegalmonitorstateexception and when it will be thrown?
What is collection api?