• Post Reply Bookmark Topic Watch Topic
  • New Topic

static synchronized method?  RSS feed

 
Norm Radder
Rancher
Posts: 1734
22
  • Mark post as helpful
  • send pies
  • 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?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • 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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!