• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization  RSS feed

 
mandeepsangwan kumar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why would i synchronize an object .
what the difference between synchronizing an object and a method .
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
mandeepsangwan kumar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still can't help myself,i already did
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mandeepsangwan kumar wrote:why would i synchronize an object .
what the difference between synchronizing an object and a method .


You would typically use synchronization when multiple entities might be referring to a single entity and these entities perform some logic based on the state of the single entity.

In layman's terms, imagine a bank. People put in their money and the bank then lends this money out. Now if the people who want loans come in first before the people who want to deposit their money, the bank business would collapse.

Now imagine a bank building with a single entrance and multiple teller windows inside.
If you put a security guard at the door and instruct him to let only one customer inside at any given time, you are synchronizing the object.
On the other hand, if you instruct him to let people inside, but only one person can be at a teller window at one time, you are synchronizing the method.

 
mandeepsangwan kumar
Ranch Hand
Posts: 36
 
mandeepsangwan kumar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnak you
i find your explaination helped me clear this concept
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only synchronize on objects. Using the keyword synchronized on a non-static method is the same as synchronizing the entire body of the method on this:

And for a static method, it is the same as synchronizing on the Class object for the class:

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