• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static synchronized method?

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw some posted code on another forum that had some methods in a class with both static and synchronized modifiers:

public static synchronized int aMethod(<parms> {

Is there an object for the methods to synch on?
Would the synchronized modifier be any use?
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's class-level synchronization - it synchronises on the Class object for that particular class (returned by getClass()). So all static methods are synchronised together and all instance methods are synchronised together independently.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Norm Radder:
I saw some posted code on another forum that had some methods in a class with both static and synchronized modifiers:

public static synchronized int aMethod(<parms> {

Is there an object for the methods to synch on?
Would the synchronized modifier be any use?




static identifier says, no instance of the class is required to access that method and static synchronized says, you need class level lock to access that method. In other words, more than one static synchronized methods can't be executing simultaneously, as there is only class level lock, therefore only one method (static synchronized) will be in execution in one moment of time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic