How does synchronized modifier work?
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 |
What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?
Difference between string, stringbuffer and stringbuilder?
What is the difference between Synchronizing mehtod & Synchronizing block?
What is the this keyword?
Is 'sizeof' a keyword?
How do you know if a value is nan?
we know that every java prog must follows OOPS Principles. Anybody can answer, HOW THE FOLLOWING PROGRAM FOLLOWS OOPS CONCEPTS i.e, Inheritance,Polymarphism,Encapsulation? class a{ public static void main(String args[]){ System.out.println("Hello World"); } }
java Api provided try catch finally ,try catch(its ok) but why try finally and exception occured at try then how the flow is?
What is meant by object oriented programming – oop?
What is the access scope of protected access specifier?
Explain about data types?
What does indexof mean?