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 is the difference between final, finally and finalize() in java?
Will the jvm load the package twice at runtime?
What is java used for on a computer?
Can an interface have a class?
What are the different ways of implementing thread? Which one is more advantageous?
public class Test { public static void main(String ar[]) { Integer a = 10; Integer b =10; Integer c = 145; Integer d = 145; System.out.println(a==b); System.out.println(c==d); } }
Which category the java thread do fall in?
Hi ,i convert contrller as jSp And presentation as servlet ...will it do? if so what are advantage and idsadvantages
How does a for loop work java?
What is exception propagation?
What is jar?
Program to Find the second largest element in an array.