• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronozation

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what is the difference between function synchronization and object synchronization.
Thanks
Bhaskar
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm...there is only one kind of synchronization.

If you synchronize a whole method, the lock goes on instance of the object in which the method is defined. If you synchronize just a block of code, then you get to choose to which object the code is locked. But in either case, the synchronization simply prevents more than one thread from calling on that code at any given time.

That brings up an interesting question. Is it possible, I wonder, to change during run time which object gets a given synchronized code block's lock. Something like:

synchronized(k.next()) {

}

where k is an iterator on some collection.



Chris
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronization is related to object monitors. Every object has an associated object monitor that is comprised of a Mutex and a Condition Variable.

When you synchronize:
- a static method, you acquire the Mutex associated with the Class instance
for the object's class;
- an instance method, you acquire the Mutex for the instance for which the method is called;
- an arbitrary object using synchronized(object), the Mutex of this object.

Condition Variables are related to wait(), notify() and notifyAll(). You must have acquired the corresponding Mutex for the condition variable to be able to call any of these functions:

wait()
Releases the Mutex and waits until the condition variable is signaled
and the calling thread is unblocked.

notify()
Signals the Condition Variable and unblocks one thread that called wait().
The unblocked thread must acquire the Mutex and competes with
any other thread that is trying to acquire the Mutex by
performing synchronize. Don't assume that the next thread that will
acquire the Mutex is the thread that returns from wait().

notifyAll()
Signals the Condition Variable and unblocks all threads that
called wait().
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just thought I'd stress that by definition, "synchronized method" means you synchronized on "this" for the entire method block. Namely the following are identical:


Those 2 forms produce the same behaviour, from the average programmer's point of view.
Admittedly, I've tried having a look at the actual byte code, and javac appears to issue a different bytecode for those two class versions. However, I would suspect that the runtime optimizer will eventuall converge both versions to the exact same code. If anyone happens to have more accurate information on this, I'd appreciate it.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christopher Arthur:


/.../ If you synchronize just a block of code, then you get to choose to which object the code is locked. /.../

That brings up an interesting question. Is it possible, I wonder, to change during run time which object gets a given synchronized code block's lock. Something like:

synchronized(k.next()) {

}



I think you answered your own question. There is no constraint that the expression should be a compile time constant. If so, it would rule out "synchronized(this)" (as 'this' is as far from constant you can get) which would make things a lot less interesting.

/Dan
 
Dan Johnsson
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bhaskar Gara:
Hi,
what is the difference between function synchronization and object synchronization.
Thanks
Bhaskar



There is a difference when you use the "synchronized" modifier on a static method. In that case it is the corresponding Class object that is locked on.

/Dan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic