• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference  RSS feed

 
donatellah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am new here .. Hi there..

i don't know what is the difference between synchronizing an object or method..am really confused..

get any help!!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.
 
donatellah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooops..i changed it..
thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not OK: we need two, different, real-sounding names, a first and last name, with a space between.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We always synchronize on an object instance.

When you put the keyword on an instance method, you synchronize on the object that has the method.

When you put the keyword on a static method you synchronize on the class object which is shared among all instances and static methods.

When you use the synchronized(object) syntax you synchronize on the specified object.

Some other differences ...

The method keywords synchronize the entire method. There may be code in the method that doesn't really require synchronization, and that can make the sync block take longer than it needs to which can be a performance hit. Synchronizing on a specified object for only a line or two might be a better choice.

The method keywords synchronize on the entire object, which is visible to any number of other objects and threads. Some of the folks here call that making the monitor public. There is a chance for somebody to synchronize on your object in unexpected ways and make unpredictable things happen. That sounds bad, no?

Think about synchronizing the smallest possible block of code - not a method - on a private object. When that works, it avoids those potential problems.

Any of that help?
 
Swapnil Shroff
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[When you use the synchronized(object) syntax you synchronize on the specified object.]

can you give example of this?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a purely made up thing ... needs some imports to compile ...

Here I've synchronized on a variable called "locker". It doesn't matter what this object is, so I used the most abstract thing I could come up with, an instance of Object. locker is private, so no other code from outside my class could ever synchronize on it. If I made add and remove synchronized methods, then the monitor is on the instance of Sample, which could be owned by some other object.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!