aspose file tools*
The moose likes Threads and Synchronization and the fly likes static synchronized method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "static synchronized method?" Watch "static synchronized method?" New topic
Author

static synchronized method?

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
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

Joined: Mar 27, 2003
Posts: 836
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.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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.


cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static synchronized method?