• Post Reply Bookmark Topic Watch Topic
  • New Topic

static synchronized method  RSS feed

 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that when static synchronized method is called it acquires the lock on Class object therefore until this static synchronized method terminates other threads can't run this synchronized static method.
As mentioned in the k&B book

A static synchronized method and a non-static synchronized method
will not block each other, ever. The static method locks on a Class
instance while the non-static method locks on the this instance—these
actions do not interfere with each other at all.

I have 2 requests:
1>can anyone give a example and make these points more clear
If possible,please give analogy to distinguish between non-static synchronized method and static synchronized method.

My analogy about synchronized method:
5 persons(Threads) want to use a single bathroom (resources) then until a person comes out of bathroom(synchronized method) other persons has to wait outside(in runnable pool)

2>please modify it in context of both static synchronized method and non static synchronized method and show distinction.


thanks in advance
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your analogy can work. So you have one bathroom and five people wanting to use it. Let's say these people are blind so we can control what criteria they use to see if the bathroom is empty (available).

For the synchronized method (non-static), the people walk up to the edge of the bathroom and call out "Is there anyone in the bathroom?" If there is, the person must answer. Thus the lock is established and the person cannot enter the bathroom. When the person leaves the bathroom, they say they are "done", and someone else can use the bathroom. (This assumes that the object being synchronized on is the bathroom. If we're synchronizing on the Person, the criteria is that the person asks himself "Am I currently in the bathroom", understanding that he can be in multiple places at once. If he is already in the bathroom, he can't enter the bathroom again until he has exited it.)

For the static synchronized method, the people walk up to the bathroom and check if the door is closed (class lock). If the door is closed, there is assumed to be a person in the bathroom. If the door is open, there is not a person in the bathroom. The person enters the bathroom and closes the door. (Note that the non-static person did not close the door when he entered - these people are all blind after all). If the door is closed, the person will not respond to questions whether he is in the bathroom (because that's the non-static lock criteria).

To test this lock and why synchronization makes a difference:
Person A enters the bathroom through the non-static method.
We also have Person B, who is non-static,
Person C who is static, and
Person D who is static.

Person A enters the bathroom. Person B tries to enter, but cannot (Person A says "It's busy").
Person C enters the bathroom, because Person A left the door open. There are now two people in the bathroom. Person A is not aware of Person C's existence, and vice versa.
Person D tries to enter, but cannot (the door is closed).
Person A exits the bathroom, Person B enters.
Person C exits the bathroom, Person D enters. Since they're using different criteria to check if it's busy they both think it's empty.

Static methods are not attached to an object instance though, so the method signatures would likely look different.
Instance method:


Static method:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roopam Samal wrote:2>please modify it in context of both static synchronized method and non static synchronized method and show distinction.

There's nothing to modify. In the instance case you can have more than one bathroom, and the analogy is for each bathroom; in the static case, there is only ONE bathroom.

That said, I can't remember EVER using a static synchronized method, except possibly by accident; and I'm pretty sure I've never written one - so if you think you might need one, ask yourself why.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!