well class level synchronization is implemented by synchronizing a block of code with a class.
synchronized(String.class) { //some code }
class level synchronization also occurs when you synchronize a static method. then the method is synchronized on the containing class.
method level synchronization occurs when you synchronize a method of a class. If it is a static method, then it will be synchronized on class, if it is a non-static method, it will be synchronized on the instance of the class with which it was called.
When you synchronize on a Class .... all instances of objects instantiated from that class are synchronized. When you synchronize on an object, the lock is only good for method calls to that one specific object instance.
So Class syncronization acts like a more global lock over all instances of that class. Non-static method synchronization acts like a lock specific to each object instance.
The block synchronization allows you to use anything that you want as a lock: a class, a specific object, or a special object created only to BE a lock.
------------------------
Bob
SCJP - 86% - June 11, 2009
Hi all, we should realize, that it is not possible to put synchronized modifier on variable or class identifier - we can only synchronized methods or block inside a method:
synchronized void aMethod() {} - which is roughly: void aMethod() { synchronized(this) {} }
So class level vers. method level seems to be a little bit confuse for me - every synchronization must be on an object (this, System.out, String.class etc.).
It is similar when some people in one room undress and burn to ashes theirs clothes except one dress. Now they are synchronized on the dress - to go outside on the street can only the one with dress on. But on the dress can be changed colour etc., no problem. [ October 22, 2008: Message edited by: Zdenek Pine ]
In Java, static variables and static methods are owned by the class; while the non-static variables and non-static methods are by instances.
I sort of agree with other posts and would assume the correct the question should be "What is difference between class level and instance level synchronisation?"
I realize that my wording may have been confusing. When I said "synchronized on the class" I did not mean that I would apply the synchronized modifier to the Class, I was implying that, when you synchronizes a static method you are using the class as the lock. By the same token, when you synchronized an instance method you are using the specific object as the lock. When the Class is used as the lock it will synchronize the calls of all objects to the synchronized static methods. When the object is used as the lock then only the methods called on that one object are synchronized.
My poor choice of wording.
------------------------
Bob
SCJP - 86% - June 11, 2009
No Bob, your explanation was not confusing. Actually the question itself is confusing. When I first read the question, I was about to say that a class cannot be synchronized. But then I realized that it is about locking on a class.
Thanks for your replies but one question is in mind
In the above code when i create object of class a it will call the methods which are defined as synchronized which is at class level as per said in your replies. Correct me if i am wrong
also, what is difference between this or the way written below
Well Dinesh, you are right. synchronised static method and synchronised(A.class) mean the same thing.
I can't say about Class s = Class.forName(A); synchronised (s) as I don't know reflection in detail. But I feel that this will not work as class level synchronization. Let's see if anyone knows it...