• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Synchronization question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is difference between class level and method level synchronisation?
It would be great if you could explain with example.

Thanks for your efforts.

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic