• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explain how static methods can be synchronized clearly?

 
vvus bharadwaj
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic