File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization question" Watch "Synchronization question" New topic
Author

Synchronization question

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
What is difference between class level and method level synchronisation?
It would be great if you could explain with example.

Thanks for your efforts.



Thanks<br />Dinesh
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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.

eg



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Just to add a little to Ankit's explanation:

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
Zdenek Pine
Greenhorn

Joined: Oct 06, 2008
Posts: 9
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 ]
Wu Kong
Greenhorn

Joined: Oct 22, 2008
Posts: 2
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?"

WK
SCJP/SCWCD/SCJD/SCEA
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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


Please help me to undertand ranchers
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization question