• 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

semantic of synchronization

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by adeel ansari:
how about this?

- when a thread is executing a synchronized block no other thread (using the same instance) can enter that block before it comes out.

- or the other thread (using the same instance) can enter the block but can not access that particular object, which we specified in synchronized(obj), simultaneously. and wait for the turn.

which one is right??
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first is right. On entering the synchronized block, the thread is getting the monitor on the object you are synchronizing on. The monitor is released when leaving the synchronized block (or when calling wait()); Only one thread can hold the monitor to an object at the same time.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by adeel ansari:
when a thread is executing a synchronized block no other thread (using the same instance) can enter that block before it comes out.


One important key to understanding synchronization is to realize that whether using a synchronized method or block you are always synchronizing on an object instance* -- not a method or block of code. The method or block only gives you the scope of the lock.

If you have two different synchronized blocks that use the same object instance for their lock, they will block each other. If all of class A's methods are synchronized, calling a method of an instance of A will block any other synchronized blocks using that same instance of A. Again, the locked instance is what matters.

* This is the same even for static synchronized methods. How? The methods lock the singleton Class instance.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the first is correct.
the second is absolutely and completely false.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i just remove this "(using the same instance)" from the first statement then what you people say.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by adeel ansari:
if i just remove this "(using the same instance)" from the first statement then what you people say.



Then it becomes wrong.
reply
    Bookmark Topic Watch Topic
  • New Topic