• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

What is meaning of a static method to synchronized???

 
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

Khalid Mughal(page 148) says that even if we have static synchronized method, and there are more than one thread to exceute the method, then their excecution is mutually exclusive among all threads i.e. at any time, at most one thread can be executing a synchronized method on an object.

but i dont understand why a static method be sunchronized as its executed by the class finally even if we have an objects ref of the corresponding class. How can a execution of a static method be synchronized or excution happens only if that thread has the lock for the object that ref refers.???

kindly explain......

thanx
amit
 
Ranch Hand
Posts: 456
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well static means that the method does not require an instantiation of the class in order to use the method. You can almost consider it a kind of global method. Now regardless of whether this method belongs to a class or a specifc instantiation of a class, if the method does something that is not thread safe then you do not want to have more than one thread accessing it at a time, thus you synchronize it.
For example if your static method alters a counter and one thread wants to base some logic on this counter at the same time that the 2nd thread is altering the counter you might get an incorrect result. Therefore you might synchronize the static methods that check the counter value and that increment the counter. When you synchronize on a static method you acquire a lock for the whole class. Thus no other thread can access anything of this class.
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but i dont understand why a static method be sunchronized as its executed by the class finally even if we have an objects ref of the corresponding class. How can a execution of a static method be synchronized or excution happens only if that thread has the lock for the object that ref refers.???



Good observation... obviously, it is not possible to grab a lock of an object, when you don't have an object.

Synchronization of static methods, grabs the lock from the Class object for the class -- which bring up two points. Since there is supposely one Class object per class, it will synchronize across all instances of the object. (and even when you don't have an instance) And two, since this class is different from all instances, it is possible to call a synchronized static method, and synchronized non-static method, at the same time.

Henry
 
Amit Das
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

Thankyou Henry/Daimen........thanks a lot indeed!!!

but Henry, could you bother to explain me ur stmt:


Since there is supposely one Class object per class, ]it will synchronize across all instances of the object. (and even when you don't have an instance) And two, since this class is different from all instances, it is possible to call a synchronized static method, and synchronized non-static method, at the same time.



plz expain me the portion in BOLD with some example if you dont mind.

thanx
amit
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any comments... ???
 
reply
    Bookmark Topic Watch Topic
  • New Topic