aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain how static methods can be synchronized clearly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explain how static methods can be synchronized clearly?" Watch "Explain how static methods can be synchronized clearly?" New topic
Author

Explain how static methods can be synchronized clearly?

vvus bharadwaj
Ranch Hand

Joined: Apr 28, 2012
Posts: 38
I know that Static methods can alose be synchronized.But i dont know the mechanism behind it i.e it deals with Class instance?Please Explain
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

All synchronization blocks synchronize on an object. In the case of a static method, it synchronizes on the instance of the Class class that represents that class (there's a Class object for every class that's loaded).

So, for example, this:
is equivalent to this:


(Edited for stupid mistake )
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

vvus bharadwaj wrote:I know that Static methods can alose be synchronized.But i dont know the mechanism behind it i.e it deals with Class instance?Please Explain


From my understanding static synchronized methods synchronize for the entire class type where as instance synchronized methods synchronise for a single instance of a class... So there could be multiple instances using the same synchronized method. A static synchronized method will on the other hand only allow one instance to use the method at any one time.

If this is wrong, please by all means correct me. I here to flex my cortex.

Respectfully,

TN
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Ted North wrote:From my understanding static synchronized methods synchronize for the entire class type where as instance synchronized methods synchronise for a single instance of a class... So there could be multiple instances using the same synchronized method. A static synchronized method will on the other hand only allow one instance to use the method at any one time.

If this is wrong, please by all means correct me. I here to flex my cortex.

Respectfully,

TN


You're right Ted. That is why when Matt wrote the code above, he made sure the parameter to synchronized was a single instance class object(SyncTest.class) and not the "this" pointer(or empty param). This way if 2 threads were to hit this method, the one which acquires the lock(class object) first would execute first.

Edit : strike.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

I think you guys (Ted and Praveen) are viewing it backwards. It's not really multiple instances using the same synchronized method because all instances share the same code. Synchronization is concerned with the shared data. It may just be the way you worded it but the proper perspective is that when multiple threads are executing, each thread can still execute a synchronized method concurrently as long as they are each executing on separate instances of the class. This is because the lock is on each individual instance and each instance has its own data.

However, if multiple threads tried were to attempt to execute the method on a single instance of the class, all these threads would still have to queue up while one thread at a time obtains the lock and executes the method. With the static synchronized method, the lock object is the class itself, of which there is only one, and the result is the same as when multiple threads try to access a synchronized method of a single instance of the class. I don't know if that makes it clearer or not but it's late and I can't find a way to word it better.

Edit: Ah, I think I found the perfect analogy for you: Imagine 5 threads are 5 guys who need to go to the bathroom. Assuming a bathroom has only one urinal, if there were 5 bathrooms in the building (5 instances of the same class), then the 5 guys could still go do their business at the same time because they each have their own bathroom. However, if there were only one bathroom in the building (again, only one urinal), then the 5 guys would have to line up to use the urinal one at a time. The 5 guys and 1 bathroom scenario is the same equivalent when you have a static synchronized method because the lock is obtained on the class itself.

Clear now?

BTW, Praveen, in a static method, this refers to the class itself, not any particular instance. In a regular method, this refers to the instance of the class on which the current active thread is executing. So testMethod1 and testMethod2 in the listing below are equivalent:


Junilu - [How to Ask Questions] [How to Answer Questions]
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Praveen Kumar M K wrote:
Junilu Lacar wrote:
BTW, Praveen, in a static method, this refers to the class itself, not any particular instance. In a regular method, this refers to the instance of the class on which the current active thread is executing. So testMethod1 and testMethod2 in the listing below are equivalent:


It is indeed late Junilu :-)

By the way, I think we cannot use "this" keyword in static method. Will testMethod2() compile?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Helen: it seems I am getting senile. You are correct to question it. It doesn't compile. Not sure why I had that in my head as a "fact" when in fact, it isn't. I'll go back and edit my replies to avoid any confusion. Thanks for pointing it out.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Helen Ma wrote:
Praveen Kumar M K wrote:
Junilu Lacar wrote:
BTW, Praveen, in a static method, this refers to the class itself, not any particular instance. In a regular method, this refers to the instance of the class on which the current active thread is executing. So testMethod1 and testMethod2 in the listing below are equivalent:


It is indeed late Junilu :-)

By the way, I think we cannot use "this" keyword in static method. Will testMethod2() compile?


Hi Helen,

You are correct. 'this' can not be used in a static context. Please, check-out the following compile error.



Junilu, I think you explained the difference between static synchronized methods and instance synchronized methods correctly other than the incorrect syntax on the 'this' keyword.
Great job everyone.

Respectfully,

TN - juggernaut of shells on the command prompt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain how static methods can be synchronized clearly?